我有一个程序,训练一个具有2级分类结果的算法,然后运行并写出未标记数据集的预测(每个类的概率)。
针对此程序运行的所有数据集将与结果具有相同的2个类。考虑到这一点,我运行了预测,并使用一些事后统计数据来确定哪一列结果描述了哪些结果,并继续硬编码:
public class runPredictions {
public static void runPredictions(ArrayList al2) throws IOException, Exception{
// Retrieve objects
Instances newTest = (Instances) al2.get(0);
Classifier clf = (Classifier) al2.get(1);
// Print status
System.out.println("Generating predictions...");
// create copy
Instances labeled = new Instances(newTest);
BufferedWriter outFile = new BufferedWriter(new FileWriter("silverbullet_rro_output.csv"));
StringBuilder builder = new StringBuilder();
builder.append("Prob_Retain"+","+"Prob_Attrite"+"\n");
for (int i = 0; i < labeled.size(); i++)
{
double[] clsLabel = clf.distributionForInstance(newTest.instance(i));
for(int j=0;j<2;j++){
builder.append(clsLabel[j]+"");
if(j < clsLabel.length - 1)
builder.append(",");
}
builder.append("\n");
}
outFile.write(builder.toString());//save the string representation
System.out.println("Output file written.");
System.out.println("Completed successfully!");
outFile.close();
}
}
问题在于,事实证明,2列中的哪一列描述了2个结果类别中的哪一个未被修复。这似乎与训练数据集中首先出现的类别有关,这完全是任意的。因此,当其他数据集与此程序一起使用时,硬编码标签就会倒退。
所以,我需要一种更好的方式来标记它们,但是查看Classifier
和distributionForInstance
的文档,我没有看到任何有用的内容。
更新:
我想出了如何将它打印到屏幕上(感谢this),但仍然无法将其写入csv:
for (int i = 0; i < labeled.size(); i++)
{
// Discreet prediction
double predictionIndex =
clf.classifyInstance(newTest.instance(i));
// Get the predicted class label from the predictionIndex.
String predictedClassLabel =
newTest.classAttribute().value((int) predictionIndex);
// Get the prediction probability distribution.
double[] predictionDistribution =
clf.distributionForInstance(newTest.instance(i));
// Print out the true predicted label, and the distribution
System.out.printf("%5d: predicted=%-10s, distribution=",
i, predictedClassLabel);
// Loop over all the prediction labels in the distribution.
for (int predictionDistributionIndex = 0;
predictionDistributionIndex < predictionDistribution.length;
predictionDistributionIndex++)
{
// Get this distribution index's class label.
String predictionDistributionIndexAsClassLabel =
newTest.classAttribute().value(
predictionDistributionIndex);
// Get the probability.
double predictionProbability =
predictionDistribution[predictionDistributionIndex];
System.out.printf("[%10s : %6.3f]",
predictionDistributionIndexAsClassLabel,
predictionProbability );
// Attempt to write to CSV
builder.append(i+","+predictedClassLabel+","+
predictionDistributionIndexAsClassLabel+","+predictionProbability);
//.charAt(0)+','+predictionProbability.charAt(0));
}
System.out.printf("\n");
builder.append("\n");
答案 0 :(得分:1)
我根据此answer和此answer调整了以下代码。基本上,您可以查询测试数据以获取类属性,然后获取每个可能类的特定值。
for (int i = 0; i < labeled.size(); i++)
{
// Discreet prediction
double predictionIndex =
clf.classifyInstance(newTest.instance(i));
// Get the predicted class label from the predictionIndex.
String predictedClassLabel =
newTest.classAttribute().value((int) predictionIndex);
// Get the prediction probability distribution.
double[] predictionDistribution =
clf.distributionForInstance(newTest.instance(i));
// Print out the true predicted label, and the distribution
System.out.printf("%5d: predicted=%-10s, distribution=",
i, predictedClassLabel);
// Loop over all the prediction labels in the distribution.
for (int predictionDistributionIndex = 0;
predictionDistributionIndex < predictionDistribution.length;
predictionDistributionIndex++)
{
// Get this distribution index's class label.
String predictionDistributionIndexAsClassLabel =
newTest.classAttribute().value(
predictionDistributionIndex);
// Get the probability.
double predictionProbability =
predictionDistribution[predictionDistributionIndex];
System.out.printf("[%10s : %6.3f]",
predictionDistributionIndexAsClassLabel,
predictionProbability );
// Write to CSV
builder.append(i+","+
predictionDistributionIndexAsClassLabel+","+predictionProbability);
}
System.out.printf("\n");
builder.append("\n");
}
// Save results in .csv file
outFile.write(builder.toString());//save the string representation