本文整理匯總了Java中weka.classifiers.Classifier.classifyInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java Classifier.classifyInstance方法的具體用法?Java Classifier.classifyInstance怎麽用?Java Classifier.classifyInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類weka.classifiers.Classifier
的用法示例。
在下文中一共展示了Classifier.classifyInstance方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: predictExamples
import weka.classifiers.Classifier; //導入方法依賴的package包/類
@TimeThis(task="prediction")
protected void predictExamples(ProcessingContext<Corpus> ctx, Classifier classifier, IdentifiedInstances<Element> devSet, Corpus corpus) throws Exception {
ElementClassifierResolvedObjects resObj = getResolvedObjects();
RelationDefinition relationDefinition = resObj.getRelationDefinition();
Evaluator examples = resObj.getExamples();
String predictedClassFeatureKey = getPredictedClassFeatureKey();
TargetStream evaluationFile = getEvaluationFile();
boolean withId = evaluationFile != null;
String[] classes = getClasses(devSet);
getLogger(ctx).info("predicting class for each example");
EvaluationContext evalCtx = new EvaluationContext(getLogger(ctx));
for (Element example : Iterators.loop(getExamples(corpus, examples, evalCtx))) {
Instance inst = relationDefinition.addExample(devSet, evalCtx, example, withId, withId);
double prediction = classifier.classifyInstance(inst);
example.addFeature(predictedClassFeatureKey, classes[(int) prediction]);
if (!withId)
devSet.delete();
}
}
示例2: predictOneFlow
import weka.classifiers.Classifier; //導入方法依賴的package包/類
private static boolean predictOneFlow(String line, String domainOS) {
if (!domainOSModel.containsKey(domainOS))
return false;
else {
try {
Classifier classifier = domainOSModel.get(domainOS);
Map<String, Integer> fi = domainOSFeature.get(domainOS);
Instances structure = domainOSStruct.get(domainOS);
Instance current = getInstance(line, fi, fi.size());
Instances is = new Instances(structure);
is.setClassIndex(is.numAttributes() - 1);
is.add(current);
current = is.get(is.size() - 1);
current.setClassMissing();
double predicted = classifier.classifyInstance(current);
if (predicted > 0) {
return true;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
示例3: predicting
import weka.classifiers.Classifier; //導入方法依賴的package包/類
public void predicting(String testFile) throws Exception {
BufferedReader reader = new BufferedReader(
new FileReader(testFile));
Instances inst = new Instances(reader);
reader.close();
inst.setClassIndex(inst.numAttributes() - 1);
// deserialize model
Classifier cls = (Classifier) weka.core.SerializationHelper.read(modelFile);
double sum = inst.numInstances(), right = 0.0f;
for(int i = 0;i<sum;i++)//測試分類結果
{
if(cls.classifyInstance(inst.instance(i))==inst.instance(i).classValue())//如果預測值和答案值相等(測試語料中的分類列提供的須為正確答案,結果才有意義)
{
right++;//正確值加1
}
System.out.println(cls.classifyInstance(inst.instance(i)) + " " +inst.instance(i).classValue());
}
System.out.println("J48 classification precision:"+(right/sum));
}
示例4: classifyInstance
import weka.classifiers.Classifier; //導入方法依賴的package包/類
/**
* Classify an instance.
*
* @param inst the instance to predict
* @return a prediction for the instance
* @throws Exception if an error occurs
*/
public double classifyInstance(Instance inst) throws Exception {
double prediction = m_zeroR.classifyInstance(inst);
// default model?
if (!m_SuitableData) {
return prediction;
}
for (Classifier classifier : m_Classifiers) {
double toAdd = classifier.classifyInstance(inst);
if (Utils.isMissingValue(toAdd)) {
throw new UnassignedClassException("AdditiveRegression: base learner predicted missing value.");
}
toAdd *= getShrinkage();
prediction += toAdd;
}
return prediction;
}
示例5: residualReplace
import weka.classifiers.Classifier; //導入方法依賴的package包/類
/**
* Replace the class values of the instances from the current iteration
* with residuals ater predicting with the supplied classifier.
*
* @param data the instances to predict
* @param c the classifier to use
* @param useShrinkage whether shrinkage is to be applied to the model's output
* @return a new set of instances with class values replaced by residuals
* @throws Exception if something goes wrong
*/
private Instances residualReplace(Instances data, Classifier c,
boolean useShrinkage) throws Exception {
double pred,residual;
Instances newInst = new Instances(data);
for (int i = 0; i < newInst.numInstances(); i++) {
pred = c.classifyInstance(newInst.instance(i));
if (Utils.isMissingValue(pred)) {
throw new UnassignedClassException("AdditiveRegression: base learner predicted missing value.");
}
if (useShrinkage) {
pred *= getShrinkage();
}
residual = newInst.instance(i).classValue() - pred;
newInst.instance(i).setClassValue(residual);
}
// System.err.print(newInst);
return newInst;
}
示例6: testScratch
import weka.classifiers.Classifier; //導入方法依賴的package包/類
double testScratch(Classifier C, Instances data)
{
double[] P = new double[data.instance(0).numClasses()];
double err = 0;
for(int i = 0; i < data.numInstances(); i ++)
{
try{
//Datapoint p = new Datapoint(data.instance(i).toDoubleArray(),(int)data.instance(i))
if(C.classifyInstance(data.instance(i)) != data.instance(i).classValue())
{
err ++;
}//if
P[(int)data.instance(i).classValue()] ++;
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}//for
double MSErr = 0;
for(int i = 0; i < P.length; i ++)
{
P[i] /= (double)data.numInstances();
MSErr += P[i] * (1-P[i]) * (1 - P[i]);
}
return MSErr - err / (double)data.numInstances();
}
示例7: classifyMessage
import weka.classifiers.Classifier; //導入方法依賴的package包/類
public static double[] classifyMessage(Classifier classifier, Instances trainingData,String message) throws Exception {
Instances testset = trainingData.stringFreeStructure();
// Make message into test instance.
Instance instance = makeInstance(message, testset);
// // Filter instance.
// m_Filter.input(instance);
// Instance filteredInstance = m_Filter.output();
// Get index of predicted class value.
Instance filteredInstance = instance;
double predicted = classifier.classifyInstance(filteredInstance);
// Output class value.
System.err.println("Message classified as : " +
trainingData.classAttribute().value((int)predicted));
return classifier.distributionForInstance(filteredInstance);
}
示例8: classifyMessageNaiveBayes
import weka.classifiers.Classifier; //導入方法依賴的package包/類
public double[] classifyMessageNaiveBayes(Classifier classifier, Instances trainingData, Instances instance) throws Exception {
double predicted = classifier.classifyInstance(instance.get(0));
double outcomes[] = classifier.distributionForInstance(instance.get(0));
// Output class value.
System.out.println("NaivieBayes classified as: " + trainingData.classAttribute().value((int) predicted) + " Threshold: bad=" + outcomes[1] + ", good="
+ outcomes[0]);
return outcomes;
}
示例9: classifyMessageSGD
import weka.classifiers.Classifier; //導入方法依賴的package包/類
public double[] classifyMessageSGD(Classifier classifier, Instances trainingData, Instances instance) throws Exception {
double predicted = classifier.classifyInstance(instance.get(0));
double outcomes[] = classifier.distributionForInstance(instance.get(0));
// Output class value.
System.out.println(
"SGD classified as: " + trainingData.classAttribute().value((int) predicted) + " Threshold: bad=" + outcomes[1] + ", good=" + outcomes[0]);
return outcomes;
}
示例10: classifyMessageWithFilter
import weka.classifiers.Classifier; //導入方法依賴的package包/類
public double[] classifyMessageWithFilter(Classifier classifier, Instances trainingData, Filter filter, Instances instance) throws Exception {
Instances instanceFiltered = Filter.useFilter(instance, filter);
instanceFiltered.setClassIndex(0);
double predicted = classifier.classifyInstance(instanceFiltered.get(0));
double outcomes[] = classifier.distributionForInstance(instanceFiltered.get(0));
// Output class value.
System.out.println(classifier.getClass().getName() + " classified as: " + trainingData.classAttribute().value((int) predicted) + ": " + predicted
+ " Threshold: bad=" + outcomes[1] + ", good=" + outcomes[0]);
return outcomes;
}