-
Notifications
You must be signed in to change notification settings - Fork 52
/
knnClassifierPCA.py
152 lines (114 loc) · 4.86 KB
/
knnClassifierPCA.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
import numpy
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
def knn_classifier_pca():
file_x = 'data/features_raw.dat'
file_y = 'data/label_class_0.dat'
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
# Split the data into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# PCA to select features
pca = PCA(n_components=20, svd_solver='full')
pca.fit(X)
X = pca.transform(X)
# KNN classsifier
clf = KNeighborsClassifier(n_neighbors=9)
trained_model=clf.fit(X_train,y_train)
trained_model.fit(X_train,y_train )
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
cm = confusion_matrix(y_test, y_predict)
print(cm)
print("Accuracy score of valence test KNN-PCA")
print(accuracy_score(y_test, y_predict)*100)
########################################################################
file_x = 'data/features_raw.dat'
file_y = 'data/label_class_1.dat'
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
# Split the data into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# PCA to select features
pca = PCA(n_components=20, svd_solver='full')
pca.fit(X)
X = pca.transform(X)
# KNN classsifier
clf = KNeighborsClassifier(n_neighbors=9)
trained_model=clf.fit(X_train,y_train)
trained_model.fit(X_train,y_train )
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
cm = confusion_matrix(y_test, y_predict)
print(cm)
print("Accuracy score of Arousal test KNN-PCA")
print(accuracy_score(y_test, y_predict)*100)
##########################################################################33
file_x = 'data/features_raw.dat'
file_y = 'data/label_class_2.dat'
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
# Split the data into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# PCA to select features
pca = PCA(n_components=20, svd_solver='full')
pca.fit(X)
X = pca.transform(X)
#explained_variance=pca.explained_variance_ratio_
# KNN classsifier
clf = KNeighborsClassifier(n_neighbors=9)
trained_model=clf.fit(X_train,y_train)
trained_model.fit(X_train,y_train )
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
cm = confusion_matrix(y_test, y_predict)
print(cm)
print("Accuracy score of Dominance test KNN-PCA")
print(accuracy_score(y_test, y_predict)*100)
######################################################################
file_x = 'data/features_raw.dat'
file_y = 'data/label_class_3.dat'
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
# Split the data into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# PCA to select features
pca = PCA(n_components=20, svd_solver='full')
pca.fit(X)
X = pca.transform(X)
#explained_variance=pca.explained_variance_ratio_
# KNN classsifier
clf = KNeighborsClassifier(n_neighbors=9)
trained_model=clf.fit(X_train,y_train)
trained_model.fit(X_train,y_train )
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
cm = confusion_matrix(y_test, y_predict)
print(cm)
print("Accuracy score of Liking test KNN-PCA")
print(accuracy_score(y_test, y_predict)*100)
if __name__ == '__main__':
knn_classifier_pca()