-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttt1.py
76 lines (67 loc) · 1.87 KB
/
ttt1.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
from libsvm.python.svmutil import *
import numpy as np
#from plot_decision_boundary import plot_decision_boundary
from matplotlib import pyplot as plt
import xlrd
data = xlrd.open_workbook('xigua_data.xls')
table = data.sheet_by_name('Sheet1')
y = table.col_values(2)
x = []
for idx in xrange(table.nrows):
x.append({1:table.cell(idx,0).value,2:table.cell(idx,1).value})
model1 = svm_train(y[:16],x[:16],'-t 3')
model2 = svm_train(y[:16],x[:16],'-c 4')
print '!!!!!!!!!!!!!!!!!!!!!!!!'
print x
print y
#p_label, p_acc, p_vals = svm_predict(y[16:],x[16:],model)
#print p_label,p_acc,p_vals
X = np.array([[0.697,0.460,1],
[0.774,0.376,1],
[0.634,0.264,1],
[0.608,0.318,1],
[0.556,0.215,1],
[0.403,0.237,1],
[0.481,0.149,1],
[0.437,0.211,1],
[0.666,0.091,-1],
[0.243,0.267,-1],
[0.245,0.057,-1],
[0.343,0.099,-1],
[0.639,0.161,-1],
[0.657,0.198,-1],
[0.360,0.370,-1],
[0.719,0.103,-1],
[0.593,0.042,-1]])
x1_min,x1_max = X[:,0].min() - .1,X[:,0].max()+.1
x2_min,x2_max = X[:,1].min() - .1,X[:,1].max()+.1
#print x1_min,x1_max
y2 = X[:,2]
h = 0.01
plt.figure(figsize=(5,5),dpi=200)
#print np.linspace(x1_min,x1_max,h)
ax1 = plt.subplot(211)
ax2 = plt.subplot(212)
xx,yy = np.meshgrid(np.arange(x1_min,x1_max,h),np.arange(x2_min,x2_max,h))
#print '$$$$$$$$$$$$$$$$$$$$$$$$'
#print xx.ravel(),yy.ravel()
#z = pred_func(np.c_[xx.ravel(),yy.ravel()])
test_x = []
test_y = []
for i in xrange(len(xx.ravel())):
test_x.append({1:xx.ravel()[i],2:yy.ravel()[i]})
test_y.append(1)
z = svm_predict(test_y, test_x, model1)[0]
z = np.array(z)
z = z.reshape(xx.shape)
z1 = svm_predict(test_y, test_x, model2)[0]
z1 = np.array(z)
z1 = z.reshape(xx.shape)
plt.sca(ax1)
plt.contourf(xx,yy,z,cmap = plt.cm.Spectral)
plt.scatter(X[:,0],X[:,1],c = y2,s=50,cmap=plt.cm.Spectral)
plt.sca(ax2)
plt.contourf(xx,yy,z1,cmap = plt.cm.Spectral)
#print z
plt.scatter(X[:,0],X[:,1],c = y2,s=50,cmap=plt.cm.Spectral)
plt.show()