/** 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); }
Citation c1 = (Citation)pair.getObject1(); Citation c2 = (Citation)pair.getObject2(); if (labeling.getBestLabel().toString().equals("yes") && labeling.getBestValue() > 0.8) { boolean newCluster = true; for (int j=0; j<clustering.size(); j++) {
static private void internalTest() throws IOException { Classifier classifier = train(internalData, internalTargets, 1.0, null); System.out.println("Training accuracy = " + test(classifier, internalData, internalTargets)); Classification cl = classify(classifier, internalInstance); Labeling lab = cl.getLabeling(); LabelAlphabet labels = lab.getLabelAlphabet(); for (int c = 0; c < labels.size(); c++) System.out.print(labels.lookupObject(c) + " " + lab.value(c) + " "); System.out.println(); }
public double valueOfCorrectLabel () { Labeling correctLabeling = instance.getLabeling(); int correctLabelIndex = correctLabeling.getBestIndex(); return labeling.value (correctLabelIndex); }
public void print () { // xxx Fix this. System.out.print (classifier.getClass().getName() + "(."); System.out.print (") = ["); for (int i = 0; i < labeling.numLocations(); i++) System.out.print (labeling.labelAtLocation(i).toString()+"="+labeling.valueAtLocation(i)+" "); System.out.println ("]"); }
private static double computeScore(MaxEnt classifier, Instance instPair) { // Include the feature weights according to each label Classification classification = (Classification)classifier.classify(instPair); Labeling labeling = classification.getLabeling(); /** NOTE: THIS ASSUMES THERE ARE JUST TWO LABELS - IE CLASSIFIER IS * BINARY */ double score = 0.0; if (labeling.labelAtLocation(0).toString().equals("no")) { score = labeling.valueAtLocation(1)-labeling.valueAtLocation(0); } else { score = labeling.valueAtLocation(0)-labeling.valueAtLocation(1); } return score; }
Labeling labeling = inst.getLabeling(); double labelWeightSum = 0; for (int ll = 0; ll < labeling.numLocations(); ll++) { int li = labeling.indexAtLocation(ll); double labelWeight = labeling.valueAtLocation(ll); labelWeightSum += labelWeight; targetCounts[li] += labelWeight; for (int ll = 0; ll < labeling.numLocations(); ll++) { int li = labeling.indexAtLocation(ll); double labelWeight = labeling.valueAtLocation(ll); passTestTargetCounts[li] += labelWeight;
private static void printTrialClassification(Trial trial) { ArrayList classifications = trial.toArrayList(); for (int i = 0; i < classifications.size(); i++) { Instance instance = trial.getClassification(i).getInstance(); System.out.print(instance.getName() + " " + instance.getTarget() + " "); Labeling labeling = trial.getClassification(i).getLabeling(); for (int j = 0; j < labeling.numLocations(); j++){ System.out.print(labeling.getLabelAtRank(j).toString() + ":" + labeling.getValueAtRank(j) + " "); } System.out.println(); } }
for (int ll = 0; ll < labeling.numLocations(); ll++) { int li = labeling.indexAtLocation (ll); double labelWeight = labeling.value (li); labelWeightSum += labelWeight; double labelWeightDiff = Math.abs(labelWeight - classifications[i].value(li));
public boolean bestLabelIsCorrect () { Labeling correctLabeling = instance.getLabeling(); if (correctLabeling == null) throw new IllegalStateException ("Instance has no label."); return (labeling.getBestLabel().equals (correctLabeling.getBestLabel())); }
/** * 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]++; } }
public LabelVector targetLabelDistribution () { if (instances.size() == 0) return null; if (!(getInstance(0).getTarget() instanceof Labeling)) throw new IllegalStateException ("Target is not a labeling."); double[] counts = new double[getTargetAlphabet().size()]; for (int i = 0; i < instances.size(); i++) { Instance instance = getInstance(i); Labeling l = (Labeling) instance.getTarget(); l.addTo (counts, getInstanceWeight(i)); } return new LabelVector ((LabelAlphabet)getTargetAlphabet(), counts); }
if (l.labelAtLocation(0).toString().equals("no")) sb.append (" (" + (params[indices[i]+numParams]-params[indices[i]]) + ")"); else
targetDict = ((Labeling)carrier.getTarget()).getLabelAlphabet(); else throw new IllegalStateException ("Instance target is not a " +
private double computeScore(MaxEnt classifier, Instance instPair) { // Include the feature weights according to each label Classification classification = (Classification)classifier.classify(instPair); Labeling labeling = classification.getLabeling(); /** NOTE: THIS ASSUMES THERE ARE JUST TWO LABELS - IE CLASSIFIER IS * BINARY */ double score = 0.0; if (labeling.labelAtLocation(0).toString().equals("no")) { score = labeling.valueAtLocation(1)-labeling.valueAtLocation(0); } else { score = labeling.valueAtLocation(0)-labeling.valueAtLocation(1); } return score; }
for (int ll = 0; ll < labeling.numLocations(); ll++) { int li = labeling.indexAtLocation (ll); double labelWeight = labeling.valueAtLocation (ll); labelWeightSum += labelWeight; if (labelWeight == 0) continue;
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) { Classification c = weakLearners[round].classify(inst); double htCorrect = c.valueOfCorrectLabel(); double htWrong = c.getLabeling().value(classIndices[i]); epsilon += weights[i] * (1 - htCorrect + htWrong); exponents[i] = 1 + htCorrect - htWrong;
public void print () { if (child0 == null) System.out.println (getName() + ": " + labeling.getBestLabel()); else { child0.print(); child1.print(); } }
public Classification classify (Instance inst) { int numClasses = getLabelAlphabet().size(); double[] scores = new double[numClasses]; int bestIndex; double sum = 0; for (int i = 0; i < baggedClassifiers.length; i++) { Labeling labeling = baggedClassifiers[i].classify(inst).getLabeling(); labeling.addTo (scores); } DenseVector.normalize (scores); return new Classification (inst, this, new LabelVector (getLabelAlphabet(), scores)); }
public Instance pipe (Instance carrier) { NodeClusterPair pair = (NodeClusterPair)carrier.getData(); Citation node = (Citation)pair.getNode(); Collection cluster = (Collection)pair.getCluster(); Iterator iter = cluster.iterator (); int numNeg = 0; while (iter.hasNext()) { Citation c = (Citation) iter.next(); NodePair np = new NodePair (c, node); Instance inst = new Instance (np, "unknown", null, np, classifier.getInstancePipe()); Labeling labeling = classifier.classify (inst).getLabeling(); if (labeling.labelAtLocation(0).toString().equals("no")) { if (++numNeg >= n) break; } } if (numNeg >= n) pair.setFeatureValue ("ClusterContainsAtLeast"+n+"NegativeNodes", 1.0); return carrier; } }