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 ieonly usenplinalg.svd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetcholivettifaces
from sklearn.neighbors import KNeighborsClassifier
xtrain, ytrain, xtest, ytest getfacesdatantrain ntest
Use SVD to find a basis of the matrix of the training data. The training data should be a D array. Each row of the array should be an image flattened into a vector.
Visualize or basis vectors. Looking at the visualization what does themost importantbasis vector represent? What do the next vectors represent? Using def plotimagesinarowims:
axs pltsubplotslenimsfigsize
for imax in zipimsaxsravel:
aximshowimcmap'gray'
axaxisoff
Find a way to express any given image in particular from the test datasetin your new basis. Find the simplest way possible. Now each image should be a vector of sizeExpressalltraining and test images in this new basis
Pick vectors 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 identicalto the originals. Plot them side by side.
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 shapeas a vector ofrcoordinatesThe resulting vector must represent the coordinates in thermost prominent new basis vectors computed earlier. In other words these should be coordinates in a truncated basisrshould be a parameter of the function.The coordinatesmustcome from the basis computed for the train dataset using SVD
Pickrdimensional vectors of selected images from thetest dataset. Visualize the images they represent display each image for different values ofrTune your choice ofrbased on what you see. Briefly describe your process of choosing the rightr
Perform your dimensionality reduction of the train and test dataset. Based on the observation from the previous part, choose yourrthe number of basis vectors you will use. just use the information coming from SVD on the train data only
Construct train, test and tune yourknearest neighbours classifier using therdimensional representation.train on the dimensionalityreduced training data and test on the dimensionalityreduced test data.use the data obtained from SVDuse theKNeighborsClassifier
Getaccuracy on thetest setandkeep the running time on the entire test set belowsecondFix your finalrand other parameters and train the classifier using the reduced data produced using these parameters.don't have to recompute the SVDJust recompute whatever depends on the parameterr
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
