-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvmplot.py
98 lines (85 loc) · 2.25 KB
/
svmplot.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
90
91
92
93
94
95
96
97
98
# svmplot.py
# Bill Waldrep, December 2012
#
# Utility functions for running/testing the svm
# class and plotting the graphs requested for
# homework 4.
# numeric stuff
import numpy as np
# plotting data
import pylab as pl
# generate test data
from sklearn.datasets.samples_generator import make_blobs
# actual svm module
import svm
def make2dData(n_samples):
# generate a 2d 2-class dataset
X,y = make_blobs(n_samples=n_samples, centers=[[-1,-1],[1,1]])
y[y==0] = -1
return X,y
def readSatData(fname='tr'):
data = []
with open('data/satimage.scale.' + fname, 'r') as f:
for line in f.readlines():
s = line.split()
row = np.zeros(37)
# set label
if s[0] == '6':
row[0] = 1
else:
row[0] = -1
hand = 1
for i in range(1,37):
k,v = s[hand].split(':')
if int(k) == i:
hand += 1
row[i] = float(v)
else:
row[i] = 0
data.append(row)
data = np.vstack(data)
y = data[:,0]
X = data[:,1:]
return X,y
def plot2d(s, X, y):
xmin = np.min(X[:,0])
ymin = np.min(X[:,1])
xmax = np.max(X[:,0])
ymax = np.max(X[:,1])
density = 50
xx, yy = np.meshgrid(np.linspace(xmin, xmax, density),
np.linspace(ymin, ymax, density))
result = s.eval(np.c_[xx.ravel(), yy.ravel()])
result = result.reshape(np.shape(xx))
pl.imshow(result, interpolation='nearest', extent=(xmin,xmax,ymin,ymax),
aspect='auto', origin='lower', cmap=pl.cm.PuOr_r)
contours = pl.contour(xx,yy,result,levels=[0],linewidths=2,linetypes='--')
pl.scatter(X[:,0],X[:,1],s=30,c=y,cmap=pl.cm.Paired)
pl.show()
#X,y = make2dData(200)
#c.train(X,y)
#print c.alphas
#print c.findC(X,y,count=5)
#X,y = readSatData()
#c = s.findC(X,y,count=50,kfolds=5)
#s = svm.SVM(c,kernel=svm.RBFKernel(sigma))
#s.train(X,y)
#Xt,yt = readSatData('t')
#print "final error", s.test(Xt,yt)
#print "final error", s.test(X,y)
sigma = 2.0
#c = 10
k = svm.RBFKernel(sigma)
k = svm.LinKernel()
X,y = make2dData(200)
cs = [0.1, 1, 5, 10, 100]
cs = np.logspace(-2,2,10)
sigmas = [0.5, 1, 2, 5]
for c in cs:
for sigma in sigmas:
k = svm.RBFKernel(sigma)
print "c =", c, "sigma = ", sigma
s = svm.SVM(c,k)
s.train(X,y)
print "error", s.test(X,y)
plot2d(s,X,y)