xxxxxxxxxx
67
var per;
var weights = [0,0,0]
var ind = 0;
var po = 0;
var max_points = 1000;
var pointab = [];
function drawline(weights)
{
let slope = -(weights[2] / weights[1]) / (weights[2] / weights[0])
let y_intercept = (-weights[2]/weights[1]);
let y1 = y_intercept;
let y2 = slope * width + y_intercept;
//let y1 = (-weights[2] - weights[0]*0)/weights[1];
//let y2 = (-weights[2] - weights[0]*width)/weights[1];
strokeWeight(2);
stroke(0);
line(0, y1, width, y2);
}
function setup() {
createCanvas(800, 800);
background(230);
per = new perceptron(random(-1, 1), random(-1, 1), 0, 0.01);
frameRate(200);
}
function draw()
{
background(255);
for (let i = 0; i < pointab.length; i++)
{
if (pointab[i][0] + 300 > pointab[i][1])
stroke(255,0,0);
else
stroke(0,0,255);
ellipse(pointab[i][0], pointab[i][1], 3, 3);
}
while (ind < max_points)
{
let points = [random(0, width), random(0, height)];
pointab.push(points);
let status = 0;
/*if(points[0] > points[1])
status = 1;
else
status = -1;
per.train(points[0], points[1], status);*/
ind++;
}
let status = 0;
if(pointab[po][0] > pointab[po][1])
status = 1;
else
status = -1;
if (per.predict(pointab[po][0], pointab[po][1]) != status)
{
stroke(0);
ellipse(pointab[po][0], pointab[po][1], 3, 3);
per.train(pointab[po][0], pointab[po][1], status);
}
po++;
if (po >= pointab.length)
po = 0;
drawline(per.getweights());
}