Example usage for weka.classifiers.evaluation NominalPrediction NominalPrediction

List of usage examples for weka.classifiers.evaluation NominalPrediction NominalPrediction

Introduction

In this page you can find the example usage for weka.classifiers.evaluation NominalPrediction NominalPrediction.

Prototype

public NominalPrediction(double actual, double[] distribution, double weight) 

Source Link

Document

Creates the NominalPrediction object.

Usage

From source file:bme.mace.logicdomain.Evaluation.java

License:Open Source License

/**
 * Evaluates the classifier on a single instance and records the prediction
 * (if the class is nominal).//  w  w  w .j  a  v a2 s .  c  o m
 * 
 * @param classifier machine learning classifier
 * @param instance the test instance to be classified
 * @return the prediction made by the clasifier
 * @throws Exception if model could not be evaluated successfully or the data
 *           contains string attributes
 */
public double evaluateModelOnceAndRecordPrediction(List<LibSVM> classifier, List<Double> classifierWeight,
        Instance instance) throws Exception {
    Instance classMissing = (Instance) instance.copy();
    double pred = 0;
    classMissing.setDataset(instance.dataset());
    classMissing.setClassMissing();
    if (m_ClassIsNominal) {
        if (m_Predictions == null) {
            m_Predictions = new FastVector();
        }
        List<double[]> prob = new ArrayList<double[]>();//
        double[] finalProb = new double[instance.numClasses()];
        for (int i = 0; i < classifier.size(); i++) {
            double[] dist = classifier.get(i).distributionForInstance(classMissing);//
            prob.add(dist);
        }
        for (int i = 0; i < finalProb.length; i++) {
            for (int j = 0; j < classifier.size(); j++) {
                finalProb[i] += prob.get(j)[i] * classifierWeight.get(j);
            }
        }
        double sum = 0;
        for (int i = 0; i < finalProb.length; i++) {
            sum += finalProb[i];
        }
        for (int i = 0; i < finalProb.length; i++) {
            finalProb[i] = finalProb[i] / sum;
        }
        pred = Utils.maxIndex(finalProb);
        if (finalProb[(int) pred] <= 0) {
            pred = Instance.missingValue();
        }
        updateStatsForClassifier(finalProb, instance);
        m_Predictions.addElement(new NominalPrediction(instance.classValue(), finalProb, instance.weight()));
    } else {

        pred = classifier.get(0).classifyInstance(classMissing);
        updateStatsForPredictor(pred, instance);
    }
    return pred;
}

From source file:bme.mace.logicdomain.Evaluation.java

License:Open Source License

/**
 * Evaluates the supplied distribution on a single instance.
 * //from   www  .ja  v  a  2 s .  c  o m
 * @param dist the supplied distribution
 * @param instance the test instance to be classified
 * @return the prediction
 * @throws Exception if model could not be evaluated successfully
 */
public double evaluateModelOnceAndRecordPrediction(double[] dist, Instance instance) throws Exception {
    double pred;
    if (m_ClassIsNominal) {
        if (m_Predictions == null) {
            m_Predictions = new FastVector();
        }
        pred = Utils.maxIndex(dist);
        if (dist[(int) pred] <= 0) {
            pred = Instance.missingValue();
        }
        updateStatsForClassifier(dist, instance);
        m_Predictions.addElement(new NominalPrediction(instance.classValue(), dist, instance.weight()));
    } else {
        pred = dist[0];
        updateStatsForPredictor(pred, instance);
    }
    return pred;
}

From source file:com.evaluation.ConfidenceLabelBasedMeasures.java

License:Open Source License

private void computeMeasures(MultiLabelOutput[] output, boolean[][] trueLabels) {
    int numLabels = trueLabels[0].length;

    // AUC/*from   w  w  w .  j a va  2s . c o m*/
    FastVector[] m_Predictions = new FastVector[numLabels];
    for (int j = 0; j < numLabels; j++)
        m_Predictions[j] = new FastVector();
    FastVector all_Predictions = new FastVector();

    int numInstances = output.length;
    for (int instanceIndex = 0; instanceIndex < numInstances; instanceIndex++) {
        double[] confidences = output[instanceIndex].getConfidences();
        for (int labelIndex = 0; labelIndex < numLabels; labelIndex++) {

            int classValue;
            boolean actual = trueLabels[instanceIndex][labelIndex];
            if (actual)
                classValue = 1;
            else
                classValue = 0;

            double[] dist = new double[2];
            dist[1] = confidences[labelIndex];
            dist[0] = 1 - dist[1];

            m_Predictions[labelIndex].addElement(new NominalPrediction(classValue, dist, 1));
            all_Predictions.addElement(new NominalPrediction(classValue, dist, 1));
        }
    }

    labelAUC = new double[numLabels];
    for (int i = 0; i < numLabels; i++) {
        ThresholdCurve tc = new ThresholdCurve();
        Instances result = tc.getCurve(m_Predictions[i], 1);
        labelAUC[i] = ThresholdCurve.getROCArea(result);
    }
    auc[Averaging.MACRO.ordinal()] = Utils.mean(labelAUC);
    ThresholdCurve tc = new ThresholdCurve();
    Instances result = tc.getCurve(all_Predictions, 1);
    auc[Averaging.MICRO.ordinal()] = ThresholdCurve.getROCArea(result);
}

From source file:cotraining.copy.Evaluation_D.java

License:Open Source License

/**
 * Evaluates the classifier on a single instance and records the
 * prediction (if the class is nominal).
 *
 * @param classifier machine learning classifier
 * @param instance the test instance to be classified
 * @return the prediction made by the clasifier
 * @throws Exception if model could not be evaluated 
 * successfully or the data contains string attributes
 *//*  www  .j a  v  a  2s .  c o  m*/
public double evaluateModelOnceAndRecordPrediction(Classifier classifier, Instance instance) throws Exception {

    Instance classMissing = (Instance) instance.copy();
    double pred = 0;
    classMissing.setDataset(instance.dataset());
    classMissing.setClassMissing();
    if (m_ClassIsNominal) {
        if (m_Predictions == null) {
            m_Predictions = new FastVector();
        }
        double[] dist = classifier.distributionForInstance(classMissing);
        pred = Utils.maxIndex(dist);
        if (dist[(int) pred] <= 0) {
            pred = Instance.missingValue();
        }
        updateStatsForClassifier(dist, instance);
        m_Predictions.addElement(new NominalPrediction(instance.classValue(), dist, instance.weight()));
    } else {
        pred = classifier.classifyInstance(classMissing);
        updateStatsForPredictor(pred, instance);
    }
    return pred;
}

From source file:mulan.evaluation.measure.LabelBasedAUC.java

License:Open Source License

protected void updateConfidence(double[] confidences, boolean[] truth) {
    for (int labelIndex = 0; labelIndex < numOfLabels; labelIndex++) {

        int classValue;
        boolean actual = truth[labelIndex];
        if (actual) {
            classValue = 1;//from  w  ww .  ja v  a2s.  c  o m
        } else {
            classValue = 0;
        }

        double[] dist = new double[2];
        dist[1] = confidences[labelIndex];
        dist[0] = 1 - dist[1];

        m_Predictions[labelIndex].addElement(new NominalPrediction(classValue, dist, 1));
        all_Predictions.addElement(new NominalPrediction(classValue, dist, 1));
    }
}