/** * 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; }
/** * Calculates the cost of the <code>trainingSet</code>. * * @param hyp the hypothesis to use in calculating the cost. * @return the cost associated with the hypothesis. */ public double defaultCostFunction(Hypothesis hyp) { double error = 0; double h; int answer; for (TrainingExample t : trainingSet) { try { h = (Double) hyp.predict(t.getInput()); } catch (Exception e) { e.printStackTrace(); continue; } answer = t.getAnswer(); error -= answer * log(h) + (1 - answer) * log(1 - h); } double regError = 0; for (int i = 0; i < hyp.getNumFeatures(); ++i) { regError += pow(hyp.getParameter(i), 2); } error += regError / regularizationParam; return error / (2 * trainingSet.length); }