-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm.py
38 lines (32 loc) · 1.27 KB
/
svm.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
import numpy as np
import utils as u
import sys
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
def run(dataset):
np.random.shuffle(dataset)
classes = dataset[:, -1]
features = dataset[:, :54]
features = u.tf_idf(features)
svm("linear", 3, features, classes) #3 default degree value in SVC, it's ignored in linear and rbf
svm("poly", 2, features, classes)
svm("rbf", 3, features, classes)
features = normalize(features)
svm("linear", 3, features, classes)
svm("poly", 2, features, classes)
svm("rbf", 3, features, classes)
def view(result, kernel):
print("***********")
print("Kernel: "+kernel)
print("Minimum accuracy: "+str(result.min()))
print("Maximum accuracy: "+str(result.max()))
print("Accuracy mean: "+str(result.mean()))
print("Accuracy variance: "+str(result.var()))
print("***********")
def svm(kernel, degree, features, classes):
classifier = SVC(kernel=kernel, degree=degree)
result = cross_val_score(classifier, features, classes, cv=10, n_jobs=-1)
view(result, classifier.kernel)
def normalize(features):
norms = np.sqrt((np.power(features+sys.float_info.epsilon,2)).sum(axis=1, keepdims=True))
return np.where(norms > 0.0, features / norms, 0.)