# -*- coding: utf-8 -*- # import the main module in python numpy (numerical python) import numpy as np # load the olivetti face dataset # 1. vector from of the faces faces_image =...

1 answer below »
i have upload the question in two pictures one and 2, also the python and the datasets


# -*- coding: utf-8 -*- # import the main module in python numpy (numerical python) import numpy as np # load the olivetti face dataset # 1. vector from of the faces faces_image = np.load('Desktop/olivetti_faces.npy') # the class (label, or target) of each face faces_target = np.load('Desktop/olivetti_faces_target.npy') #check the size of the dataset: number face_image x number_pixel_height x # number_of _pixels_width faces_image.shape # transform the 2D face image into vectors of 4096 (64x64) pixels faces_data = faces_image.reshape(faces_image.shape[0], faces_image.shape[1] * faces_image.shape[2]) #check the new size of the dataset faces_data.shape #display the label of faces from 0 to 39 (there are 40 persons.) #each person has 10 face images. print(faces_target) #this to avoid printing warning import warnings warnings.filterwarnings("ignore") #skimage is a module to process image from skimage.io import imshow loadImage = faces_image[20] imshow(loadImage) # if skimage dosent work, then it is not installes. # so instead of skimage please use plt of matplotlib import matplotlib.pyplot as plt plt.imshow(faces_image[20], cmap='gray') #n_samples is the number of face image in the dataset n_samples= faces_image.shape[0] #for machine learning we use the 2 data directly # X is 2D dataset. each row is a face image in vector form, and each colon #is a feature(pixel value) X = faces_data n_features = faces_data.shape[1] #y is the label to prodect is the id of the person y = faces_target # number of classes (or persons) n_classes = faces_target.shape[0] print("Total dataset size:") print("n_samples: %d" %n_samples) print("n_featues: %d" %n_features) print("n_classes: %d" % n_classes) # take only two classes scince our perceptron is designes for two classes C0 = X[y==0] C1 = X[y==1] #display one image from class 0, C0. For example the 5th imFromC0 = C0[5] #display it. first scince the imave is vectorized, we to reshape it to 2D imFromC02D = imFromC0.reshape(64,64) plt.imshow(imFromC02D, cmap='gray') #now, merge class 0 set and class 1 set in one x2 X2 = np.concatenate((C0,C1)) print(X2.shape) #you should get 20 rows. because you have two classes, 10 faces #in each one #also create a vector of 20 labels, 10 labels 0 (first class) and 10 labels for # (2nd class) zeros = np.zeros(10) #tan zeros ones = np.ones(10) #ten ones y2 = np.concatenate((zeros, ones)) #concatenate ten in one single vector y2 #in machine learning, we always split randomly the dataset into training set #to learn the classifier # and test to test it. #first import the module train_test_split used to split the dataset from sklearn.model_selection import train_test_split #split into 70% train and 30% test Xtrain, Xtest, ytrain, ytest = train_test_split(X2, y2, test_size=0.3) print("Xtrain", Xtrain) print("Length of Xtrain:",len(Xtrain)) print("Xtest",Xtest) print("Length of Xttest:",len(Xtest)) print("ytrain", ytrain) print("Length of ytrain:",len(ytrain)) print("ytest",ytest) print("Length of yttest:",len(ytest)) # part2 percepton :training and test #this module is to compute the accurcy of classification from sklearn.metrics import accuracy_score #definition of perception def perceptron_train(x,y,z,eta,t): ''' input parameters: x:data set of input features y: actual output z:activation function threshold eta:learning __PRAGMA_REDEFINE_EXTNAME t: number of iteration ''' #initilizing the weight w = np.zeros(len(x[0])) n = 0 #initilazing additional parameters to compute sum-of-squares errors yhat_vec = np.ones(len(y)) #vector for predictions errors = np.ones(len(y)) #vector for errors (actual - predictions) J = [] #vector for the SSE cost function while n < t:="" for="" i="" in="" range(0,="" len(x)):="" #dot="" product="" f="np.dot(x[i]," w)="" #activation="" function="" if="" f="">= z: yhat = 1. else: yhat = 0. yhat_vec[i] = yhat # updating the weights for j in range(0, len (w)): w[j] = w[j] + eta*(y[i]*yhat)*x[i][j] n += 1 # computing the sum-of-squared errors for i in range(0,len(y)): errors[i] = (y[i]-yhat_vec[i])**2 J.append(0.5*np.sum(errors)) if n % 10==0: print('Iteration: ', n, 'Error:',J) return w, J #test perception given a single test sample def perceptron_test_one_sample(x,y,z): f = np.dot(x,w) if f > z: yhat = 1 else: yhat = 0 y_pred = yhat return y_pred #test preception given set of test sampled def perceptron_test(x, w, z, eta, t): y_pred = [] for i in range(0, len(x-1)): f = np.dot(x[i], w) #activation function if f > z: yhat = 1 else: yhat = 0 y_pred.append(yhat) return y_pred #Train the percepton #set the parameters of the perceptron z = 0.0 # activtion function threshold eta = 0.1 #learning rate t = 30 # number of iteration #training phase using Xtrain set w = perceptron_train(Xtrain, ytrain, z, eta, t)[0] # training phase using test set #a. test perceptron using one samples #load one face image from test set testImg = Xtest[4] testImg2D = testImg.reshape(64,64) plt.imshow(testImg2D, cmap='gray') y_pred = perceptron_test_one_sample(testImg, w, z) print(y_pred) #b. test perceptron using all the stst set y_pred = perceptron_test(Xtest, w, z, eta, t) #compute the accuracy. the percentage of the correct classied face print("the accuracy score is:") print(accuracy_score(ytest, y_pred))
Answered Same DayApr 05, 2021

Answer To: # -*- coding: utf-8 -*- # import the main module in python numpy (numerical python) import numpy as...

Shivinder answered on Apr 06 2021
148 Votes

#Import packages
import numpy as np
import pandas as pd
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklear
n.model_selection import train_test_split
#Read the dataset
faces_image = np.load('olivettifaces-jecop3ls.npy')
faces_target = np.load('olivettifacestarget-3nehwzba.npy')
#Let's have a look at the shape of the dataset
print('Image dataset shape', faces_image.shape)
print('Encoded names shape', faces_target.shape)
#Transform the 2D face image into vectors of 4096 (64x64) pixels
faces_data = faces_image.reshape(faces_image.shape[0], faces_image.shape[1] * faces_image.shape[2])
print('Feature Vector Shape',faces_data.shape)
#split into 70% train and 30% test
X_train, X_test, y_train, y_test = train_test_split(faces_data, faces_target, test_size=0.3, random_state = 19)
print('Training Feature Shape', X_train.shape)
print('Training Label Shape', y_train.shape)
print('Test Feature Shape',X_test.shape)
print('Test Label Shape',y_test.shape)
#Apply KNN
knn = KNeighborsClassifier() #Default
knn.fit(X_train, y_train)
#Make the prediction on the KNN model and check for accuracy
y_pred_knn = knn.predict(X_test)
acc_knn = round(accuracy_score(y_test, y_pred_knn)*100,2)
print('KNN model accuracy is:',acc_knn)
#Apply SVM
svm_mod = SVC(kernel = 'linear') #Taking the linear kernel
svm_mod.fit(X_train, y_train)
#Make the prediction on the KNN model and check for accuracy
y_pred_svm = svm_mod.predict(X_test)
acc_svm = round(accuracy_score(y_test, y_pred_svm)*100,2)
print('SVM model accuracy is:',acc_svm)
n_neighbors = [1,3,5,7] #Neighbor values
for neighbor in n_neighbors:

knn = KNeighborsClassifier(n_neighbors = neighbor)
knn.fit(X_train, y_train)
y_pred_knn = knn.predict(X_test)
acc = round(accuracy_score(y_test, y_pred_knn)*100,2)
print('KNN model accuracy with neighbor {} is:{}'.format(neighbor, acc))
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here