100 points were made using 4x+5y=2000 as the dividing line;
The initial dividing line is 0, 0;
After 1000 rounds of correction, the result is:
X+31 y = 11876
Comparison results 4 x + 5 y = 2000
is still relatively close.
Just beginning to update w the line of code mistaken, thought is to use predict to correct, in fact, should use the real value of sample to correct.
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);
Handwriting a machine learning entry algorithm-Perceptron algorithm