public boolean bestLabelIsCorrect () { Labeling correctLabeling = instance.getLabeling(); if (correctLabeling == null) throw new IllegalStateException ("Instance has no label."); return (labeling.getBestLabel().equals (correctLabeling.getBestLabel())); }
public Labeling2Matrix(Labeling labeling) { super(1, labeling.getLabelAlphabet().size()); this.labeling = labeling; }
private static void printTrialClassification(Trial trial) { for (Classification c : trial) { Instance instance = c.getInstance(); System.out.print(instance.getName() + " " + instance.getTarget() + " "); Labeling labeling = c.getLabeling(); for (int j = 0; j < labeling.numLocations(); j++){ System.out.print(labeling.getLabelAtRank(j).toString() + ":" + labeling.getValueAtRank(j) + " "); } System.out.println(); } }
public double valueOfCorrectLabel () { Labeling correctLabeling = instance.getLabeling(); int correctLabelIndex = correctLabeling.getBestIndex(); return labeling.value (correctLabelIndex); }
public Instance toInstance() { Instance ret; FeatureVector fv; double[] values = new double[labeling.numLocations()]; int[] indices = new int[labeling.numLocations()]; for(int i = 0; i < labeling.numLocations(); i++){ indices[i] = labeling.indexAtLocation(i); values[i] = labeling.valueAtLocation(i); } fv = new FeatureVector(labeling.getAlphabet(), indices, values); ret = new Instance(fv,null,null,instance.getSource()); return ret; } }
public void print (PrintWriter pw) throws FileNotFoundException { // 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 ("]");*/ pw.print(classifier.getClass().getName()); pw.print(" "); pw.print(instance.getSource() + " "); for (int i = 0; i < labeling.numLocations(); i++) pw.print (labeling.labelAtLocation(i).toString()+"="+labeling.valueAtLocation(i)+" "); pw.println (); }
public double labelLogLikelihood (InstanceList ilist) { double logLikelihood = 0; for (int ii = 0; ii < ilist.size(); ii++) { double instanceWeight = ilist.getInstanceWeight(ii); Instance inst = ilist.get(ii); Labeling labeling = inst.getLabeling (); if (labeling == null) continue; Labeling predicted = this.classify(inst).getLabeling(); //System.err.println ("label = \n"+labeling); //System.err.println ("predicted = \n"+predicted); if (labeling.numLocations() == 1) { logLikelihood += instanceWeight * Math.log (predicted.value(labeling.getBestIndex())); } else { for (int lpos = 0; lpos < labeling.numLocations(); lpos++) { int li = labeling.indexAtLocation (lpos); double labelWeight = labeling.valueAtLocation (lpos); //System.err.print (", "+labelWeight); if (labelWeight == 0) continue; logLikelihood += instanceWeight * labelWeight * Math.log (predicted.value(li)); } } } return logLikelihood; }
private void incorporateOneInstance (Instance instance, double instanceWeight) { Labeling labeling = instance.getLabeling (); if (labeling == null) return; // Handle unlabeled instances by skipping them FeatureVector fv = (FeatureVector) instance.getData (); double oneNorm = fv.oneNorm(); if (oneNorm <= 0) return; // Skip instances that have no features present if (docLengthNormalization > 0) // Make the document have counts that sum to docLengthNormalization // I.e., if 20, it would be as if the document had 20 words. instanceWeight *= docLengthNormalization / oneNorm; assert (instanceWeight > 0 && !Double.isInfinite(instanceWeight)); for (int lpos = 0; lpos < labeling.numLocations(); lpos++) { int li = labeling.indexAtLocation (lpos); double labelWeight = labeling.valueAtLocation (lpos); if (labelWeight == 0) continue; //System.out.println ("NaiveBayesTrainer me.increment "+ labelWeight * instanceWeight); me[li].increment (fv, labelWeight * instanceWeight); // This relies on labelWeight summing to 1 over all labels pe.increment (li, labelWeight * instanceWeight); } }
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 getRecall (Labeling label) { return getRecall (label.getBestIndex()); }
/** Return the average rank of the correct class label as returned by Labeling.getRank(correctLabel) on the predicted Labeling. */ public double getAverageRank () { double rsum = 0; Labeling tmpL; Classification tmpC; Instance tmpI; Label tmpLbl, tmpLbl2; int tmpInt; for(int i = 0; i < this.size(); i++) { tmpC = this.get(i); tmpI = tmpC.getInstance(); tmpL = tmpC.getLabeling(); tmpLbl = (Label)tmpI.getTarget(); tmpInt = tmpL.getRank(tmpLbl); tmpLbl2 = tmpL.getLabelAtRank(0); rsum = rsum + tmpInt; } return rsum/this.size(); }
/** * Constructs matrix and calculates values * @param t the trial to build matrix from */ public ConfusionMatrix(Trial t) { this.trial = t; this.classifications = t; 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 (this.size() == 0) return null; if (!(get(0).getTarget() instanceof Labeling)) throw new IllegalStateException ("Target is not a labeling."); double[] counts = new double[getTargetAlphabet().size()]; for (int i = 0; i < this.size(); i++) { Instance instance = get(i); Labeling l = (Labeling) instance.getTarget(); l.addTo (counts, getInstanceWeight(i)); } return new LabelVector ((LabelAlphabet)getTargetAlphabet(), counts); }
public double getDouble(long row, long column) { return labeling.valueAtLocation((int) column); }
public Alphabet getAlphabet () { return labels[0].getAlphabet(); } public Alphabet[] getAlphabets () { return labels[0].getAlphabets(); }
public Alphabet[] getAlphabets () { return labels[0].getAlphabets(); }
public double labelLogLikelihood (InstanceList ilist) { double logLikelihood = 0; for (int ii = 0; ii < ilist.size(); ii++) { double instanceWeight = ilist.getInstanceWeight(ii); Instance inst = ilist.get(ii); Labeling labeling = inst.getLabeling (); if (labeling == null) continue; Labeling predicted = this.classify(inst).getLabeling(); //System.err.println ("label = \n"+labeling); //System.err.println ("predicted = \n"+predicted); if (labeling.numLocations() == 1) { logLikelihood += instanceWeight * Math.log (predicted.value(labeling.getBestIndex())); } else { for (int lpos = 0; lpos < labeling.numLocations(); lpos++) { int li = labeling.indexAtLocation (lpos); double labelWeight = labeling.valueAtLocation (lpos); //System.err.print (", "+labelWeight); if (labelWeight == 0) continue; logLikelihood += instanceWeight * labelWeight * Math.log (predicted.value(li)); } } } return logLikelihood; }
public void print (PrintWriter pw) throws FileNotFoundException { // 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 ("]");*/ pw.print(classifier.getClass().getName()); pw.print(" "); pw.print(instance.getSource() + " "); for (int i = 0; i < labeling.numLocations(); i++) pw.print (labeling.labelAtLocation(i).toString()+"="+labeling.valueAtLocation(i)+" "); pw.println (); }
public Instance toInstance() { Instance ret; FeatureVector fv; double[] values = new double[labeling.numLocations()]; int[] indices = new int[labeling.numLocations()]; for(int i = 0; i < labeling.numLocations(); i++){ indices[i] = labeling.indexAtLocation(i); values[i] = labeling.valueAtLocation(i); } fv = new FeatureVector(labeling.getAlphabet(), indices, values); ret = new Instance(fv,null,null,instance.getSource()); return ret; } }
private void incorporateOneInstance (Instance instance, double instanceWeight) { Labeling labeling = instance.getLabeling (); if (labeling == null) return; // Handle unlabeled instances by skipping them FeatureVector fv = (FeatureVector) instance.getData (); double oneNorm = fv.oneNorm(); if (oneNorm <= 0) return; // Skip instances that have no features present if (docLengthNormalization > 0) // Make the document have counts that sum to docLengthNormalization // I.e., if 20, it would be as if the document had 20 words. instanceWeight *= docLengthNormalization / oneNorm; assert (instanceWeight > 0 && !Double.isInfinite(instanceWeight)); for (int lpos = 0; lpos < labeling.numLocations(); lpos++) { int li = labeling.indexAtLocation (lpos); double labelWeight = labeling.valueAtLocation (lpos); if (labelWeight == 0) continue; //System.out.println ("NaiveBayesTrainer me.increment "+ labelWeight * instanceWeight); me[li].increment (fv, labelWeight * instanceWeight); // This relies on labelWeight summing to 1 over all labels pe.increment (li, labelWeight * instanceWeight); } }