用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);