我试图使用weka的java API创建一个“自动训练”,但我猜我做错了什么,每当我通过weka的接口使用MultiLayerPerceptron测试我的ARFF文件时,使用10交叉验证或66%分割,我得到了一些令人满意的结果(大约90%),但是当我尝试通过weka的api测试相同的文件时,每个测试基本上都返回0%匹配(每一行都返回false)
以下是weka的gui的输出:
===对测试拆分=== ===汇总===的评估
Correctly Classified Instances 78 91.7647 %
Incorrectly Classified Instances 7 8.2353 %
Kappa statistic 0.8081
Mean absolute error 0.0817
Root mean squared error 0.24
Relative absolute error 17.742 %
Root relative squared error 51.0603 %
Total Number of Instances 85 按类===列出的===详细精度
TP Rate FP Rate Precision Recall F-Measure ROC Area Class
0.885 0.068 0.852 0.885 0.868 0.958 1
0.932 0.115 0.948 0.932 0.94 0.958 0
Weighted Avg. 0.918 0.101 0.919 0.918 0.918 0.958===混淆矩阵===
a b <-- classified as
23 3 | a = 1
4 55 | b = 0下面是我在java上使用的代码(实际上是在使用IKVM的.NET上):
var classifier = new weka.classifiers.functions.MultilayerPerceptron();
classifier.setOptions(weka.core.Utils.splitOptions("-L 0.7 -M 0.3 -N 75 -V 0 -S 0 -E 20 -H a")); //these are the same options (the default options) when the test is run under weka gui
string trainingFile = Properties.Settings.Default.WekaTrainingFile; //the path to the same file I use to test on weka explorer
weka.core.Instances data = null;
data = new weka.core.Instances(new java.io.BufferedReader(new java.io.FileReader(trainingFile))); //loads the file
data.setClassIndex(data.numAttributes() - 1); //set the last column as the class attribute
cl.buildClassifier(data);
var tmp = System.IO.Path.GetTempFileName(); //creates a temp file to create an arff file with a single row with the instance I want to test taken from the arff file loaded previously
using (var f = System.IO.File.CreateText(tmp))
{
//long code to read data from db and regenerate the line, simulating data coming from the source I really want to test
}
var dataToTest = new weka.core.Instances(new java.io.BufferedReader(new java.io.FileReader(tmp)));
dataToTest.setClassIndex(dataToTest.numAttributes() - 1);
double prediction = 0;
for (int i = 0; i < dataToTest.numInstances(); i++)
{
weka.core.Instance curr = dataToTest.instance(i);
weka.core.Instance inst = new weka.core.Instance(data.numAttributes());
inst.setDataset(data);
for (int n = 0; n < data.numAttributes(); n++)
{
weka.core.Attribute att = dataToTest.attribute(data.attribute(n).name());
if (att != null)
{
if (att.isNominal())
{
if ((data.attribute(n).numValues() > 0) && (att.numValues() > 0))
{
String label = curr.stringValue(att);
int index = data.attribute(n).indexOfValue(label);
if (index != -1)
inst.setValue(n, index);
}
}
else if (att.isNumeric())
{
inst.setValue(n, curr.value(att));
}
else
{
throw new InvalidOperationException("Unhandled attribute type!");
}
}
}
prediction += cl.classifyInstance(inst);
}
//prediction is always 0 here, my ARFF file has two classes: 0 and 1, 92 zeroes and 159 ones这很有趣,因为如果我将分类器更改为NaiveBayes,结果将与通过weka的gui进行的测试相匹配
发布于 2012-06-13 05:53:31
您正在使用一种不推荐使用的读取ARFF文件的方式。请参阅此documentation。试着这样做:
import weka.core.converters.ConverterUtils.DataSource;
...
DataSource source = new DataSource("/some/where/data.arff");
Instances data = source.getDataSet();请注意,该文档还展示了如何直接连接到数据库,并绕过创建临时ARFF文件。此外,您还可以从数据库中读取并手动创建用于填充instances对象的实例。
最后,如果简单地将代码顶部的分类器类型更改为NaiveBayes就能解决问题,那么请检查MultilayerPerceptron的weka gui中的选项,看看它们是否与默认值不同(不同的设置会导致相同的分类器类型产生不同的结果)。
MLP:看起来您在代码中使用的测试数据与在weka GUI中使用的测试数据不同(来自数据库而不是原始训练文件的文件夹);也可能是数据库中的特定数据在分类器看来确实像class 0。要验证是否是这种情况,您可以使用weka接口将训练arff拆分为训练/测试集,然后在代码中重复原始实验。如果结果与gui相同,那么您的数据就有问题。如果结果不同,那么我们需要更仔细地查看代码。您要调用的函数是这个(from the Doc)
public Instances trainCV(int numFolds, int numFold)发布于 2014-12-30 02:02:09
我也有同样的问题。
与Java中的交叉验证相比,Weka在Explorer中给出了不同的结果。
一些有用的东西:
Instances dataSet = ...;
dataSet.stratify(numOfFolds); // use this
//before splitting the dataset into train and test set!https://stackoverflow.com/questions/10999792
复制相似问题