public LinearClassifier batchTrain(ArrayList<ClassificationInstance> trainingData) { LinearClassifier w = new LinearClassifier(xAlphabet, yAlphabet, fxy); LinearClassifier theta = null; if (performAveraging) theta = new LinearClassifier(xAlphabet, yAlphabet, fxy); for (int iter = 0; iter < numIterations; iter++) { for (ClassificationInstance inst : trainingData) { int yhat = w.label(inst.x); if (yhat != inst.y) { StaticUtils.plusEquals(w.w, fxy.apply(inst.x, inst.y)); StaticUtils.plusEquals(w.w, fxy.apply(inst.x, yhat), -1); } if (performAveraging) StaticUtils.plusEquals(theta.w, w.w, 1); } } if (performAveraging) return theta; return w; }
public static void main(String[] args) throws IOException { // read in the data. ArrayList<ClassificationInstance> allData = (new InternetAdReader(new Alphabet(), new Alphabet())).readFile(args[0]); StaticUtils.shuffle(allData, 0); // randomly split data into training and testing part ArrayList<ClassificationInstance>[] tmp = StaticUtils.split(allData, 200); ArrayList<ClassificationInstance> train = tmp[0]; ArrayList<ClassificationInstance> test = tmp[1]; Alphabet xA = allData.get(0).xAlphabet; Alphabet yA = allData.get(0).yAlphabet; System.out.println("num Features = " + allData.get(0).xAlphabet.size()); LinearClassifier h; h = trainAdaBoost(50, train, xA, yA); System.out.println("Boost Train Accuracy = " + StaticUtils.computeAccuracy(h, train)); System.out.println("Boost Test Accuracy = " + StaticUtils.computeAccuracy(h, test)); h = trainMaxEnt(train, xA, yA); // print out accuracy System.out.println("MaxEnt Train Accuracy = " + StaticUtils.computeAccuracy(h, train)); System.out.println("MaxEnt Test Accuracy = " + StaticUtils.computeAccuracy(h, test)); h = trainNaivBayes(train, xA, yA); // print out accuracy System.out.println("NaiveB Train Accuracy = " + StaticUtils.computeAccuracy(h, train)); System.out.println("NaiveB Test Accuracy = " + StaticUtils.computeAccuracy(h, test)); h = trainPerceptron(false, 20, train, xA, yA); System.out.println("Percep Train Accuracy = " + StaticUtils.computeAccuracy(h, train)); System.out.println("Percep Test Accuracy = " + StaticUtils.computeAccuracy(h, test)); h = trainPerceptron(true, 20, train, xA, yA); System.out.println("AvgPer Train Accuracy = " + StaticUtils.computeAccuracy(h, train)); System.out.println("AvgPer Test Accuracy = " + StaticUtils.computeAccuracy(h, test)); }