📄 weightedperformancemeasures.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.operator.learner.meta;
import java.awt.image.RescaleOp;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.operator.OperatorException;
/**
* This private class cares about <i>weighted</i> performance measures as
* used by the <code>BayesianBoosting</code> algorithm and the similarly
* working <code>ModelBasedSampling</code> operator.
*
* @author scholz
*/
public class WeightedPerformanceMeasures {
/** This constant is used to express that no examples have been observed. */
public static final double RULE_DOES_NOT_APPLY = Double.NaN;
private double[] predictions;
private double[] labels;
private double[][] pred_label;
/**
* Constructor. Reads an example set, calculates its weighted performance
* values and caches them internally for later requests.
* @param exampleSet the <code>ExampleSet</code> this object shall hold the performance
* measures for
*/
public WeightedPerformanceMeasures(ExampleSet exampleSet)
throws OperatorException
{
{
int numberOfClasses = exampleSet.getLabel().getValues().size();
this.labels = new double[numberOfClasses];
// It is not necessary to interpret the result of the embedded learner as
// predictions. Especially it not mandatory to have as many "predictions" as
// labels. However, without any further information let's assume the simple
// case, namely that the learner tries to predict the label with the result
// of the model:
this.predictions = new double[numberOfClasses];
// This array stores all combinations:
this.pred_label = new double[this.predictions.length][this.labels.length];
}
ExampleReader reader = exampleSet.getExampleReader();
double sumOfWeights = 0;
if (exampleSet.getPredictedLabel().isNumerical()) {
// the used model gave confidence-predictions, only supported for the binary case
if (this.predictions.length != 2) {
throw new OperatorException("Soft base classifiers only supported for binary classification problems.");
}
while (reader.hasNext()) {
Example exa = reader.next();
double exaW = exa.getWeight();
sumOfWeights += exaW;
int eLabel = (int) (exa.getLabel() - Attribute.FIRST_CLASS_INDEX);
double ePred = exa.getPredictedLabel();
if (ePred < 0)
ePred = 0;
else if (ePred > 1)
ePred = 1;
this.labels[eLabel] += exaW;
// These mappings are obviously problematic:
this.predictions[0] += (1 - ePred) * exaW;
this.predictions[1] += ePred * exaW;
this.pred_label[0][eLabel] += (1 - ePred) * exaW;
this.pred_label[1][eLabel] += ePred * exaW;
}
}
else while (reader.hasNext()) {
// crisp base classifier, multi-class prediction problems possible
Example exa = reader.next();
double exaW = exa.getWeight();
sumOfWeights += exaW;
int eLabel = (int) (exa.getLabel() - Attribute.FIRST_CLASS_INDEX);
int ePred = (int) (exa.getPredictedLabel() - Attribute.FIRST_CLASS_INDEX);
this.labels[eLabel] += exaW;
this.predictions[ePred] +=exaW;
this.pred_label[ePred][eLabel] +=exaW;
}
if (sumOfWeights > 0) {
// If sum is 0 all examples have been "explained deterministically"!
// Otherwise: Normalize!
for (int i=0; i<this.predictions.length; i++) {
this.predictions[i] /= sumOfWeights;
for (int j=0; j<this.labels.length; j++) {
this.pred_label[i][j] /= sumOfWeights;
}
}
for (int j=0; j<this.labels.length; j++) {
this.labels[j] /= sumOfWeights;
}
}
else { // Assign default values to all fields.
double defaultPredProb = 1 / ((double) this.predictions.length);
double defaultLabelProb = 1 / ((double) this.labels.length);
double defaultPredLabelProb = defaultPredProb * defaultLabelProb;
for (int i=0; i<this.predictions.length; i++) {
this.predictions[i] = defaultPredProb;
for (int j=0; j<this.labels.length; j++) {
this.pred_label[i][j] = defaultPredLabelProb;
}
}
for (int j=0; j<this.labels.length; j++) {
this.labels[j] = defaultLabelProb;
}
}
}
/**
* @return the number of classes, namely different values of this
* object's example set's label attribute
*/
public int getNumberOfLabels() {
return this.labels.length;
}
/**
* @return number of predictions or nominal classes predicted by
* the embedded learner. Not necessarily the same as the number of
* class labels.
*/
public int getNumberOfPredictions() {
return this.predictions.length;
}
/**
* Method to query for the probability of one of the prediction/label subsets
* "label=Attribute.FIRST_CLASS_INDEX, prediction=...", ...
* @param label the (correct) class label of the example as it comes from the
* internal index
* @param prediction the boolean value predicted by the model (premise)
* (internal index number)
* @return the joint probability of label and prediction
*/
public double getProbability(int label, int prediction) {
return this.pred_label[prediction - Attribute.FIRST_CLASS_INDEX][label - Attribute.FIRST_CLASS_INDEX];
}
/**
* Method to query for the "prior" probability of one of the labels.
* @param label the nominal class label (internal YALE index)
* @return the probability of seeing an example with this label
*/
public double getProbabilityLabel(int label) {
return this.labels[label - Attribute.FIRST_CLASS_INDEX];
}
/**
* Method to query for the "prior" probability of one of the predictions.
* @param premise the prediction of a model (internal YALE index)
* @return the probability of drawing an example so that the model makes this prediction
*/
public double getProbabilityPrediction(int premise) {
return this.predictions[premise - Attribute.FIRST_CLASS_INDEX];
}
/** Helper method calculating the number of non-empty classes for a premise. */
private int getNumOfClassesFor(int prediction) {
int nonNulls = 0;
for (int i=0; i<this.getNumberOfLabels(); i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -