用4x+5y=2000作为分界线制造了100个点;
初始分界线为0,0;
经过1000轮纠正后,结果是:
22 x+31 y = 11876
对比结果4 x + 5 y = 2000
还是比较接近的。
刚开始更新w的那行代码搞错了,以为是用predict去纠正,其实应该用sample的真实值去纠正。
import random;
def find_split(points):
w=(0,0,0)
for _ in range(1,2000):
print 'w='+str(w);
for pt in points:
(x1,x2,z) = pt;
(w1,w2,w3)=w;
predict = int((w1+w2*x1+w3*x2)>0)*2-1
if predict!=z:
print 'wrong: '+str(pt)
w=(w1+z,w2+z*x1,w3+z*x2);
# break;
else:
print 'right: '+str(pt)
return w;
def test_split(points,w):
points_2 = filter(lambda pt:((int(w[0]+w[1]*pt[0]+w[2]*pt[1])>=0)*2-1)==pt[2],points)
return points_2;
def init_points(max_x,max_y,num_of_pts):
points=[];
for i in range(1,num_of_pts,1):
x = int(random.random()*max_x);
y = int(random.random()*max_y);
z = int((4*x+5*y)>=2000)*2-1
points.append((x,y,z));
return points;
if __name__ == '__main__':
points = init_points(400,500,100);
print points;
line = find_split(points);
print(line);
pts = test_split(points,line);
print points;
print len(pts);