gpt4 book ai didi

java - 在 j48 树 weka 中设置交叉验证的数量

转载 作者:行者123 更新时间:2023-12-01 13:50:14 26 4
gpt4 key购买 nike

我想使用 weka j48 树进行 5 交叉验证。这是到目前为止我的代码,

public class WekaJvMain {
public static void main(String[] args) {
try
{
CSV2Arff converter =new CSV2Arff();
converter.convert();

DataSource source = new DataSource("data.arff");
Instances train = source.getDataSet();

train.setClassIndex(train.numAttributes() - 1); // setting class attribute

// classifier
J48 j48 = new J48();
j48.setUnpruned(true); // using an unpruned J48

j48.buildClassifier(train);
System.out.print(j48.graph());

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

此代码训练数据并打印出 j48 树。但是我找不到如何设置交叉验证的折叠数?请详细解释一下,我不擅长Java。

最佳答案

这是您的代码,通过对 j48 分类器进行 5 倍交叉验证评估进行了增强。在训练最终分类器之前进行评估非常重要。更多信息请参见 here

public class WekaJvMain {
public static void main(String[] args) {
try
{
CSV2Arff converter =new CSV2Arff();
converter.convert();

DataSource source = new DataSource("data.arff");
Instances train = source.getDataSet();

train.setClassIndex(train.numAttributes() - 1); // setting class attribute

// classifier
J48 j48 = new J48();
j48.setUnpruned(true); // using an unpruned J48

//evaluate j48 with cross validation
Evaluation eval=new Evaluation(train);

//first supply the classifier
//then the training data
//number of folds
//random seed
eval.crossValidateModel(j48, train, 5, new Random(1));
System.out.println("Percent correct: "+
Double.toString(eval.pctCorrect()));


j48.buildClassifier(train);
System.out.print(j48.graph());

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

关于java - 在 j48 树 weka 中设置交叉验证的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20019765/

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