/* * LeastSquaresDiscriminator.java * * (C) 2008, Alex S. */ import java.awt.*; import java.util.*; /** * Least Squares Discriminator */ public class LeastSquaresDiscriminator extends BufferedApplet { private int width,height; // points are read into this thing. private Vector points = new Vector(); // matrices to easily move from model to screen and vice versa. private pMatrix toscreen = null; private pMatrix tomodel = null; CheckboxGroup checkboxes = null; /** * initialize things */ public void init(){ super.init(); width = bounds().width; height = bounds().height; tomodel = pMatrix.reflecty3d().mult(pMatrix.translate3d(-width/2,-height/2,0)); toscreen = tomodel.inv3d(); checkboxes = new CheckboxGroup(); Panel p = new Panel(); p.setLayout(new FlowLayout()); p.add(new Checkbox("x", checkboxes, true)); p.add(new Checkbox("o", checkboxes, false)); setLayout(new BorderLayout()); add(BorderLayout.SOUTH,p); } /** * find line equation with maximal margin that best classifies */ public double[] solve(){ // make the X (samples) matrix. pMatrix X = new pMatrix(points.size(),2); pMatrix Y = new pMatrix(points.size(),1); for(int i=0;i 0 ? "x" : "o",(int)p[0],(int)p[1]); } // render line; y = (D - Ax)/B double[] oldp = toscreen.mult(new double[]{ -width, (line[2] - line[0]*-width) / line[1], 0, 1 }); g.setColor(Color.blue); for(int x=-width;x