-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsvm.py
More file actions
65 lines (46 loc) · 2.03 KB
/
Copy pathsvm.py
File metadata and controls
65 lines (46 loc) · 2.03 KB
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
# Saves pickle files for SVM models for computing precision and recall
from sklearn.svm import SVC
import numpy as np
import pickle
from sklearn.metrics import average_precision_score, accuracy_score
from sklearn.model_selection import GridSearchCV
def make_data_label_files(arr):
labels = []
data = []
for i in range(len(arr)):
for j in range(len(arr[i])):
labels.append(i)
data.append(arr[i][j].reshape(-1))
return data, labels
if (__name__ == '__main__'):
#method = 'spectro'
method = 'mfcc'
noise = True
if (noise):
noise_str = '_noise'
else:
noise_str = ''
with open (method + '_training' + noise_str + '.txt', 'rb') as fp:
spectro_training = pickle.load(fp)
spectro_features_train, spectro_labels_train = make_data_label_files(spectro_training)
with open (method + '_validation' + noise_str + '.txt', 'rb') as fp:
spectro_testing = pickle.load(fp)
spectro_features_test, spectro_labels_test = make_data_label_files(spectro_testing)
model_spectro = SVC(C=0.5, kernel='poly',gamma='auto')
# cs = [0.001, 0.01, 0.1, 1, 10]
# gammas = [0.001, 0.01, 0.1, 1, 10]
# params = {'C':cs,'gamma':gammas}
# grid_search = GridSearchCV(SVC(), params)
spectro_features_train = np.nan_to_num(spectro_features_train)
model_spectro.fit(spectro_features_train, spectro_labels_train)
# grid_search.fit(spectro_features_test, spectro_labels_test)
# print (grid_search.best_params)
# clf_spectro.fit(spectro_features_train, spectro_labels_train)
# pred_spectro = clf_spectro.predict(spectro_features_test)
# print (accuracy_score(pred_spectro, spectro_labels_test))
with open(method + noise_str + '_svm.pkl','wb') as f:
pickle.dump(model_spectro, f)
#model = grid_search.best_estimator_
spectro_features_test = np.nan_to_num(spectro_features_test)
pred_spectro = model_spectro.predict(spectro_features_test)
print (accuracy_score(pred_spectro, spectro_labels_test))