本文整理汇总了Java中weka.classifiers.meta.FilteredClassifier.classifyInstance方法的典型用法代码示例。如果您正苦于以下问题:Java FilteredClassifier.classifyInstance方法的具体用法?Java FilteredClassifier.classifyInstance怎么用?Java FilteredClassifier.classifyInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.classifiers.meta.FilteredClassifier
的用法示例。
在下文中一共展示了FilteredClassifier.classifyInstance方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildFilteredClassifier
import weka.classifiers.meta.FilteredClassifier; //导入方法依赖的package包/类
public void buildFilteredClassifier(){
rf = new RandomForest();
Remove rm = new Remove();
rm.setAttributeIndices("1");
FilteredClassifier fc = new FilteredClassifier();
fc.setFilter(rm);
fc.setClassifier(rf);
try{
fc.buildClassifier(weather);
for (int i = 0; i < weather.numInstances(); i++){
double pred = fc.classifyInstance(weather.instance(i));
System.out.print("given value: " + weather.classAttribute().value((int) weather.instance(i).classValue()));
System.out.println("---predicted value: " + weather.classAttribute().value((int) pred));
}
} catch (Exception e) {
}
}
示例2: classify
import weka.classifiers.meta.FilteredClassifier; //导入方法依赖的package包/类
/**
* Classifies function wise test instances in the associated with the names labels mentioned in the arraylist passed as the argument.
*
* @param list - labels of instances contained in the test set that need to be classified.
* @return TreeMap containing the instance labels and the associated classification results.
* @throws ClassificationFailedException
*/
@Override
public LinkedHashMap<String, String> classify(LinkedList<String> list) throws ClassificationFailedException {
output = new LinkedHashMap<String, String>();
J48 j48 = new J48();
Remove rm = new Remove();
rm.setAttributeIndices("1");
FilteredClassifier fc = new FilteredClassifier();
fc.setFilter(rm);
fc.setClassifier(j48);
try {
fc.buildClassifier(trainSet);
for (int i = 0; i < testSet.numInstances(); i++) {
double pred = fc.classifyInstance(testSet.instance(i));
if (list.isEmpty()) {
output.put(String.valueOf(i + 1), testSet.classAttribute().value((int) pred));
} else {
output.put(list.get(i), testSet.classAttribute().value((int) pred));
}
}
} catch (Exception ex) {
throw new ClassificationFailedException();
}
return output;
}
示例3: classify
import weka.classifiers.meta.FilteredClassifier; //导入方法依赖的package包/类
/**
* Classifies whole program test instances,
*
* @return String containing the classification result of the evaluated program's dataset.
* @throws ClassificationFailedException
*/
@Override
public Object classify() throws ClassificationFailedException {
J48 j48 = new J48();
Remove rm = new Remove();
String output = null;
rm.setAttributeIndices("1");
FilteredClassifier fc = new FilteredClassifier();
fc.setFilter(rm);
fc.setClassifier(j48);
try {
fc.buildClassifier(trainSet);
this.treeModel = j48.toString();
double pred = fc.classifyInstance(testSet.instance(0));
output = testSet.classAttribute().value((int) pred);
classificationResult = output;
} catch (Exception ex) {
throw new ClassificationFailedException();
}
return output;
}
示例4: classify
import weka.classifiers.meta.FilteredClassifier; //导入方法依赖的package包/类
/**
* Classifies Timesliced test data instances.
*
* @return Resulting linked list with timelsiced classification results.
* @throws ClassificationFailedException
*/
@Override
public Object classify() throws ClassificationFailedException {
output = new LinkedList<String>();
J48 j48 = new J48();
Remove rm = new Remove();
rm.setAttributeIndices("1");
FilteredClassifier fc = new FilteredClassifier();
fc.setFilter(rm);
fc.setClassifier(j48);
try {
fc.buildClassifier(trainSet);
for (int i = 0; i < testSet.numInstances(); i++) {
//System.out.println(testSet.instance(i));
double pred = fc.classifyInstance(testSet.instance(i));
output.add(testSet.classAttribute().value((int) pred));
}
} catch (Exception ex) {
System.out.println(ex.toString());
throw new ClassificationFailedException();
}
return output;
}
示例5: classifyDisagreedOnAgreed
import weka.classifiers.meta.FilteredClassifier; //导入方法依赖的package包/类
/**
* Classifies the list of testing set(disagreed items) with the trained model of the training set(total of agreed items)
* @param training the set of agreed items
* @param testing the set of disagreed items
* @param pointer value of 0 if the Instances are of Item type or 1 if the Instances are of User type
* @throws Exception
*/
public void classifyDisagreedOnAgreed(Instances training, Instances testing, int pointer) throws Exception {
/**classify disagreed building model on the agreed**/
//define the filtered classifier
FilteredClassifier fc = new FilteredClassifier();
RandomForest tree = new RandomForest();
//remove the id attribute
Remove rm = new Remove();
rm.setAttributeIndices("1");
//set the remove filter
fc.setFilter(rm);
//set the the classifier type
fc.setClassifier(tree);
//build the model
try {
fc.buildClassifier(training);
} catch (Exception e) {
e.printStackTrace();
}
int counter2 = 0, counterFake=0, counterReal=0;
//iterate through the testing set and predict with the model formed before
for (int i=0; i<testing.size(); i++) {
double pred = fc.classifyInstance(testing.instance(i));
//System.out.print("ID: " + testing.instance(i).stringValue(0));
String actual = testing.classAttribute().value((int) testing.instance(i).classValue());
//System.out.print(", actual: " + testing.classAttribute().value((int) testing.instance(i).classValue()));
String predicted = testing.classAttribute().value((int) pred);
//System.out.println(", predicted: " + testing.classAttribute().value((int) pred));
//compare the actual and the predicted values of each instance
if (actual.equals(predicted)) {
counter2++;
if (actual.equals("fake")) {
counterFake++;
}
else {
counterReal++;
}
}
}
//print info
System.out.println();
System.out.println("DISAGREED CLASSIFICATION BASED ON THE AGREED");
System.out.println("Testing with a training set:");
System.out.println("Disagreed accuracy "+ (double)counter2/testing.size()*100 );
System.out.println("Fake items predicted right "+counterFake);
System.out.println("Real items predicted right "+counterReal);
System.out.println("Testing with bagging:");
int trainingSize = training.size()/5;
//apply bagging with training set the agreed items
if (pointer==0) {
Classifier[] itemCls = Bagging.createClassifiers(training, testing, trainingSize);
Bagging.classifyItems(itemCls,Bagging.getTestingSets());
}
else {
Classifier[] userCls = Bagging.createClassifiersUser(training, testing, trainingSize);
Bagging.classifyItems(userCls,Bagging.getTestingSetsUser());
}
/**end of classify disagreed building model on the agreed**/
}