Question: Vectorize and minimize Python loops You must only use SVD ( i . e . , only use np . linalg.svd ) . import numpy

Vectorize and minimize Python loops
Youmustonly use SVD (i.e.,only usenp.linalg.svd).
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_olivetti_faces
from sklearn.neighbors import KNeighborsClassifier
x_train, y_train, x_test, y_test =get_faces_data(n_train =2_000,n_test =10_000)
1.Use SVD to find a basis of the matrix of the training data. The training data should be a 2D array. Each row of the array should be an image flattened into a vector.
2.Visualize 5or 6basis vectors. Looking at the visualization --what does themost importantbasis vector represent? What do the next 3vectors represent? Using def plot_images_in_a_row(ims):
_,axs =plt.subplots(1,len(ims),figsize =(15,5))
for im,ax in zip(ims,axs.ravel()):
ax.imshow(im,cmap='gray')
ax.axis('off')
3.Find a way to express any given image (in particular from the test dataset)in your new basis. Find the simplest way possible. Now each image should be a vector of size64^2.Expressalltraining and test images in this new basis
4.Pick 5vectors, each representing an image from thetest datasetin the new basis. Write code which goes back to the standard basis and visualizes the images. Verify that the images you obtained this way are similar (if not identical)to the originals. Plot them side by side.
5.Implement a function that expresses each image in a collection of images (from the training dataset, test dataset, or any other images of the same shape)as a vector ofrcoordinates.The resulting vector must represent the coordinates in thermost prominent new basis vectors computed earlier. (In other words these should be coordinates in a truncated basis).rshould be a parameter of the function.The coordinatesmustcome from the basis computed for the train dataset using SVD.
6.Pickr-dimensional vectors of 5selected images from thetest dataset. Visualize the images they represent --display each image for different values ofr.Tune your choice ofrbased on what you see. Briefly describe your process of choosing the rightr.
7.Perform your dimensionality reduction of the train and test dataset. Based on the observation from the previous part, choose yourr--the number of basis vectors you will use. just use the information coming from SVD on the train data only
8.Construct, train, test and tune yourk-nearest neighbours classifier using ther-dimensional representation.train on the dimensionality-reduced training data and test on the dimensionality-reduced test data.use the data obtained from SVD.use theKNeighborsClassifier
9.Get>90%accuracy on thetest setandkeep the running time on the entire test set below1second.Fix your finalrand other parameters and train the classifier using the reduced data produced using these parameters.don't have to re-compute the SVD.Just recompute whatever depends on the parameterr

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!