我使用以下代码来训练/测试一些分类器:
public class WekaTest {
public static BufferedReader readDataFile(String filename)
{
BufferedReader inputReader = null;
try
{
inputReader = new BufferedReader(new FileReader(filename));
}
catch (FileNotFoundException ex)
{
System.err.println("File not found: " + filename);
}
return inputReader;
}
public static Evaluation classify(Classifier model,
Instances trainingSet, Instances testingSet) throws Exception {
Evaluation evaluation = new Evaluation(trainingSet);
model.buildClassifier(trainingSet);
evaluation.evaluateModel(model, testingSet);
return evaluation;
}
public static double calculateAccuracy(FastVector predictions) {
double correct = 0;
for (int i = 0; i < predictions.size(); i++) {
NominalPrediction np = (NominalPrediction) predictions.elementAt(i);
if (np.predicted() == np.actual()) {
correct++;
}
}
return 100 * correct / predictions.size();
}
public static Instances[][] crossValidationSplit(Instances data, int numberOfFolds) {
Instances[][] split = new Instances[2][numberOfFolds];
Random random = new Random();
for (int i = 0; i < numberOfFolds; i++)
{
split[0][i] = data.trainCV(numberOfFolds, i, random);
split[1][i] = data.testCV(numberOfFolds, i);
}
return split;
}
public static void main(String[] args) throws Exception
{
BufferedReader datafile = readDataFile("training_1.arff");
Instances data = new Instances(datafile);
data.setClassIndex(data.numAttributes() - 1);
// Do 10-split cross validation
Instances[][] split = crossValidationSplit(data, 10);
// Separate split into training and testing arrays
Instances[] trainingSplits = split[0];
Instances[] testingSplits = split[1];
// Use a set of classifiers
Classifier[] models = {
// new J48(), // a decision tree
// new PART(),
// new DecisionTable(),//decision table majority classifier
// new DecisionStump(), //one-level decision tree
new NaiveBayes(),
// new AdaBoostM1()
new RandomForest()
// new LMT()
};
// Run for each model
for (int j = 0; j < models.length; j++)
{
// Collect every group of predictions for current model in a FastVector
FastVector predictions = new FastVector();
// For each training-testing split pair, train and test the classifier
for (int i = 0; i < trainingSplits.length; i++)
{
Evaluation validation = classify(models[j], trainingSplits[i], testingSplits[i]);
predictions.appendElements(validation.predictions());
System.out.println(validation.toMatrixString());
// Uncomment to see the summary for each training-testing pair.
// System.out.println(models[j].toString());
// generate curve
ThresholdCurve tc = new ThresholdCurve();
int classIndex = 0;
Instances result = tc.getCurve(validation.predictions(), classIndex);
System.out.println("tPR :"+validation.truePositiveRate(classIndex));
System.out.println("fNR :"+validation.falseNegativeRate(classIndex));
// plot curve
ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
vmc.setROCString("(Area under ROC = " +
Utils.doubleToString(tc.getROCArea(result), 4) + ")");
vmc.setName(result.relationName());
PlotData2D tempd = new PlotData2D(result);
tempd.setPlotName(result.relationName());
tempd.addInstanceNumberAttribute();
// specify which points are connected
boolean[] cp = new boolean[result.numInstances()];
for (int n = 1; n < cp.length; n++)
cp[n] = true;
tempd.setConnectPoints(cp);
// add plot
vmc.addPlot(tempd);
// display curve
String plotName = vmc.getName();
final javax.swing.JFrame jf =
new javax.swing.JFrame("Weka Classifier Visualize: "+plotName);
jf.setSize(500,400);
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(vmc, BorderLayout.CENTER);
jf.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
jf.dispose();
}
});
jf.setVisible(true);
}
// Calculate overall accuracy of current classifier on all splits
double accuracy = calculateAccuracy(predictions);
// Print current classifier's name and accuracy in a complicated,
// but nice-looking way.
System.out.println("Accuracy of " + models[j].getClass().getSimpleName() + ": "
+ String.format("%.2f%%", accuracy)
+ "\n---------------------------------");
}
}
}
arff文件包含描述,后跟20个数据属性,后跟“是”&#39;或者&#39;否&#39;类标签。 在某些数据上运行此操作,TPR和FPR被准确地计算并显示为对应于每个混淆矩阵;但是,ROC曲线下面积显示为“NaN”。并且曲线是直的垂直或水平线:
我做错了什么?任何帮助将不胜感激。
答案 0 :(得分:1)
这将是一个评论,但我是新的,无法发表评论。 我从循环内部运行你的代码,我的数据就像魅力一样。 所以这不是打印的问题。
看起来您的评估有效
在某些数据上运行此操作时,TPR和FPR会根据每个混淆矩阵进行精确计算和显示;
那你有没有尝试过评价课的以下功能?
evaluation.areaUnderROC(int classIndex);
要了解ROC曲线应该是什么样的?
您的标签类{“是”,“否”}或{0,1}? 我认为这不是问题,但你可以试试吗
Instances result = tc.getCurve(validation.predictions());
而不是
Instances result = tc.getCurve(validation.predictions(), classIndex);
你能发布一些混淆矩阵和TPR / FPR的值吗?
欢呼