aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtrain.py29
1 files changed, 19 insertions, 10 deletions
diff --git a/train.py b/train.py
index fd03220..7d82d73 100755
--- a/train.py
+++ b/train.py
@@ -15,6 +15,7 @@ from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
+from sklearn.utils import check_array
import argparse
import numpy as np
@@ -29,7 +30,7 @@ def normalise_faces(average_face, faces):
# Split data into training and testing sets
def test_split(n_faces, raw_faces, split, seed):
random.seed(seed)
- n_cases = 52
+ n_cases = 10
n_pixels = 2576
raw_faces_split = np.split(raw_faces,n_cases)
@@ -72,9 +73,9 @@ args = parser.parse_args()
M = args.eigen
raw_faces = genfromtxt(args.data, delimiter=',')
-targets = np.repeat(np.arange(10),52)
+targets = np.repeat(np.arange(52),10)
-n_faces = 10
+n_faces = 52
faces_train, faces_test, target_train, target_test = test_split(n_faces, raw_faces, args.split, args.seed)
@@ -89,7 +90,8 @@ explained_variances = ()
if args.pca or args.pca_r:
# faces_pca containcts the principial components or the M most variant eigenvectors
average_face = np.mean(faces_train, axis=0)
- deviations = np.std(faces_train, axis=0)
+ deviations_tr = np.std(faces_train, axis=0)
+ deviations_tst = np.std(faces_train, axis=0)
faces_train = normalise_faces(average_face, faces_train)
faces_test = normalise_faces(average_face, faces_test)
if (args.pca_r):
@@ -105,15 +107,16 @@ if args.pca or args.pca_r:
e_vals = np.flip(e_vals)[:M]
e_vecs = np.fliplr(e_vecs).T[:M]
- deviations = np.flip(deviations)
+ deviations_tr = np.flip(deviations_tr)
+ deviations_tst = np.flip(deviations_tst)
faces_train = np.dot(faces_train, e_vecs.T)
faces_test = np.dot(faces_test, e_vecs.T)
if (args.reconstruct):
- rec_vec = np.add(average_face, np.dot(faces_train[args.reconstruct], e_vecs) * deviations)
+ rec_vec = np.add(average_face, np.dot(faces_train[args.reconstruct], e_vecs) * deviations_tr)
+ rec_faces_test = np.add(average_face, np.dot(faces_test, e_vecs) * deviations_tst)
rec_error = LA.norm(np.subtract(raw_faces_train[args.reconstruct], rec_vec))
- print(rec_error)
ar = plt.subplot(2, 1, 1)
ar.imshow(rec_vec.reshape([46,56]).T, cmap = 'gist_gray')
ar = plt.subplot(2, 1, 2)
@@ -146,14 +149,20 @@ if args.principal:
if args.grapheigen:
# Colors for distinct individuals
- cols = ['#{:06x}'.format(randint(0, 0xffffff)) for i in range(10)]
+ cols = ['#{:06x}'.format(randint(0, 0xffffff)) for i in range(52)]
pltCol = [cols[int(k)] for k in target_train]
plt.scatter(faces_train[:, 0], faces_train[:, 1], color=pltCol)
plt.show()
classifier = KNeighborsClassifier(n_neighbors=args.neighbors)
-classifier.fit(faces_train, target_train)
-target_pred = classifier.predict(faces_test)
+if (args.reconstruct):
+ classifier.fit(raw_faces_train, target_train)
+ target_pred = classifier.predict(rec_faces_test)
+ #Better Passing n_neighbors = 1
+else:
+ classifier.fit(faces_train, target_train)
+ target_pred = classifier.predict(faces_test)
+ #Better n_neighbors = 2
cm = confusion_matrix(target_test, target_pred)
print(cm)