gpt4 book ai didi

java - weka 培训和 java 培训的不同结果

转载 作者:行者123 更新时间:2023-11-29 08:06:18 25 4
gpt4 key购买 nike

我正在尝试使用 weka 的 java api 创建一个“自动训练”,但我想我做错了什么,每当我使用带有 10 交叉验证或 66% 百分比拆分的 MultiLayerPerceptron 通过 weka 的界面测试我的 ARFF 文件时,我得到一些令人满意的结果(大约 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 上使用的代码(实际上是在 .NET 上使用 IKVM):

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 进行的测试相匹配

最佳答案

您正在使用一种已弃用的方式来读取 ARFF 文件。看这个documentation .试试这个:

 import weka.core.converters.ConverterUtils.DataSource;
...
DataSource source = new DataSource("/some/where/data.arff");
Instances data = source.getDataSet();

请注意,该文档还展示了如何直接连接到数据库,以及绕过临时 ARFF 文件的创建。此外,您可以从数据库中读取并手动创建实例来填充 Instances 对象。

最后,如果只是将代码顶部的分类器类型更改为 NaiveBayes 就解决了问题,那么请检查 weka gui 中 MultilayerPerceptron 的选项,看看它们是否与默认值不同(不同的设置可能导致相同的结果分类器类型以产生不同的结果)。

更新:看起来您在代码中使用的测试数据与在 weka GUI 中使用的测试数据不同(来自数据库与原始训练文件的折叠);也可能是数据库中的特定数据实际上看起来像 MLP 分类器的 class 0。要验证是否是这种情况,您可以使用 weka 接口(interface)将您的训练 arff 拆分为训练/测试集,然后在您的代码中重复原来的实验。如果结果和gui一样,说明你的数据有问题。如果结果不同,那么我们需要更仔细地查看代码。您要调用的函数是 (from the Doc) :

public Instances trainCV(int numFolds, int numFold)

关于java - weka 培训和 java 培训的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10999792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com