-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_3.py
More file actions
38 lines (25 loc) · 1.04 KB
/
Copy pathquestion_3.py
File metadata and controls
38 lines (25 loc) · 1.04 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
# must run svm.py before this on the output of question 1 or 2.
# It will save pickle files for SVM models.
from sklearn.svm import SVC
import numpy as np
import pickle
from sklearn.metrics import precision_score, recall_score
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
model_path = './spectro_svm_poly_05.pkl'
test_file_path = './spectro_validation.txt'
with open(model_path,'rb') as f:
model = pickle.load(f)
with open (test_file_path, 'rb') as fp:
spectro_testing = pickle.load(fp)
spectro_features_test, spectro_labels_test = make_data_label_files(spectro_testing)
spectro_features_test = np.nan_to_num(spectro_features_test)
pred_spectro = model.predict(spectro_features_test)
print (precision_score(pred_spectro, spectro_labels_test, average='weighted'))
print (recall_score(pred_spectro, spectro_labels_test, average='weighted'))