-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
189 lines (123 loc) · 3.89 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Author: Ian Russell
################################################
################ Dependencies ##################
import os
import scipy.io
import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
import time
# Custom imports, make sure in working directory
from PCA import PCA
from LDA import LDA
################################################
################## Settings ####################
# Data, make sure in working directory
file = "PIE.mat"
# Dimensions
d1,d2 = 100, 100
# KNN Params.
n = 3 # -> Value is parameter for KNN
trainNums = [5,10,15]
# Eigen_Faces
Eigen_Faces = True # Change to True to see plot output
#########################################################
################## Utility Functions ####################
def load_data(file):
data = scipy.io.loadmat(file)
data, labels = data["Data"], data["Label"]
return data, labels
def score(finaldf):
Guesses = list(finaldf["Predicted"])
Truths = list(finaldf["Actual"])
i = 0
Answers = []
for element in Guesses:
if element == Truths[i]:
Answers.append("Correct")
i += 1
hit_rate = len(Answers)/len(Truths)
return hit_rate
def pca(data, labels, d1, n, trainNum):
pca = PCA(data, d1)
reduced = pca.reduce()
# Visualize with first five eigen_vectors
#model.eigface(1)
# Get transformed data
results = reduced[0]
values = reduced[0]
results = pd.DataFrame(results)
results["Label"] = labels
X_train, X_test, y_train, y_test = train_test_split(values, results["Label"], test_size=trainNum, random_state=42)
neigbors = KNeighborsClassifier(n_neighbors=n)
neigbors.fit(X_train, y_train)
predictions = neigbors.predict(X_test)
final = pd.DataFrame()
final["Predicted"] = predictions
actual = pd.Series(y_test).reset_index().drop(columns="index")
final["Actual"] = actual
return final
def lda(data, labels, d1, d2, n, trainNum):
# Intialize PCA
model = PCA(data, d1)
reduced = model.reduce()
# Get transformed data
transformed = reduced[0]
transformed = pd.DataFrame(transformed)
transformed["Label"] = labels
# Perform LDA
lda = LDA(transformed, d2)
# Create DataFrame
values = pd.DataFrame(lda.disc().T)
results = values
results["Label"] = labels
# Classifier
X_train, X_test, y_train, y_test = train_test_split(values, results["Label"], test_size=trainNum, random_state=42)
neigbors = KNeighborsClassifier(n_neighbors=n)
neigbors.fit(X_train, y_train)
predictions = neigbors.predict(X_test)
final = pd.DataFrame()
final["Predicted"] = predictions
actual = pd.Series(y_test).reset_index().drop(columns="index")
final["Actual"] = actual
return final
def eigen_faces(data, d1, numFaces):
model = PCA(data, d1)
model.eigface(numFaces)
##############################################
################## OUTPUT ####################
data, labels = load_data("PIE.mat")
index = [trainNum for trainNum in trainNums]
pcaScores = []
pcaTimes = []
ldaScores =[]
ldaTimes =[]
print()
print("Calculating PCA...")
print()
for trainNum in trainNums:
start_time = time.time()
# Run classifier
results = pca(data,labels, d1, n, 1-trainNum/21)
pcaScores.append(str(round(score(results)*100, 1)) + "%")
pcaTimes.append("%ss" % (round(time.time() - start_time, 2)))
print("Calculating LDA...")
for trainNum in trainNums:
start_time = time.time()
# Run classifier
results = lda(data, labels, d1, d1, n, 1-trainNum/21)
ldaScores.append(str(round(score(results)*100, 1)) + "%")
ldaTimes.append("%ss" % (round(time.time() - start_time, 2)))
tabular = pd.DataFrame(columns=["trainNum", "PCA_Score","LDA_Score", "PCA_Time", "LDA_Time"])
tabular["trainNum"] = index
tabular["PCA_Score"] = pcaScores
tabular["LDA_Score"] = ldaScores
tabular["PCA_Time"] = pcaTimes
tabular["LDA_Time"] = ldaTimes
print()
print(tabular)
print()
if Eigen_Faces:
eigen_faces(data, d1, 5)