Repository navigation
Expand file tree
/
Copy pathSVM.py
More file actions
167 lines (130 loc) · 4.64 KB
/
Copy pathSVM.py
File metadata and controls
167 lines (130 loc) · 4.64 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
import cvxopt
import pandas as pd
from sklearn.datasets.samples_generator import make_blobs
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
from sklearn.svm import LinearSVC
from sklearn.metrics import confusion_matrix
from tqdm import tqdm
import pickle
class SVM:
def fit(self, X, y):
n_samples, n_features = X.shape
# P = X^T X
K = np.zeros((n_samples, n_samples))
for i in tqdm(range(n_samples)):
for j in tqdm(range(n_samples)):
K[i, j] = np.dot(X[i], X[j])
P = cvxopt.matrix(np.outer(y, y) * K)
# q = -1 (1xN)
q = cvxopt.matrix(np.ones(n_samples) * -1)
# A = y^T
A = cvxopt.matrix(y, (1, n_samples))
# b = 0
b = cvxopt.matrix(0.0)
# -1 (NxN)
G = cvxopt.matrix(np.diag(np.ones(n_samples) * -1))
# 0 (1xN)
h = cvxopt.matrix(np.zeros(n_samples))
# line below doesn't work, use main2 for now.
solution = cvxopt.solvers.qp(P, q, G, h, A, b)
# Lagrange multipliers
a = np.ravel(solution["x"])
# Lagrange have non zero lagrange multipliers
sv = a > 1e-5
ind = np.arange(len(a))[sv]
self.a = a[sv]
self.sv = X[sv]
self.sv_y = y[sv]
# Intercept
self.b = 0
for n in range(len(self.a)):
self.b += self.sv_y[n]
self.b -= np.sum(self.a * self.sv_y * K[ind[n], sv])
self.b /= len(self.a)
# Weights
self.w = np.zeros(n_features)
for n in range(len(self.a)):
self.w += self.a[n] * self.sv_y[n] * self.sv[n]
def project(self, X):
return np.dot(X, self.w) + self.b
def predict(self, X):
return np.sign(self.project(X))
def main1():
df = pd.read_csv(r"./kucoin_eth-usdt.csv")
Y = df["movement"].values[:100]
df = df.drop("movement", axis=1)
X = df.values[:100]
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size=0.33, random_state=42
)
svm = SVM()
svm.fit(X_train, y_train)
svc = LinearSVC()
svc.fit(X_train, y_train)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap="winter")
ax = plt.gca()
xlim = ax.get_xlim()
w = svc.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(xlim[0], xlim[1])
yy = a * xx - svc.intercept_[0] / w[1]
plt.plot(xx, yy)
yy = a * xx - (svc.intercept_[0] - 1) / w[1]
plt.plot(xx, yy, "k--")
yy = a * xx - (svc.intercept_[0] + 1) / w[1]
plt.plot(xx, yy, "k--")
y_pred = svc.predict(X_test)
confusion_matrix(y_test, y_pred)
from sklearn import svm
from sklearn import metrics
training = True
# if you want to try prediction only on saved model, change training to False
def main2(df, fn):
# Training the model for pickle
if training:
# Create a svm Classifier
clf = svm.SVC(
kernel="linear", verbose=True, max_iter=9999999
) # Linear Kernel #99999999
# df = pd.read_csv(r'./kucoin_eth-usdt.csv')
Y = df["movement"].values # [:100]
df = df.drop("movement", axis=1)
X = df.values # [:100]
print(X)
print(Y)
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size=0.3, random_state=42
)
print("Train Test Split Complete")
# Train the model using the training sets
clf.fit(X_train, y_train)
print("Fitting Complete")
# Predict the response for test dataset
y_pred = clf.predict(X_test)
print("Prediction Complete")
print(list(y_pred))
print(list(y_test))
# [print(a, b) for a, b in zip(y_pred, y_test) if a!=b]
accuracy = metrics.accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Model Precision: what percentage of positive tuples are labeled as such?
precision = metrics.precision_score(y_test, y_pred)
print("Precision:", precision)
# Model Recall: what percentage of positive tuples are labelled as such?
recall = metrics.recall_score(y_test, y_pred)
print("Recall:", recall)
filename = "./SVM_Models_1hr_levels/SVM_model_" + fn + ".sav"
pickle.dump(clf, open(filename, "wb"))
return accuracy, precision, recall, list(y_pred), list(y_test), filename
else:
# Putting in parameters on already trained model
filename = "./SVM_Models/SVM_model.sav"
clf = pickle.load(open(filename, "rb"))
y_pred = clf.predict(X_test)
print(y_pred)
# print("\nMain 1 Running: \n")
# main1()
# print("\nMain 2 Running: \n")
# main2()