-
Notifications
You must be signed in to change notification settings - Fork 1
/
svm.py
93 lines (67 loc) · 3.04 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 1 21:36:33 2020
@author: Maria
"""
# Support Vector Regression
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score, classification_report#, multilabel_confusion_matrix
from sklearn.model_selection import GridSearchCV
import numpy as np
###############################################
# SVM #
###############################################
class svm:
def __init__(self, X_train, y_train, X_test, y_true):
self.X_train = X_train
self.y_train = y_train
self.y_true = y_true
model = LinearSVC(multi_class = 'crammer_singer',
loss = 'squared_hinge',
C = 0.1,
max_iter = 12000
)
model.fit(X_train, y_train)
# Prediction and Error
self.predictions = model.predict(X_test)
self.accuracy_score = accuracy_score(y_true, self.predictions)
# self.classification_report = classification_report(y_true, self.predictions,
# output_dict = True)
#self.multilabel_confusion_matrix = multilabel_confusion_matrix(y_true, self.predictions)
def getPredictions(self):
# print("\n Predictions:", self.predictions)
return self.predictions
def getAccuracy(self):
# print("\n Accuracy Score:", self.accuracy_score)
return self.accuracy_score
# def getClassificationReport(self):
# print("\n Classification report")
# for x,y in self.classification_report.items():
# print(x)
# for a, b in y.items():
# print("\t", a, " - ", b)
# return self.classification_report
# def getMultilabelCM(self):
# print(self.multilabel_confusion_matrix)
# return self.multilabel_confusion_matrix
###############################################
# VISUALISATION #
###############################################
def svm_graph(self):
plt.scatter(self.y_true, self.predictions, c='#83A89A')
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.plot(np.unique(self.y_true), np.poly1d(np.polyfit(self.y_true, self.predictions, 1))(np.unique(self.y_true)))
plt.show()
def grid_search(self):
param_grid = { 'C': [1, 100, 0.1],
'max_iter': [500, 1000],
'loss': ['squared_hinge', 'hinge'],
'multi_class': ['ovr', 'crammer_singer']}
grid = GridSearchCV(LinearSVC(), param_grid, refit = True, verbose = 3)
# fitting the model for grid search
grid.fit(self.X_train, self.y_train)
print("\nBest params:", grid.best_params_)
print("\nBest score:", grid.best_score_)
return grid.best_params_, grid.best_estimator_, grid.best_score_