/** * Convert to Weka (multi-target) Predictions. * Note: currently only multi-label. * */ public static ArrayList<Prediction> toWekaPredictions(int y[], double p[]) { ArrayList<Prediction> predictions = new ArrayList<Prediction>(); for(int i = 0; i < y.length; i++) { predictions.add(new NominalPrediction((double)y[i], new double[]{1.-p[i],p[i]})); } return predictions; }
/** * Gets a human readable representation of this prediction. * * @return a human readable representation of this prediction. */ public String toString() { StringBuffer sb = new StringBuffer(); sb.append("NOM: ").append(actual()).append(" ").append(predicted()); sb.append(' ').append(weight()); double [] dist = distribution(); for (int i = 0; i < dist.length; i++) { sb.append(' ').append(dist[i]); } return sb.toString(); }
/** * Pulls all the margin values out of a vector of NominalPredictions. * * @param predictions a FastVector containing NominalPredictions * @return an array of margin values. */ private double[] getMargins(ArrayList<Prediction> predictions) { // sort by predicted probability of the desired class. double[] margins = new double[predictions.size()]; for (int i = 0; i < margins.length; i++) { NominalPrediction pred = (NominalPrediction) predictions.get(i); margins[i] = pred.margin(); } return margins; }
public double calculateAccuracy(ArrayList predictions) { double correct = 0; if (predictions != null && predictions.size() > 0) { for (Object prediction : predictions) { NominalPrediction np = (NominalPrediction) prediction; if (np.predicted() == np.actual()) { correct++; } } return 100 * correct / predictions.size(); } return 0.0; }
/** * Includes a prediction in the confusion matrix. * * @param pred the NominalPrediction to include * @exception Exception if no valid prediction was made (i.e. unclassified). */ public void addPrediction(NominalPrediction pred) throws Exception { if (pred.predicted() == NominalPrediction.MISSING_VALUE) { throw new Exception("No predicted value given."); } if (pred.actual() == NominalPrediction.MISSING_VALUE) { throw new Exception("No actual value given."); } set((int) pred.actual(), (int) pred.predicted(), get((int) pred.actual(), (int) pred.predicted()) + pred.weight()); }
|| (((NominalPrediction) predictions.get(0)).distribution().length <= classIndex)) { return null; if (pred.actual() == Prediction.MISSING_VALUE) { System.err.println(getClass().getName() + " Skipping prediction with missing class value"); continue; if (pred.weight() < 0) { System.err.println(getClass().getName() + " Skipping prediction with negative weight"); continue; if (pred.actual() == classIndex) { totPos += pred.weight(); } else { totNeg += pred.weight(); if (pred.actual() == Prediction.MISSING_VALUE) { System.err.println(getClass().getName() + " Skipping prediction with missing class value"); continue; if (pred.weight() < 0) { System.err.println(getClass().getName() + " Skipping prediction with negative weight"); continue; if (pred.actual() == classIndex) {
/** * * @param predictions the predictions to use * @param classIndex the class index * @return the probabilities */ private double[] getProbabilities(ArrayList<Prediction> predictions, int classIndex) { // sort by predicted probability of the desired class. double[] probs = new double[predictions.size()]; for (int i = 0; i < probs.length; i++) { NominalPrediction pred = (NominalPrediction) predictions.get(i); probs[i] = pred.distribution()[classIndex]; } return probs; }
Classifier nbTree = (Classifier)SerializationHelper.read(Model) as NBTree; Instances testDataSet = new Instances(new BufferedReader(new FileReader(arff))); testDataSet.setClassIndex(10); Evaluation evaluation = new Evaluation(testDataSet); for (int i = 0; i < testDataSet.numInstances(); i++) { Instance instance = testDataSet.instance(i); evaluation.evaluateModelOnceAndRecordPrediction(nbTree, instance); } foreach (object o in evaluation.predictions().toArray()) { NominalPrediction prediction = o as NominalPrediction; if (prediction != null) { double[] distribution = prediction.distribution(); double predicted = prediction.predicted(); } }
protected static ExperimentResults resultsFromEvaluation(Evaluation eval, String authorCSV, List<String> documentTitles){ ExperimentResults results = new ExperimentResults(); FastVector predictions = eval.predictions(); String[] authors = getAuthorsFromAttributeString(authorCSV); //for each document for (int i = 0; i<predictions.size(); i++){ NominalPrediction prediction = (NominalPrediction)predictions.elementAt(i); String actual = authors[(int)prediction.actual()]; double[] probabilities = prediction.distribution(); Map<String,Double> probMap = new HashMap<String,Double>(); //for each potential author... for (int j = 0; j< probabilities.length; j++){ probMap.put(authors[j], probabilities[j]); } results.addDocResult(new DocResult(documentTitles.get(i),actual,probMap)); } return results; }
/** * Creates the NominalPrediction object. * * @param actual the actual value, or MISSING_VALUE. * @param distribution the predicted probability distribution. Use * NominalPrediction.makeDistribution() if you only know the predicted value. * @param weight the weight assigned to the prediction. */ public NominalPrediction(double actual, double [] distribution, double weight) { if (distribution == null) { throw new NullPointerException("Null distribution in NominalPrediction."); } m_Actual = actual; m_Distribution = distribution.clone(); m_Weight = weight; updatePredicted(); }
for (int element : sorted) { double current = margins[element]; double weight = ((NominalPrediction) predictions.get(element)).weight(); totalMargin += weight; binMargin += weight;
/** * Includes a prediction in the confusion matrix. * * @param pred the NominalPrediction to include * @exception Exception if no valid prediction was made (i.e. unclassified). */ public void addPrediction(NominalPrediction pred) throws Exception { if (pred.predicted() == NominalPrediction.MISSING_VALUE) { throw new Exception("No predicted value given."); } if (pred.actual() == NominalPrediction.MISSING_VALUE) { throw new Exception("No actual value given."); } set((int) pred.actual(), (int) pred.predicted(), get((int) pred.actual(), (int) pred.predicted()) + pred.weight()); }
|| (((NominalPrediction) predictions.get(0)).distribution().length <= classIndex)) { return null; if (pred.actual() == Prediction.MISSING_VALUE) { System.err.println(getClass().getName() + " Skipping prediction with missing class value"); continue; if (pred.weight() < 0) { System.err.println(getClass().getName() + " Skipping prediction with negative weight"); continue; if (pred.actual() == classIndex) { totPos += pred.weight(); } else { totNeg += pred.weight(); if (pred.actual() == Prediction.MISSING_VALUE) { System.err.println(getClass().getName() + " Skipping prediction with missing class value"); continue; if (pred.weight() < 0) { System.err.println(getClass().getName() + " Skipping prediction with negative weight"); continue; if (pred.actual() == classIndex) {
/** * * @param predictions the predictions to use * @param classIndex the class index * @return the probabilities */ private double[] getProbabilities(ArrayList<Prediction> predictions, int classIndex) { // sort by predicted probability of the desired class. double[] probs = new double[predictions.size()]; for (int i = 0; i < probs.length; i++) { NominalPrediction pred = (NominalPrediction) predictions.get(i); probs[i] = pred.distribution()[classIndex]; } return probs; }
/** * Creates the NominalPrediction object. * * @param actual the actual value, or MISSING_VALUE. * @param distribution the predicted probability distribution. Use * NominalPrediction.makeDistribution() if you only know the predicted value. * @param weight the weight assigned to the prediction. */ public NominalPrediction(double actual, double [] distribution, double weight) { if (distribution == null) { throw new NullPointerException("Null distribution in NominalPrediction."); } m_Actual = actual; m_Distribution = distribution.clone(); m_Weight = weight; updatePredicted(); }
for (int element : sorted) { double current = margins[element]; double weight = ((NominalPrediction) predictions.get(element)).weight(); totalMargin += weight; binMargin += weight;
/** * Gets a human readable representation of this prediction. * * @return a human readable representation of this prediction. */ public String toString() { StringBuffer sb = new StringBuffer(); sb.append("NOM: ").append(actual()).append(" ").append(predicted()); sb.append(' ').append(weight()); double [] dist = distribution(); for (int i = 0; i < dist.length; i++) { sb.append(' ').append(dist[i]); } return sb.toString(); }
/** * Convert to Weka (multi-target) Predictions. * Note: currently only multi-label. * */ public static ArrayList<Prediction> toWekaPredictions(int y[], double p[]) { ArrayList<Prediction> predictions = new ArrayList<Prediction>(); for(int i = 0; i < y.length; i++) { predictions.add(new NominalPrediction((double)y[i], new double[]{1.-p[i],p[i]})); } return predictions; }
/** * Calculates the performance stats for the default class and return results * as a set of Instances. The structure of these Instances is as follows: * <p> * <ul> * <li><b>Probability Cost Function </b> * <li><b>Normalized Expected Cost</b> * <li><b>Threshold</b> contains the probability threshold that gives rise to * the previous performance values. * </ul> * <p> * * @see TwoClassStats * @param predictions the predictions to base the curve on * @return datapoints as a set of instances, null if no predictions have been * made. */ public Instances getCurve(ArrayList<Prediction> predictions) { if (predictions.size() == 0) { return null; } return getCurve(predictions, ((NominalPrediction) predictions.get(0)).distribution().length - 1); }
/** * Pulls all the margin values out of a vector of NominalPredictions. * * @param predictions a FastVector containing NominalPredictions * @return an array of margin values. */ private double[] getMargins(ArrayList<Prediction> predictions) { // sort by predicted probability of the desired class. double[] margins = new double[predictions.size()]; for (int i = 0; i < margins.length; i++) { NominalPrediction pred = (NominalPrediction) predictions.get(i); margins[i] = pred.margin(); } return margins; }