-
Notifications
You must be signed in to change notification settings - Fork 2
/
mlGraphics.py
114 lines (94 loc) · 3.47 KB
/
mlGraphics.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Some useful graphics functions
"""
import util
import binary
from numpy import *
from pylab import *
# def plotLinearClassifier(h, X, Y):
# """
# Draw the current decision boundary, margin and data
# """
# if type(h.weights) == ndarray:
# nx,mx,ny,my = axis()
# # find the point y such that
# # nx*w[0] + y*w[1] == 0
# # y = -(nx*w[0]) / w[1]
# nx_y = -(nx * h.weights[0]) / h.weights[1]
# mx_y = -(mx * h.weights[0]) / h.weights[1]
# # find the point x such that
# # x*w[0] + ny*w[1] + b == 0
# # x = -(ny*w[1] + b) / w[0]
# ny_x = -(ny * h.weights[1]) / h.weights[0]
# my_x = -(my * h.weights[1]) / h.weights[0]
# plot(X[Y>=0.5,0], X[Y>=0.5,1], 'b+',
# X[Y< 0.5,0], X[Y< 0.5,1], 'ro',
# [0.,h.weights[0]], [0.,h.weights[1]], 'g-',
# [nx,mx], [nx_y, mx_y], 'k-')
# legend(('positive', 'negative', 'weights', 'hyperplane'))
# nx,mx,ny,my = axis()
def plotLinearClassifier(h, X, Y):
"""
Draw the current decision boundary, margin and data
"""
plot(X[Y>=0.5,0], X[Y>=0.5,1], 'b+',
X[Y< 0.5,0], X[Y< 0.5,1], 'ro')
axes = figure(1).get_axes()[0]
xlim = axes.get_xlim()
ylim = axes.get_ylim()
xmin = xlim[0] + (xlim[1] - xlim[0]) / 100
xmax = xlim[1] - (xlim[1] - xlim[0]) / 100
ymin = ylim[0] + (ylim[1] - ylim[0]) / 100
ymax = ylim[1] - (ylim[1] - ylim[0]) / 100
if type(h.weights) == ndarray:
b = h.bias
w = h.weights
#print b
#print w
# find the zeros along each axis
# w0*l + w1*? + b = 0 ==> ? = -(b + w0*l) / w1
xmin_zero = - (b + w[0] * xmin) / w[1]
xmax_zero = - (b + w[0] * xmax) / w[1]
ymin_zero = - (b + w[1] * ymin) / w[0]
ymax_zero = - (b + w[1] * ymax) / w[0]
#print (ylim, xlim, (xmin_zero, xmax_zero), (ymin_zero, ymax_zero))
# now, two of these should actually be in bounds, figure out which
inBounds = []
if ylim[0] <= xmin_zero and xmin_zero <= ylim[1]:
inBounds.append( (xmin, xmin_zero) )
if ylim[0] <= xmax_zero and xmax_zero <= ylim[1]:
inBounds.append( (xmax, xmax_zero) )
if xlim[0] <= ymin_zero and ymin_zero <= xlim[1]:
inBounds.append( (ymin_zero, ymin) )
if xlim[0] <= ymax_zero and ymax_zero <= xlim[1]:
inBounds.append( (ymax_zero, ymax) )
#print inBounds
if len(inBounds) >= 2:
plot(X[Y>=0.5,0], X[Y>=0.5,1], 'b+',
X[Y< 0.5,0], X[Y< 0.5,1], 'ro',
[inBounds[0][0], inBounds[1][0]], [inBounds[0][1], inBounds[1][1]], 'k-')
figure(1).set_axes([axes])
legend(('positive', 'negative', 'hyperplane'))
else:
plot(X[Y>=0.5,0], X[Y>=0.5,1], 'b+',
X[Y< 0.5,0], X[Y< 0.5,1], 'ro')
figure(1).set_axes([axes])
legend(('positive', 'negative'))
def runOnlineClassifier(h, X, Y):
N,D = X.shape
order = range(N)
util.permute(order)
plot(X[Y< 0.5,0], X[Y< 0.5,1], 'b+',
X[Y>=0.5,0], X[Y>=0.5,1], 'ro')
noStop = False
for n in order:
print (Y[n], X[n,:])
h.nextExample(X[n,:], Y[n])
hold(True)
plot([X[n,0]], [X[n,1]], 'ys')
hold(False)
if not noStop:
v = raw_input()
if v == "q":
noStop = True
plotLinearClassifier(h, X, Y)