-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (63 loc) · 2.44 KB
/
main.py
File metadata and controls
81 lines (63 loc) · 2.44 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
import scipy.optimize as opt
import numpy, random, math
import matplotlib.pyplot as plt
def objective(alpha):
result = numpy.dot(alpha, Pmatrix)
return 0.5 * numpy.dot(result, alpha) - numpy.sum(alpha)
sigma = 1
def kernel_function(x1, x2):
dot = numpy.dot(x1, x2)
res = pow((dot + 1), 2)
exp = math.exp(- pow(numpy.linalg.norm(x1-x2), 2)/(2 * pow(sigma, 2)))
return res
def zerofun(vector):
return numpy.dot(vector, targets)
# generating test data
numpy.random.seed(100)
classA = numpy.concatenate((numpy.random.randn(100, 2)*3.2 + [1.5, 0.5],
numpy.random.randn(10, 2)*3.2 + [-1.5, 0.5]))
classB = numpy.random.randn(200, 2) * 2.2 + [0.0, -0.5]
inputs = numpy.concatenate((classA, classB))
targets = numpy.concatenate((numpy.ones(classA.shape[0]), -numpy.ones(classB.shape[0])))
N = inputs.shape[0] # Number of rows (samples)
permute = list(range(N))
random.shuffle(permute)
inputs = inputs[permute, :]
targets = targets[permute]
Pmatrix = numpy.zeros((N, N))
for i in range(N):
for j in range(N):
Pmatrix[i][j] = targets[i] * targets[j] * kernel_function(inputs[i], inputs[j])
start = numpy.zeros(N)
C = 10
B = [(0, C) for b in range(N)]
XC = {'type': 'eq', 'fun': zerofun}
def indicator_function(x, y, nonzero_alphas, b_value):
indicator = 0
S = [x, y]
for i in range(len(nonzero_alphas)):
indicator += nonzero_alphas[i][1] * nonzero_alphas[i][2] * kernel_function(S, nonzero_alphas[i][3])
indicator -= b_value
return indicator
def main():
ret = opt.minimize(objective, start, bounds=B, constraints=XC)
alpha = ret['x']
nonzero_alpha = []
for i in range(N):
if alpha[i] > 1e-5:
nonzero_alpha.append([i, alpha[i], targets[i], inputs[i]])
b_value = 0
for i in range(N):
b_value += alpha[i] * targets[i] * kernel_function(inputs[i], nonzero_alpha[0][3])
b_value -= targets[nonzero_alpha[0][0]]
xgrid = numpy.linspace(-15, 15)
ygrid = numpy.linspace(-15, 15)
grid = numpy.array([[indicator_function(x, y, nonzero_alpha, b_value) for x in xgrid] for y in ygrid])
plt.contour(xgrid, ygrid, grid, (-1.0, 0.0, 1.0), colors=('red', 'black', 'blue'), linewidths=(1, 3, 1))
plt.plot([p[0] for p in classA], [p[1] for p in classA], 'b.')
plt.plot([p[0] for p in classB], [p[1] for p in classB], 'r.')
plt.axis('equal')
plt.savefig('svmplot.pdf')
plt.show()
if __name__ == '__main__':
main()