-
Notifications
You must be signed in to change notification settings - Fork 2
/
svm_multiclass.py
47 lines (35 loc) · 1.81 KB
/
svm_multiclass.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 import datasets
from sklearn.metrics import confusion_matrix
from sklearn.metrics import recall_score
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn import metrics
from sklearn.metrics import average_precision_score
from sklearn.metrics import classification_report
import numpy as np
import warnings
accuracy = 0
def svm(X_train, X_test, y_train, y_test, kernelSVM, feature):
warnings.filterwarnings('always')
svm_model = SVC(kernel = kernelSVM).fit(X_train, y_train)
y_predict = svm_model.predict(X_test)
# creating a confusion matrix
# cm = confusion_matrix(y_test, y_predict)
print("\nSVM Classification using", feature, "feature and", kernelSVM, "kernel.\n")
filename = "SVM-" + feature + "_" + kernelSVM
print("\nConfusion Matrix SVM Classification using", feature, "feature and", kernelSVM, "kernel.")
confusionmatrix = confusion_matrix(y_test, y_predict)
print(confusionmatrix)
np.savetxt(filename + '_confusion_matrix.csv', confusionmatrix.astype(int), fmt='%i', delimiter=" ")
print("\nClassification Report SVM Classification using", feature, "feature and", kernelSVM, "kernel.")
classificationreport = classification_report(y_test, y_predict)
print(classificationreport)
# Model Accuracy: how often is the classifier correct?
print("Accuracy:", svm_model.score(X_test, y_test))
# Model Precision: what percentage of positive tuples are labeled as such?
# Model Recall: what percentage of positive tuples are labelled as such?
report_file = open(filename + '_classification_report.csv', "w")
report_file.write(
classificationreport + "\n\n" +
"Accuracy: " + str(svm_model.score(X_test, y_test))
)