cc.mallet.types.Instance.getLabeling()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(124)

本文整理了Java中cc.mallet.types.Instance.getLabeling()方法的一些代码示例,展示了Instance.getLabeling()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Instance.getLabeling()方法的具体详情如下:
包路径:cc.mallet.types.Instance
类名称:Instance
方法名:getLabeling

Instance.getLabeling介绍

暂无

代码示例

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

public double valueOfCorrectLabel ()
{
  Labeling correctLabeling = instance.getLabeling();
  int correctLabelIndex = correctLabeling.getBestIndex();
  return labeling.value (correctLabelIndex);
}

代码示例来源:origin: com.github.steveash.mallet/mallet

public double valueOfCorrectLabel ()
{
  Labeling correctLabeling = instance.getLabeling();
  int correctLabelIndex = correctLabeling.getBestIndex();
  return labeling.value (correctLabelIndex);
}

代码示例来源:origin: cc.mallet/mallet

public double valueOfCorrectLabel ()
{
  Labeling correctLabeling = instance.getLabeling();
  int correctLabelIndex = correctLabeling.getBestIndex();
  return labeling.value (correctLabelIndex);
}

代码示例来源:origin: cc.mallet/mallet

public boolean bestLabelIsCorrect ()
{
  Labeling correctLabeling = instance.getLabeling();
  if (correctLabeling == null)
    throw new IllegalStateException ("Instance has no label.");
  return (labeling.getBestLabel().equals (correctLabeling.getBestLabel()));
}

代码示例来源:origin: com.github.steveash.mallet/mallet

public boolean bestLabelIsCorrect ()
{
  Labeling correctLabeling = instance.getLabeling();
  if (correctLabeling == null)
    throw new IllegalStateException ("Instance has no label.");
  return (labeling.getBestLabel().equals (correctLabeling.getBestLabel()));
}

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

public boolean bestLabelIsCorrect ()
{
  Labeling correctLabeling = instance.getLabeling();
  if (correctLabeling == null)
    throw new IllegalStateException ("Instance has no label.");
  return (labeling.getBestLabel().equals (correctLabeling.getBestLabel()));
}

代码示例来源:origin: cc.mallet/mallet

public static Clustering mergeInstancesWithSameLabel (Clustering clustering) {
  InstanceList list = clustering.getInstances();
  for (int i = 0; i < list.size(); i++) {
    Instance ii = list.get(i);
    int li = clustering.getLabel(i);
    for (int j = i + 1; j < list.size(); j++) {
      Instance ij = list.get(j);
      int lj = clustering.getLabel(j);
      if (li != lj && ii.getLabeling().equals(ij.getLabeling()))
        clustering = ClusterUtils.mergeClusters(clustering, li, lj);
    }
  }    
  return clustering;
}

代码示例来源:origin: com.github.steveash.mallet/mallet

public static Clustering mergeInstancesWithSameLabel (Clustering clustering) {
  InstanceList list = clustering.getInstances();
  for (int i = 0; i < list.size(); i++) {
    Instance ii = list.get(i);
    int li = clustering.getLabel(i);
    for (int j = i + 1; j < list.size(); j++) {
      Instance ij = list.get(j);
      int lj = clustering.getLabel(j);
      if (li != lj && ii.getLabeling().equals(ij.getLabeling()))
        clustering = ClusterUtils.mergeClusters(clustering, li, lj);
    }
  }    
  return clustering;
}

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

public static Clustering mergeInstancesWithSameLabel (Clustering clustering) {
  InstanceList list = clustering.getInstances();
  for (int i = 0; i < list.size(); i++) {
    Instance ii = list.get(i);
    int li = clustering.getLabel(i);
    for (int j = i + 1; j < list.size(); j++) {
      Instance ij = list.get(j);
      int lj = clustering.getLabel(j);
      if (li != lj && ii.getLabeling().equals(ij.getLabeling()))
        clustering = ClusterUtils.mergeClusters(clustering, li, lj);
    }
  }    
  return clustering;
}

代码示例来源:origin: cc.mallet/mallet

/**
 * 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]++;
  }            
}

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

/**
 * 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]++;
  }            
}

代码示例来源:origin: com.github.steveash.mallet/mallet

/**
 * 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]++;
  }            
}

代码示例来源:origin: cc.mallet/mallet

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);
 }
}

代码示例来源:origin: com.github.steveash.mallet/mallet

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);
 }
}

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

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);
 }
}

代码示例来源:origin: cc.mallet/mallet

public void testRandomTrained ()
{
  InstanceList ilist = new InstanceList (new Randoms(1), 10, 2);
  Classifier c = new NaiveBayesTrainer ().train (ilist);
  // test on the training data
  int numCorrect = 0;
  for (int i = 0; i < ilist.size(); i++) {
    Instance inst = ilist.get(i);
    Classification cf = c.classify (inst);
    cf.print ();
    if (cf.getLabeling().getBestLabel() == inst.getLabeling().getBestLabel())
      numCorrect++;
  }
  System.out.println ("Accuracy on training set = " + ((double)numCorrect)/ilist.size());
}

代码示例来源:origin: com.github.steveash.mallet/mallet

public void testRandomTrained ()
{
  InstanceList ilist = new InstanceList (new Randoms(1), 10, 2);
  Classifier c = new NaiveBayesTrainer ().train (ilist);
  // test on the training data
  int numCorrect = 0;
  for (int i = 0; i < ilist.size(); i++) {
    Instance inst = ilist.get(i);
    Classification cf = c.classify (inst);
    cf.print ();
    if (cf.getLabeling().getBestLabel() == inst.getLabeling().getBestLabel())
      numCorrect++;
  }
  System.out.println ("Accuracy on training set = " + ((double)numCorrect)/ilist.size());
}

代码示例来源:origin: cc.mallet/mallet

public double dataLogLikelihood (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)
   logLikelihood += instanceWeight * dataLogProbability (inst, labeling.getBestIndex());
  else {
   Labeling predicted = this.classify(inst).getLabeling();
   //System.err.println ("label = \n"+labeling);
   //System.err.println ("predicted = \n"+predicted);
   for (int lpos = 0; lpos < predicted.numLocations(); lpos++) {
    int li = predicted.indexAtLocation (lpos);
    double labelWeight = predicted.valueAtLocation (lpos);
    //System.err.print (", "+labelWeight);
    if (labelWeight == 0) continue;
    logLikelihood += instanceWeight * labelWeight * dataLogProbability (inst, li);
   }
  }
 }
 return logLikelihood;
}

代码示例来源:origin: com.github.steveash.mallet/mallet

public double dataLogLikelihood (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)
   logLikelihood += instanceWeight * dataLogProbability (inst, labeling.getBestIndex());
  else {
   Labeling predicted = this.classify(inst).getLabeling();
   //System.err.println ("label = \n"+labeling);
   //System.err.println ("predicted = \n"+predicted);
   for (int lpos = 0; lpos < predicted.numLocations(); lpos++) {
    int li = predicted.indexAtLocation (lpos);
    double labelWeight = predicted.valueAtLocation (lpos);
    //System.err.print (", "+labelWeight);
    if (labelWeight == 0) continue;
    logLikelihood += instanceWeight * labelWeight * dataLogProbability (inst, li);
   }
  }
 }
 return logLikelihood;
}

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

public double dataLogLikelihood (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)
   logLikelihood += instanceWeight * dataLogProbability (inst, labeling.getBestIndex());
  else {
   Labeling predicted = this.classify(inst).getLabeling();
   //System.err.println ("label = \n"+labeling);
   //System.err.println ("predicted = \n"+predicted);
   for (int lpos = 0; lpos < predicted.numLocations(); lpos++) {
    int li = predicted.indexAtLocation (lpos);
    double labelWeight = predicted.valueAtLocation (lpos);
    //System.err.print (", "+labelWeight);
    if (labelWeight == 0) continue;
    logLikelihood += instanceWeight * labelWeight * dataLogProbability (inst, li);
   }
  }
 }
 return logLikelihood;
}

相关文章