如何在WEKA中进行交叉验证后打印预测的类

一旦使用分类器进行了10次交叉验证,如何打印出每个实例的预测类以及这些实例的分布情况?如何在WEKA中进行交叉验证后打印预测的类

J48 j48 = new J48(); 

Evaluation eval = new Evaluation(newData);

eval.crossValidateModel(j48, newData, 10, new Random(1));

当我想类似的东西下面,它说,分类未建

for (int i=0; i<data.numInstances(); i++){ 

System.out.println(j48.distributionForInstance(newData.instance(i)));

}

我试图做的是相同的功能在WEKA GUI,其中一次分类器进行训练,我可以点击Visualize classifier error" > Save,我会发现文件中预测的类。但现在我需要它来使用我自己的Java代码。


我已经试过类似如下:

J48 j48 = new J48(); 

Evaluation eval = new Evaluation(newData);

StringBuffer forPredictionsPrinting = new StringBuffer();

weka.core.Range attsToOutput = null;

Boolean outputDistribution = new Boolean(true);

eval.crossValidateModel(j48, newData, 10, new Random(1), forPredictionsPrinting, attsToOutput, outputDistribution);

然而,它会提示我的错误:

Exception in thread "main" java.lang.ClassCastException: java.lang.StringBuffer cannot be cast to weka.classifiers.evaluation.output.prediction.AbstractOutput 

回答:

crossValidateModel()方法可以采取forPredictionsPrintingvarargs参数是weka.classifiers.evaluation.output.prediction.AbstractOutput实例。

其中重要的部分是一个StringBuffer来保存所有预测的字符串表示。以下代码未经测试JRuby,但您应该可以将其转换为您的需要。

j48 = j48.new 

eval = Evalution.new(newData)

predictions = java.lange.StringBuffer.new

eval.crossValidateModel(j48, newData, 10, Random.new(1), predictions, Range.new('1'), true)

# variable predictions now hold a string of all the individual predictions

回答:

我前几天被卡住了。我想使用矩阵来评估matlab中的Weka分类器,而不是从arf​​f文件加载。我使用http://www.mathworks.com/matlabcentral/fileexchange/21204-matlab-weka-interface和以下源代码。我希望这可以帮助别人。

import weka.classifiers.*; 

import java.util.*

wekaClassifier = javaObject('weka.classifiers.trees.J48');

wekaClassifier.buildClassifier(processed);%Loaded from loadARFF

e = javaObject('weka.classifiers.Evaluation',processed);%Loaded from loadARFF

myrand = Random(1);

plainText = javaObject('weka.classifiers.evaluation.output.prediction.PlainText');

buffer = javaObject('java.lang.StringBuffer');

plainText.setBuffer(buffer)

bool = javaObject('java.lang.Boolean',true);

range = javaObject('weka.core.Range','1');

array = javaArray('java.lang.Object',3);

array(1) = plainText;

array(2) = range;

array(3) = bool;

e.crossValidateModel(wekaClassifier,testing,10,myrand,array)

e.toClassDetailsString

阿斯德鲁瓦尔洛佩斯洲

回答:

clc 

clear

%Load from disk

fileDataset = 'cm1.arff';

myPath = 'C:\Users\Asdrubal\Google Drive\Respaldo\DoctoradoALCPC\Doctorado ALC PC\AlcMobile\AvTh\MyPapers\Papers2014\UnderOverSampling\data\Skewed\datasetsKeel\';

javaaddpath('C:\Users\Asdrubal\Google Drive\Respaldo\DoctoradoALCPC\Doctorado ALC PC\AlcMobile\JarsForExperiments\weka.jar');

wekaOBJ = loadARFF([myPath fileDataset]);

%Transform from data into Matlab

[data, featureNames, targetNDX, stringVals, relationName] = ...

weka2matlab(wekaOBJ,'[]');

%Create testing and training sets in matlab format (this can be improved)

[tam, dim] = size(data);

idx = randperm(tam);

testIdx = idx(1 : tam*0.3);

trainIdx = idx(tam*0.3 + 1:end);

trainSet = data(trainIdx,:);

testSet = data(testIdx,:);

%Trasnform the training and the testing sets into the Weka format

testingWeka = matlab2weka('testing', featureNames, testSet);

trainingWeka = matlab2weka('training', featureNames, trainSet);

%Now evaluate classifier

import weka.classifiers.*;

import java.util.*

wekaClassifier = javaObject('weka.classifiers.trees.J48');

wekaClassifier.buildClassifier(trainingWeka);

e = javaObject('weka.classifiers.Evaluation',trainingWeka);

myrand = Random(1);

plainText = javaObject('weka.classifiers.evaluation.output.prediction.PlainText');

buffer = javaObject('java.lang.StringBuffer');

plainText.setBuffer(buffer)

bool = javaObject('java.lang.Boolean',true);

range = javaObject('weka.core.Range','1');

array = javaArray('java.lang.Object',3);

array(1) = plainText;

array(2) = range;

array(3) = bool;

e.crossValidateModel(wekaClassifier,testingWeka,10,myrand,array)%U

e.toClassDetailsString

以上是 如何在WEKA中进行交叉验证后打印预测的类 的全部内容, 来源链接: utcz.com/qa/264279.html

回到顶部