-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_svm_standalone.py
49 lines (32 loc) · 1.29 KB
/
train_svm_standalone.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
from sklearn.svm import SVC
import numpy as np
import gc
import pickle
import h5py
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import confusion_matrix
import joblib
from utils import load_from_npy
X_Train, Y_Train, X_Test, Y_Test = load_from_npy()
# already flat
X_Train = X_Train.flatten().reshape(X_Train.shape[0], X_Train.shape[1]*X_Train.shape[2]*X_Train.shape[3])
svc = SVC(kernel='rbf', gamma='scale', verbose=True, max_iter=500000000)
#svc = SGDClassifier(learning_rate='constant', eta0=0.001, shuffle=True, verbose=True)
svc.fit(X_Train, Y_Train)
del X_Train, Y_Train
testshape = X_Test.shape
X_Test = X_Test.flatten().reshape(testshape[0], testshape[1]*testshape[2]*testshape[3])
# save the model to disk
filename = "models/svmst.sav"
joblib.dump(svc, filename)
# load the model from disk
loaded_model = joblib.load(filename)
result = loaded_model.score(X_Test, Y_Test)
print(result)
tn, fp, fn, tp = confusion_matrix(y_true=Y_Test, y_pred=svc.predict(X_Test)).ravel()
#print(clf.score(x_test, y_test))
#tn, fp, fn, tp = confusion_matrix(y_true=y_test, y_pred=clf.predict(x_test)).ravel()
print(f'training set: true negatives: {tn}')
print(f'training set: true positives: {tp}')
print(f'training set: false negatives: {fn}')
print(f'training set: false positives: {fp}')