/** Calculate the recall for a particular target index from an array list of classifications */ public double getRecall( ArrayList classification, int index ) { int numCorrect = 0; int numInstances = 0; int trueLabel, classLabel; for (int i = 0; i<classification.size(); i++) { trueLabel = ((Labeling)(((Classification)classification.get(i)).getInstance()).getLabeling()).getBestIndex(); classLabel = ((Classification)classification.get(i)).getLabeling().getBestIndex(); if ( trueLabel == index ) { numInstances++; if ( classLabel == index) numCorrect++; } } if (numInstances==0) logger.warning("No class instances: dividing by 0"); return ((double)numCorrect/(double)numInstances); }
/** Calculate the precision for a particular target index from an array list of classifications */ public double getPrecision( ArrayList classification, int index ) { int numCorrect = 0; int numInstances = 0; int trueLabel, classLabel; for (int i = 0; i<classification.size(); i++) { trueLabel = ((Labeling)(((Classification)classification.get(i)).getInstance()).getLabeling()).getBestIndex(); classLabel = ((Classification)classification.get(i)).getLabeling().getBestIndex(); if ( classLabel == index) { numInstances++; if ( trueLabel == index ) numCorrect++; } } if (numInstances==0) logger.warning("No class instances: dividing by 0"); return ((double)numCorrect/(double)numInstances); }
public double valueOfCorrectLabel () { Labeling correctLabeling = instance.getLabeling(); int correctLabelIndex = correctLabeling.getBestIndex(); return labeling.value (correctLabelIndex); }
/** * Constructs matrix and calculates values * @param t the trial to build matrix from */ public ConfusionMatrix(Trial t) { this.trial = t; this.classifications = t.toArrayList(); Labeling tempLabeling = ((Classification)classifications.get(0)).getLabeling(); this.numClasses = tempLabeling.getLabelAlphabet().size(); values = new int[numClasses][numClasses]; for(int i=0; i < classifications.size(); i++) { LabelVector lv = ((Classification)classifications.get(i)).getLabelVector(); Instance inst = ((Classification)classifications.get(i)).getInstance(); int bestIndex = lv.getBestIndex(); int correctIndex = inst.getLabeling().getBestIndex(); assert(correctIndex != -1); //System.out.println("Best index="+bestIndex+". Correct="+correctIndex); values[correctIndex][bestIndex]++; } }
int li = labeling.getBestIndex(); value = - (instanceWeight * Math.log (scores[li])); if(Double.isNaN(value)) {
int li = labeling.getBestIndex(); value = - (instanceWeight * Math.log (scores[li])); if(Double.isNaN(value)) {
int classIndex = labeling.getBestIndex(); if (!perClassFeaturesAlreadyThere[classIndex].contains (name)) { afv.add (name, 1.0);
FeatureVector fv = (FeatureVector) instance.getData(); int classIndex = instance.getLabeling().getBestIndex(); classCounts[classIndex]++; for (int fvi = 0; fvi < fv.numLocations(); fvi++) {
for (int i = 0; i < numInstances; i++) { Instance inst = trainingList.getInstance(i); int trueClassIndex = inst.getLabeling().getBestIndex(); for (int j = 0; j < numClasses; j++) { if (j != trueClassIndex) {
Alphabet fdict = fv.getAlphabet(); assert (fv.getAlphabet() == fd); int li = labeling.getBestIndex(); MatrixOps.rowPlusEquals (constraints, numFeatures, li, fv, instanceWeight);
Alphabet fdict = fv.getAlphabet(); assert (fv.getAlphabet() == fd); int li = labeling.getBestIndex();
/** * Classify the given instance using only the first * <tt>numWeakClassifiersToUse</tt> classifiers * trained during boosting */ public Classification classify (Instance inst, int numWeakClassifiersToUse) { if (numWeakClassifiersToUse <= 0 || numWeakClassifiersToUse > weakClassifiers.length) throw new IllegalArgumentException("number of weak learners to use out of range:" + numWeakClassifiersToUse); FeatureVector fv = (FeatureVector) inst.getData(); assert (instancePipe == null || fv.getAlphabet () == this.instancePipe.getDataAlphabet ()); int numClasses = getLabelAlphabet().size(); double[] scores = new double[numClasses]; int bestIndex; double sum = 0; // Gather scores of all weakClassifiers for (int round = 0; round < numWeakClassifiersToUse; round++) { bestIndex = weakClassifiers[round].classify(inst).getLabeling().getBestIndex(); scores[bestIndex] += alphas[round]; sum += scores[bestIndex]; } // Normalize the scores for (int i = 0; i < scores.length; i++) scores[i] /= sum; return new Classification (inst, this, new LabelVector (getLabelAlphabet(), scores)); }
/** * Classify the given instance using only the first * <tt>numWeakClassifiersToUse</tt> classifiers * trained during boosting */ public Classification classify (Instance inst, int numWeakClassifiersToUse) { if (numWeakClassifiersToUse <= 0 || numWeakClassifiersToUse > weakClassifiers.length) throw new IllegalArgumentException("number of weak learners to use out of range:" + numWeakClassifiersToUse); FeatureVector fv = (FeatureVector) inst.getData(); assert (instancePipe == null || fv.getAlphabet () == this.instancePipe.getDataAlphabet ()); int numClasses = getLabelAlphabet().size(); double[] scores = new double[numClasses]; int bestIndex; double sum = 0; // Gather scores of all weakClassifiers for (int round = 0; round < numWeakClassifiersToUse; round++) { bestIndex = weakClassifiers[round].classify(inst).getLabeling().getBestIndex(); scores[bestIndex] += alphas[round]; sum += scores[bestIndex]; } // Normalize the scores for (int i = 0; i < scores.length; i++) scores[i] /= sum; return new Classification (inst, this, new LabelVector (getLabelAlphabet(), scores)); }
private static double[][] calcFeatureCounts (InstanceList ilist) { int numClasses = ilist.getTargetAlphabet().size(); int numFeatures = ilist.getDataAlphabet().size(); double[][] featureCounts = new double[numClasses][numFeatures]; // Count features across all classes for (int i = 0; i < ilist.size(); i++) { Instance inst = ilist.getInstance(i); if (!(inst.getData() instanceof FeatureVector)) throw new IllegalArgumentException ("Currently only handles FeatureVector data"); FeatureVector fv = (FeatureVector) inst.getData (); // xxx Note that this ignores uncertain-labels. int labelIndex = inst.getLabeling ().getBestIndex(); int fli; for (int fl = 0; fl < fv.numLocations(); fl++) { fli = fv.indexAtLocation(fl); if (countInstances) featureCounts[labelIndex][fli]++; else featureCounts[labelIndex][fli] += fv.valueAtLocation(fl); } } return featureCounts; }
FeatureVector fv = (FeatureVector) inst.getData(); int fvisize = fv.numLocations(); int correctIndex = labeling.getBestIndex(); Arrays.fill(results, 0);
double[] results = new double [numLabels]; int fvisize = fv.numLocations(); int correctIndex = labeling.getBestIndex();