-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_svm_5fold.py
35 lines (27 loc) · 990 Bytes
/
train_svm_5fold.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
# Cross validation 5 folds SVM evaluation
# Compare this snippet from train_svm.py:
from sklearn.svm import SVC
import numpy as np
import pandas as pd
from sklearn.model_selection import cross_val_score
# load data hasil ekstraksi fitur fft
X = pd.read_csv("data/feature_VBL-VA001.csv", header=None)
# load label
y = pd.read_csv("data/label_VBL-VA001.csv", header=None)
# make 1D array to avoid warning
y = pd.Series.ravel(y)
# Setup arrays to store training and test accuracies
c_svm = np.arange(1, 100)
test_accuracy = np.empty(len(c_svm))
# finding best c for five folds
for i, k in enumerate(c_svm):
# Setup a knn classifier with c_svm
clf_svm = SVC(C=k)
# Do 5-cv to the model
scores = cross_val_score(clf_svm, X, y, cv=5)
print(scores)
# Compute accuracy on the test set
test_accuracy[i] = np.mean(scores)
# print max test accuracy (average of 5 folds)
print(f"Max test acc: {np.max(test_accuracy)}")
print(f"Best C: {np.argmax(test_accuracy)+1}")