-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVM.py
More file actions
55 lines (30 loc) · 1.35 KB
/
SVM.py
File metadata and controls
55 lines (30 loc) · 1.35 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
import numpy as np
import matplotlib.pyplot as plt
class SVM:
def train(self, train, label, epoch, alpha, verbose):
feature_size = len(train[0])
data_size = len(train)
lambda_v = 1/epoch
self.weight = np.ones(feature_size)
for e in range(epoch):
if verbose==1:
sum = 0
for i in range(feature_size):
sum += max(0,1-label[i]*(np.dot(train[i],self.weight))) #max(0,y<x,w>)
loss = lambda_v*pow(np.linalg.norm(self.weight),2)/2 + sum
print("Epoch {}/{}: Loss is {}".format(e+1,epoch,loss))
for i in range(data_size):
p = label[i] * (np.dot(train[i],self.weight)) # y<x,w>
if p >= 0.5: #prediction is correct
self.weight = self.weight - alpha*(2*lambda_v*self.weight)
else:
self.weight = self.weight + alpha*(label[i]*train[i] - 2*lambda_v*self.weight)
def predict(self,test,target):
res = []
for t in test:
res.append(np.dot(t,self.weight))
count = 0
for i in range(len(target)):
if (target[i] * res[i] > 0):
count += 1
return res,count/len(target)