This is also written in processing3.0+, its official website https://processing.org/, suggest to look directly at reference example to learn.
Perceptron algorithm is used by our teacher to give the PPT, the realization of the two-dimensional perceptron, in order to facilitate the look, in fact, multi-dimensional is the same:
The operating effect is:
For testing convenience, I am using a click to pick the point, the keyboard click T, the case can be, the next point is a positive example, click F, the case can be, the next point is a negative example.
Pressing S is the beginning of an iteration with a learning degree of 1. The end will be directly out of line, press 2 will be a learning rate of 2 of the line, the number of iterations will be hit under the program, the value is 2.
Code is for reference only, do not copy. Reprint please indicate the source.
//Blog:http://www.cnblogs.com/SweetBeens/p/8176764.htmlclasspoint{floatx, y; intK; Point (floatX1,floaty1) {x= x1; y =Y1; }}intnum = 500;intLen = 0;floatH=1;floatR=0; Point [] Points=NewPoint[num];intIrit = 1000;floatW1 = 0, W2 = 0, b = 0;//false=-1,true=1BooleanType =true;//If one point wrong,then, you need another irretateBooleanFlag =true;//One irretatevoidrewrite () { for(inti = 0; i < Len; i++){ //if points[i] wrong side floatResult=points[i].k* (points[i].x*w1+points[i].y*w2+b); if(result<=0) {W1= w1+h*points[i].k*points[i].x; W2= w2+h*points[i].k*points[i].y; b= B + H * points[i].k*r*R; Print (b," "); //w1=w1/w2; //w2=1; //b=b/w2;flag=true; } } }//calculate R,check out in pptvoidR () {floatMax=0; for(inti=0;i<len;i++){ if(ABS (points[i].x) >max) Max=ABS (points[i].x); if(ABS (POINTS[I].Y) >max) Max=ABS (POINTS[I].Y); } R=Max; println ("Max", Max);}voidmousepressed () {point P=NewPoint (Mousex,mousey); if(type) {P.K= 1; Fill (255); } Else{P.K=-1; Fill (0); } rect (Mousex,mousey,5,5); Points[len]=p; Len++;}voidkeypressed () {intI=0; floatY1=0,y2=0; if(key = = ' t ' | | key = = ' t ') Type=true; Else if(Key = = ' F ' | | key = = ' F ') Type=false; Else if(Key = = ' s ' | | key = = ' s ')) {R (); while(i<irit&&flag==true) {i++; Flag=false; Rewrite (); //println ("W", W1, "", W2, "B", b);Y1 =-b/W2; Y2= (-b-w1*width)/W2; //Line (0,10,width,480);} println (y1," ", y2); println ("W1,w2,b" +w1,w2,b); Line (0, Y1,width,y2); } Else if(Key = = ' 2 ') {h= 0.2; W1= 0; W2= 0; b= 0; Flag=true; I=0; while(i<irit&&flag==true) {i++; Flag=false; Rewrite (); //println ("W", W1, "", W2, "B", b);Y1 =-b/W2; Y2= (-b-w1*width)/W2; //Line (0,10,width,480);} println (y1," ", y2); println ("W1,w2,b" +w1,w2,b); Line (0, Y1,width,y2); } println (I, i);}voidSetup () {Size (500,500); Background (255);}voidDraw () {}
Machine learning--Realization of perceptron algorithm