/** * Classifies the input. * * @param input the value for each feature. * @return the probability associated with each classification. */ public HashMap<Integer, Double> classify(double[] input) { HashMap<Integer, Double> p = new HashMap<Integer, Double>(); double tProb = 0; for (Hypothesis h : hypothesis) { double prob = h.predict(input); tProb += prob; p.put(h.getClassification(), prob); } for (Integer c : p.keySet()) p.put(c, p.get(c) / tProb); return p; }
/** * Runs gradient decent to tune the parameters of each hypothesis. * * @param iterations the number of times to run gradient decent */ public void tune(int iterations) { for (Hypothesis h : hypothesis) { // construct a new training set using One vs. Rest // if the training example has the same value as the // hypothesis then set the answer to 1 // otherwise set the answer to 0. TrainingExample[] tSet = new TrainingExample[trainingSet.length]; int answer; int i = 0; for (TrainingExample t : trainingSet) { if (t.getAnswer() == h.getClassification()) answer = 1; else answer = 0; tSet[i] = new TrainingExample(t.getInput(), answer); ++i; } for (i = 0; i < iterations; ++i) { h.gradientDecent(tSet); } } }
/** Prints each hypothesis along with its classification to stdout. */ public void show() { for (Hypothesis h : hypothesis) { System.out.print(h.getClassification() + ": "); h.show(); } }