Search
By attending the lab and solving the assignment, a student should…
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_features
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/, search for other materials (duckduckgo, google) or ask your teacher.
Start by reading General information for Python development and cloning the assignment template repository.
We strongly recommend using the .ipynb notebooks provided in the template. ($ pip install jupyter, then $ jupyter notebook in the directory containing basics.ipynb. Or you can use your favorite IDE jupyter notebook plugin.)
$ pip install jupyter
$ jupyter notebook
We provide unit tests in the assignment template, see test_basics.py. To execute the tests, run $ python -m unittest. The tests are provided to simplify local development and debugging of your code. Passing all the unit tests does not automatically mean that your code will pass all BRUTE tests, feel free to write additional tests into test_basics.py if needed. Make sure that all the unittests pass OK, before uploading to BRUTE.
test_basics.py
$ python -m unittest
In the first part of today’s assignment, you will start with some simple matrix manipulation tasks. TRY TO AVOID USING LOOPS FOR MATRIX MANIPULATION IN YOUR PROGRAM! (some hints on how to do that here).
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.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
np.int32(sum_left) - np.int32(sum_right)
lr_features = compute_lr_features(letter_char, alphabet, images, labels)
lr_features
>> compute_lr_features('A', alphabet, images, labels) array([ 120 1223 -144 -161 197 -2921 -998 -944 -120 -304 -884 -1461 -1233 1444 1705 1332 881 212 92 319 -3104 -2829 255 1 -1763 2230 1916 -335 -257 -3568 -5204 -1144 -641 525 182 -768 -844 1536 1139 522 495 353 -251 1345 439 1114 -2087 -107 -563 1491 -1935 -1640 1979 2215 906 1726 1332 365 825 2776 1282 708 1010 429 1141 1145 1896 7 -642 -657 36 368 1079 79 -483 327 -135 888 2270 2211 3860 1248 1371 -857 100 -134 -946 1954 1979 -1575 -837 1363 803 546 -1916 -1808 370 -435 -363 497])
plot_letter_feature_histogram(features_1, features_2, letters)