$24
This is a short tutorial on how to use Python and a review of some small linear algebra ideas. The point of this assignment is to get you used to manipulating matrices and images in Python with NumPy.
Please put together a PDF with your results for each problem, and submit it to the appropriate assignment on Gradescope. We recommend you to add these answers to the latex template les on our website, but you can also create a PDF in any other way you prefer. There will be an additional coding assignment on Gradescope that has an autograder and is there to help you double check your code. Make sure you use the provided ".py" les to write your Python code. Submit to both the PDF and code assignment, as we will be grading the PDF submissions and using the coding assignment to check your code if needed.
NOTE: This problem set is not representative of future problem sets in terms of length or di culty, but the logistics will be similar (submission, Piazza, etc).
Most likely, you will nd the future problem sets to be more di cult than this one. This problem set is simply to ensure you have a su cient understanding of the basic prerequisites for this class.
Submit your published results to the class Gradescope (Code 6PGPN3). Feel free to ask any questions to the class Piazza forum (but use a private post if you have code or speci cs to discuss).
Piazza
Please register on the class Piazza forum: piazza.com/stanford/winter2021/cs231a.
Basic Matrix/Vector Manipulation (10 points)
In Python, please calculate the following. Given matrix M and vectors a,b,c such that
M =
24563
, a = 1 , b =
2
, c =
223
1
2
3
2 3
2
3
0
67
8
97
637
6
7
1
4
1
6 7
4 5
5
4
5
0
5
4 5
0
2
2
2
De ne Matrix M and Vectors a,b,c in Python. One helpful Python package that is commonly used for linear algebra related problems is Numpy.
(b) Find the dot product of vectors a and b (i.e. a>b). Save this value to the variable aDotb and write its value in your report.
Find the element-wise product of a and b [a1b1; a2b2; a3b3]T and write it in your report.
Find (a>b)M a and write it in your report.
Without using a loop, multiply each row of M element-wise by a. (Hint: The function numpy.tile() may come in handy). Write the result in your report.
Without using a loop, sort all of the values of the new M from (e) in increasing order and plot them in your report.
Basic Image Manipulations (20 points)
(a) Read in the images, image1.jpg and image2.jpg, as color images.
(b) Convert the images to double precision and rescale them to stretch from minimum value 0 to maximum value 1.
Add the images together and re-normalize them to have minimum value 0 and maximum value 1. Display this image in your report.
Create a new image such that the left half of the image is the left half of image1 and the right half of the image is the right half of image2. Display this image in your report.
Using a for loop, create a new image such that every odd numbered row is the correspond-ing row from image1 and the every even row is the corresponding row from image2 (Hint: Remember that indices start at 0 and not 1 in Python). Display this image in your report.
Accomplish the same task as part e without using a for-loop (the functions reshape and tile may be helpful here).
Convert the result from part f to a grayscale image. Display the grayscale image with a title in your report.
Singular Value Decomposition (20 points)
(a) Read in image1 as a grayscale image. Take the singular value decomposition of the image.
i=n
X
(b) Recall from the discussion section that the best rank n approximation of a matrix is ui ivi>,
i=1
where ui, i, and vi are the ith left singular vector, singular value, and right singular vector respectively. Save and display the best rank 1 approximation of the (grayscale) image1 in your report.
(c) Save and display the best rank 20 approximation of the (grayscale) image1 in your report.
2