|
a |
|
b/binary_classification/svm.py |
|
|
1 |
from sklearn import svm |
|
|
2 |
from sklearn.metrics import roc_auc_score |
|
|
3 |
from util import roc_results, results |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
def svm_training(x_train, y_train): |
|
|
7 |
""" |
|
|
8 |
:param x_train: the x-values we want to train on (2D numpy array) |
|
|
9 |
:param y_train: the y-values that correspond to x_train (1D numpy array) |
|
|
10 |
:return: sklearn SVM Classifier object that can now be used for predictions |
|
|
11 |
""" |
|
|
12 |
clf = svm.SVC(kernel='sigmoid', C=87, gamma=0.0212, random_state=0) |
|
|
13 |
clf.fit(x_train, y_train) |
|
|
14 |
return clf |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
def svm_classification(clf, x_test): |
|
|
18 |
""" |
|
|
19 |
:param clf: trained sklearn SVM Classifier object |
|
|
20 |
:param x_test: the x-values we want to get predictions on (2D numpy array) |
|
|
21 |
:return: a 1D numpy array containing the predictions |
|
|
22 |
""" |
|
|
23 |
return clf.predict(x_test) |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
def svm_pipeline(x_train, y_train, x_test, y_test): |
|
|
27 |
""" |
|
|
28 |
:param x_train: the x-values we want to train on (2D numpy array) |
|
|
29 |
:param y_train: the y-values that correspond to x_train (1D numpy array) |
|
|
30 |
:param x_test: the x-values we want to test on (2D numpy array) |
|
|
31 |
:param y_test: the y-values that correspond to x_test (1D numpy array) |
|
|
32 |
:return: the roc auc score |
|
|
33 |
""" |
|
|
34 |
clf = svm_training(x_train, y_train) |
|
|
35 |
y_pred = svm_classification(clf, x_test) |
|
|
36 |
roc_results(y_pred, y_test, 'SVM') |
|
|
37 |
return roc_auc_score(y_test, y_pred), results(y_pred, y_test) |