-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathsvmClassifierPCA.py
173 lines (130 loc) · 5.41 KB
/
svmClassifierPCA.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
import numpy
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import pandas as pd
def svm_classifier_pca():
file_x = 'data/features_raw.dat'
file_y = 'data/label_class_0.dat'
print("LABEL 0 - Valence \n ")
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
print("Split the data into training/testing sets \n")
# Split the data into training/testing sets
X_train, X_test, y_train, y_test_0 = train_test_split(X, y, test_size=0.33, random_state=42)
print("Feature Scaling \n")
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print("Applying PCA to select features \n")
# 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_
print("Applying SVM classifier \n")
# SVM Classifier
clf = SVC()
clf.fit(X_train, y_train)
y_predict_0 = clf.predict(X_test)
cm = confusion_matrix(y_test_0, y_predict_0)
print(cm)
print("Accuracy score of Valence SVM-PCA")
print(accuracy_score(y_test_0, y_predict_0)*100)
#######################################################################
file_x = 'data/features_raw.dat'
file_y = 'data/label_class_1.dat'
print("LABEL 1 - Arousal \n ")
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
print("Split the data into training/testing sets \n")
# Split the data into training/testing sets
X_train, X_test, y_train, y_test_0 = train_test_split(X, y, test_size=0.33, random_state=42)
print("Feature Scaling \n")
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print("Applying PCA to select features \n")
# 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_
print("Applying SVM classifier \n")
# SVM Classifier
clf = SVC()
clf.fit(X_train, y_train)
y_predict_0 = clf.predict(X_test)
cm = confusion_matrix(y_test_0, y_predict_0)
print(cm)
print("Accuracy score of Arousal SVM-PCA")
print(accuracy_score(y_test_0, y_predict_0)*100)
#######################################################################
file_x = 'data/features_raw.dat'
file_y = 'data/label_class_2.dat'
print("LABEL 2 - Dominance \n ")
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
print("Split the data into training/testing sets \n")
# Split the data into training/testing sets
X_train, X_test, y_train, y_test_0 = train_test_split(X, y, test_size=0.33, random_state=42)
print("Feature Scaling \n")
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print("Applying PCA to select features \n")
# 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_
print("Applying SVM classifier \n")
# SVM Classifier
clf = SVC()
clf.fit(X_train, y_train)
y_predict_0 = clf.predict(X_test)
cm = confusion_matrix(y_test_0, y_predict_0)
print(cm)
print("Accuracy score of Dominance SVM-PCA")
print(accuracy_score(y_test_0, y_predict_0)*100)
#######################################################################
file_x = 'data/features_raw.dat'
file_y = 'data/label_class_0.dat'
print("LABEL 3 - Liking \n ")
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
print("Split the data into training/testing sets \n")
# Split the data into training/testing sets
X_train, X_test, y_train, y_test_0 = train_test_split(X, y, test_size=0.33, random_state=42)
print("Feature Scaling \n")
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print("Applying PCA to select features \n")
# 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_
print("Applying SVM classifier \n")
# SVM Classifier
clf = SVC()
clf.fit(X_train, y_train)
y_predict_0 = clf.predict(X_test)
cm = confusion_matrix(y_test_0, y_predict_0)
print(cm)
print("Accuracy score of Liking SVM-PCA")
print(accuracy_score(y_test_0, y_predict_0)*100)
if __name__ == '__main__':
svm_classifier_pca()