Search
General information for Python development.
To fulfill this assignment, you need to submit these files (all packed in one .zip file) into the upload system:
.zip
basics.ipynb
basics.py
matrix_manip
compute_letter_mean
compute_lr_histogram
initial1_mean.png
initial2_mean.png
initials_histograms.png
Use template of the assignment. When preparing a zip file for the upload system, do not include any directories, the files have to be in the zip file root.
Beware of using for loops! :)
for
We will be using the Python programming language with the NumPy library during the whole semester. Make sure you are comfortable with these so that you don't spend more time dealing with python/numpy issues than solving the assignment tasks.
For the case you are not too sure about your Python/NumPy skills, have a look here: http://cs231n.github.io/python-numpy-tutorial/, ask your uncle (duckduckgo, google) or your teacher.
Start by reading General information for Python development and cloning the assignment template repository.
In the first part of today’s assignment, you will start with some simple matrix manipulation tasks. TRY TO AVOID USING LOOPS IN YOUR PROGRAM!
Although numpy has a matrix class, we will not be using that. Instead, we will use the array class for representing matrices, vectors, images, lists, etc. We will import numpy using
matrix
array
import numpy as np
Your goal is to complete a function output = matrix_manip(A, B), where A and B are input matrices (represented by np.array). The matrix_manip function should return a python dict containing the results of the operations described below.
output = matrix_manip(A, B)
A
B
np.array
To have some data to work with, lets use the following matrices A and B:
A = np.array([[16, 2, 3, 13], [ 5, 11, 10, 8], [ 9, 7, 6, 12], [ 4, 14, 15, 1]]) B = np.array([[3, 4, 9, 4, 3, 6, 6, 2, 3, 4], [9, 2, 10, 1, 4, 3, 7, 1, 3, 5]])
Your function should work on general input matrices, not only for the A and B shown here or for matrices with the same dimensions.
output['A_transpose']
>> output['A_transpose'] array([[16, 5, 9, 4], [ 2, 11, 7, 14], [ 3, 10, 6, 15], [13, 8, 12, 1]])
output['A_3rd_col']
>> output['A_3rd_col'] array([[ 3], [10], [ 6], [15]])
output['A_slice']
>> output['A_slice'] array([[ 7, 6, 12], [14, 15, 1]])
output['A_gr_inc']
>> output['A_gr_inc'] array([[17, 2, 3, 14, 1], [ 6, 12, 11, 9, 1], [10, 8, 7, 13, 1], [ 5, 15, 16, 1, 1]])
>
C
output['C']
>> output['C'] array([[499, 286, 390, 178], [286, 383, 351, 396], [390, 351, 383, 296], [178, 396, 296, 508]])
output['A_weighted_col_sum']
>> output['A_weighted_col_sum'] 391
np.arange
np.expand_dims
np.sum
float( … )
output['D']
>> output['D'] array([[-1, 0, 5, 0, -1, 2, 2, -2, -1, 0], [ 3, -4, 4, -5, -2, -3, 1, -5, -3, -1]])
D
output['D_select']
>> output['D_select'] array([[ 0, 5, 0, -2], [-4, 4, -5, -5]])
In this part of the assignment, you are supposed to work with a simple input data which contains images of letters. We will use similar data structures later on during the labs. Do the following:
data_33rpz_basics.npz
images
alphabet
labels
Alphabet
loaded_data = np.load("data_33rpz_basics.npz") loaded_data['images']
import matplotlib.pyplot as plt plt.imshow(montage(images), cmap='gray') plt.show()
%matplotlib notebook
uint8
letter_mean = compute_letter_mean(letter_char, alphabet, images, labels)
letter_char
letter_mean
x = sum of pixel values in the left half of image - sum of pixel values in the right half of image
lr_histogram = compute_lr_histogram(letter_char, alphabet, images, labels, num_bins)
num_bins
lr_histogram
>> compute_lr_histogram('A', alphabet, images, labels, 10) array([ 1, 1, 3, 6, 12, 27, 24, 20, 5, 1])
np.histogram