本文整理汇总了Java中cc.mallet.classify.Classification类的典型用法代码示例。如果您正苦于以下问题:Java Classification类的具体用法?Java Classification怎么用?Java Classification使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Classification类属于cc.mallet.classify包,在下文中一共展示了Classification类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ConfusionMatrix
import cc.mallet.classify.Classification; //导入依赖的package包/类
/**
* 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]++;
}
}
示例2: accuracyAtCoverage
import cc.mallet.classify.Classification; //导入依赖的package包/类
/**
* accuracy at a given coverage percentage
* @param cov coverage percentage
* @return accuracy value
*/
public double accuracyAtCoverage(double cov)
{
assert(cov <= 1 && cov > 0);
int numTrials = (int)(Math.round((double)classifications.size()*cov));
int numCorrect = 0;
// System.out.println("NumTrials="+numTrials);
for(int i= classifications.size()-1;
i >= classifications.size()-numTrials; i--)
{
Classification temp = (Classification)classifications.get(i);
if(temp.bestLabelIsCorrect())
numCorrect++;
}
// System.out.println("Accuracy at cov "+cov+" is "+
//(double)numCorrect/numTrials);
return((double)numCorrect/numTrials);
}
示例3: ConfusionMatrix
import cc.mallet.classify.Classification; //导入依赖的package包/类
/**
* 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]++;
}
}
示例4: classify
import cc.mallet.classify.Classification; //导入依赖的package包/类
@Override
public Classification classify(Instance instance) {
SparseVector vector = SVMClassifierTrainer.getVector(instance);
double[] scores = new double[model.nr_class];
double sLabel = mltLabel2svmLabel.get(instance.getTarget().toString());//(getLabelAlphabet().lookupLabel(instance.getTarget().toString()));
double p = SVMPredictor.predictProbability(new ca.uwo.csd.ai.nlp.libsvm.ex.Instance(sLabel, vector), model, scores);
//if SVM is not predicting probability then assign a score of 1.0 to the best class(p)
//and 0.0 to the other classes
if (!predictProbability) {
String label = svmLabel2mltLabel.get(p);
int index = getLabelAlphabet().lookupIndex(label.toString(), false);
scores[index] = 1.0;
} else {
rearrangeScores(scores);
}
Classification classification = new Classification(instance, this,
new LabelVector(getLabelAlphabet(), scores));
return classification;
}
示例5: applyModel
import cc.mallet.classify.Classification; //导入依赖的package包/类
@Override
public List<ModelApplication> applyModel(
AnnotationSet instanceAS, AnnotationSet inputAS, AnnotationSet sequenceAS, String parms) {
// NOTE: the crm should be of type CorpusRepresentationMalletClass for this to work!
if(!(corpusRepresentation instanceof CorpusRepresentationMalletTarget)) {
throw new GateRuntimeException("Cannot perform classification with data from "+corpusRepresentation.getClass());
}
CorpusRepresentationMalletTarget data = (CorpusRepresentationMalletTarget)corpusRepresentation;
data.stopGrowth();
List<ModelApplication> gcs = new ArrayList<ModelApplication>();
LFPipe pipe = (LFPipe)data.getRepresentationMallet().getPipe();
Classifier classifier = (Classifier)model;
// iterate over the instance annotations and create mallet instances
for(Annotation instAnn : instanceAS.inDocumentOrder()) {
Instance inst = data.extractIndependentFeatures(instAnn, inputAS);
inst = pipe.instanceFrom(inst);
Classification classification = classifier.classify(inst);
Labeling labeling = classification.getLabeling();
LabelVector labelvec = labeling.toLabelVector();
List<String> classes = new ArrayList<String>(labelvec.numLocations());
List<Double> confidences = new ArrayList<Double>(labelvec.numLocations());
for(int i=0; i<labelvec.numLocations(); i++) {
classes.add(labelvec.getLabelAtRank(i).toString());
confidences.add(labelvec.getValueAtRank(i));
}
ModelApplication gc = new ModelApplication(instAnn, labeling.getBestLabel().toString(),
labeling.getBestValue(), classes, confidences);
//System.err.println("ADDING GC "+gc);
// now save the class in our special class feature on the instance as well
instAnn.getFeatures().put("gate.LF.target",labeling.getBestLabel().toString());
gcs.add(gc);
}
data.startGrowth();
return gcs;
}
示例6: getLabelVectorsFromClassifications
import cc.mallet.classify.Classification; //导入依赖的package包/类
private static LabelVector[] getLabelVectorsFromClassifications (Classification[] c)
{
LabelVector[] ret = new LabelVector[c.length];
for (int i = 0; i < c.length; i++)
ret[i] = c[i].getLabelVector();
return ret;
}
示例7: add
import cc.mallet.classify.Classification; //导入依赖的package包/类
/**
* Adds classification results to the ROC data
*
* @param trial Trial results to add to ROC data
*/
public void add(Classification classification) {
int correctIndex = classification.getInstance().getLabeling().getBestIndex();
LabelVector lv = classification.getLabelVector();
double[] values = lv.getValues();
if (!Alphabet.alphabetsMatch(this, lv)) {
throw new IllegalArgumentException ("Alphabets do not match");
}
int numLabels = this.labelAlphabet.size();
for (int label = 0; label < numLabels; label++) {
double labelValue = values[label];
int[][] thresholdCounts = this.counts[label];
int threshold = 0;
// add the trial to all the thresholds it would be positive for
for (; threshold < this.thresholds.length && labelValue >= this.thresholds[threshold]; threshold++) {
if (correctIndex == label) {
thresholdCounts[threshold][TRUE_POSITIVE]++;
} else {
thresholdCounts[threshold][FALSE_POSITIVE]++;
}
}
// add the trial to the thresholds it would be negative for
for (; threshold < this.thresholds.length; threshold++) {
if (correctIndex == label) {
thresholdCounts[threshold][FALSE_NEGATIVE]++;
} else {
thresholdCounts[threshold][TRUE_NEGATIVE]++;
}
}
}
}
示例8: pipe
import cc.mallet.classify.Classification; //导入依赖的package包/类
/**
* Add the token classifier's predictions as features to the instance.
* This method assumes the input instance contains FeatureVectorSequence as data
*/
public Instance pipe(Instance carrier)
{
FeatureVectorSequence fvs = (FeatureVectorSequence) carrier.getData();
InstanceList ilist = convert(carrier, (Noop) m_tokenClassifiers.getInstancePipe());
assert (fvs.size() == ilist.size());
// For passing instances to the token classifier, each instance's data alphabet needs to
// match that used by the token classifier at training time. For the resulting piped
// instance, each instance's data alphabet needs to contain token classifier's prediction
// as features
FeatureVector[] fva = new FeatureVector[fvs.size()];
for (int i = 0; i < ilist.size(); i++) {
Instance inst = ilist.get(i);
Classification c = m_tokenClassifiers.classify(inst, ! m_inProduction);
LabelVector lv = c.getLabelVector();
AugmentableFeatureVector afv1 = (AugmentableFeatureVector) inst.getData();
int[] indices = afv1.getIndices();
AugmentableFeatureVector afv2 = new AugmentableFeatureVector(m_dataAlphabet,
indices, afv1.getValues(), indices.length + m_predRanks2add.length);
for (int j = 0; j < m_predRanks2add.length; j++) {
Label label = lv.getLabelAtRank(m_predRanks2add[j]);
int idx = m_dataAlphabet.lookupIndex("TOK_PRED=" + label.toString() + "[email protected]_RANK_" + m_predRanks2add[j]);
assert(idx >= 0);
afv2.add(idx, 1);
}
fva[i] = afv2;
}
carrier.setData(new FeatureVectorSequence(fva));
return carrier;
}
示例9: classify
import cc.mallet.classify.Classification; //导入依赖的package包/类
/**
*
* @param instance the instance to classify
* @param useOutOfFold whether to check the instance name and use the out-of-fold classifier
* if the instance name matches one in the training data
* @return the token classifier's output
*/
public Classification classify(Instance instance, boolean useOutOfFold)
{
Object instName = instance.getName();
if (! useOutOfFold || ! m_table.containsKey(instName))
return m_tokenClassifier.classify(instance);
Classifier classifier = (Classifier) m_table.get(instName);
return classifier.classify(instance);
}
示例10: compare
import cc.mallet.classify.Classification; //导入依赖的package包/类
public final int compare (Object a, Object b)
{
LabelVector x = (LabelVector) (((Classification)a).getLabelVector());
LabelVector y = (LabelVector) (((Classification)b).getLabelVector());
double difference = x.getBestValue() - y.getBestValue();
int toReturn = 0;
if(difference > 0)
toReturn = 1;
else if (difference < 0)
toReturn = -1;
return(toReturn);
}
示例11: classify
import cc.mallet.classify.Classification; //导入依赖的package包/类
public Classification classify (Instance instance)
{
FeatureVector fv = (FeatureVector) instance.getData ();
assert (instancePipe == null || fv.getAlphabet () == this.instancePipe.getDataAlphabet ());
Node leaf = getLeaf(m_root, fv);
return new Classification (instance, this, leaf.getGainRatio().getBaseLabelDistribution());
}
示例12: classify
import cc.mallet.classify.Classification; //导入依赖的package包/类
/**
* Classify an instance using random selection based on the trained data.
*
* @param Instance to be classified. Data field must be a FeatureVector.
* @return Classification containing the labeling of the instance.
*/
@Override
public Classification classify(Instance instance) {
int max = this.labels.size() - 1;
Random random = new Random();
int rndIndex = random.nextInt(max + 1);
Label randomLabel = this.labels.get(rndIndex);
Classification randomClassification = new Classification(instance, this, randomLabel);
return randomClassification;
}
示例13: classify
import cc.mallet.classify.Classification; //导入依赖的package包/类
/**
* Classify an instance using the most frequent class label.
*
* @param Instance to be classified. Data field must be a FeatureVector.
* @return Classification containing the labeling of the instance.
*/
@Override
public Classification classify(Instance instance) {
String mostFrequentLabelStr = this.sortedLabelMap.firstEntry().getKey();
Label mostFrequentLabel = this.labels.get(mostFrequentLabelStr);
Classification mostFrequentClassification = new Classification(instance, this, mostFrequentLabel);
return mostFrequentClassification;
}
示例14: classify
import cc.mallet.classify.Classification; //导入依赖的package包/类
@Override
public Classification classify(Instance instance) {
//TODO: find a better approach
KernelManager.setCustomKernel(this.kernel);
SparseVector vector = SVMClassifierTrainer.getVector(instance);
double[] scores = new double[model.nr_class];
String targetLabel = getLabelAlphabet().lookupLabel(instance.getTarget().toString()).toString();
// System.out.println("targetLabel is " + targetLabel);
// for (Entry<String, Double> entry : mltLabel2svmLabel.entrySet()) {
// System.out.println(entry.getKey() + "==>" + entry.getValue());
// }
double sLabel = mltLabel2svmLabel.get(targetLabel);
double p = SVMPredictor.predictProbability(new ruc.irm.wikit.nlp.libsvm.ex.Instance(sLabel, vector), model, scores);
//if SVM is not predicting probability then assign a score of 1.0 to the best class(p)
//and 0.0 to the other classes
if (!predictProbability) {
String label = svmLabel2mltLabel.get(p);
int index = getLabelAlphabet().lookupIndex(label, false);
scores[index] = 1.0;
} else {
rearrangeScores(scores);
}
Classification classification = new Classification(instance, this,
new LabelVector(getLabelAlphabet(), scores));
return classification;
}
示例15: eval
import cc.mallet.classify.Classification; //导入依赖的package包/类
public double[] eval(String[] features) {
Alphabet dataAlphabet = classifer.getAlphabet();
List<Integer> malletFeatureList = new ArrayList<>(features.length);
for (String feature : features) {
int featureId = dataAlphabet.lookupIndex(feature);
if (featureId != -1) {
malletFeatureList.add(featureId);
}
}
int malletFeatures[] = new int[malletFeatureList.size()];
for (int i = 0; i < malletFeatureList.size(); i++) {
malletFeatures[i] = malletFeatureList.get(i);
}
FeatureVector fv = new FeatureVector(classifer.getAlphabet(),
malletFeatures);
Instance instance = new Instance(fv, null, null, null);
Classification result = classifer.classify(instance);
LabelVector labeling = result.getLabelVector();
LabelAlphabet targetAlphabet = classifer.getLabelAlphabet();
double outcomes[] = new double[targetAlphabet.size()];
for (int i = 0; i < outcomes.length; i++) {
Label label = targetAlphabet.lookupLabel(i);
int rank = labeling.getRank(label);
outcomes[i] = labeling.getValueAtRank(rank);
}
return outcomes;
}