Handwriting a machine learning entry algorithm-Perceptron algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.