在java代码中使用WEKA API时出现错误:类属性未设置?

发布于 2024-12-15 03:33:45 字数 2108 浏览 0 评论 0原文

我正在尝试在我的java代码中使用weka API。我使用 J48 树分类对 MySQL 数据库中的数据集进行分类,但出现此错误:

Trying to add database driver (JDBC): RmiJdbc.RJDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): jdbc.idbDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): com.mckoi.JDBCDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): org.hsqldb.jdbcDriver - Error, not in CLASSPATH?
weka.core.UnassignedClassException: weka.classifiers.trees.j48.C45PruneableClassifierTree: Class attribute not set!
        at weka.core.Capabilities.test(Capabilities.java:1086)
        at weka.core.Capabilities.test(Capabilities.java:1018)
        at weka.core.Capabilities.testWithFail(Capabilities.java:1297)
.....

这是我的代码:

try{
       DatabaseLoader loader = new DatabaseLoader();
      loader.setSource("jdbc:mysql://localhost:3306/cuaca","root","491754");
       loader.setQuery("select * from data_training");
      Instances data = loader.getDataSet();

        jTextArea1.append(data.toString());

        String[] options = new String[1];
        options[0] = "U"; // unpruned tree
        J48 tree = new J48(); // new instance of tree
       tree.setOptions(options); // set the option
       tree.buildClassifier(data); // build classifier
       //Evaluation eval=new Evaluation(data);
       //eval.crossValidateModel(tree, data, 10, new Random(1));
       //jTextArea1.append(eval.toSummaryString("\n HASILNYA \n", false));

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

数据集实际上出现在文本区域中:

@relation data_training

@attribute Tmean numeric
@attribute Tmax numeric
@attribute Td numeric
@attribute RH numeric
@attribute SLP numeric
@attribute STP numeric
@attribute Wind_ave numeric
@attribute Wind_max numeric
@attribute Hujan {Y,T}

@data
25.9,31.6,23.1,93.4,1008.5,998.2,2.6,12.9,Y
27.6,31.4,22.3,87,1009.6,999.3,0.8,3.1,T
27.4,32.6,21.9,86.1,1009.4,999.2,4.5,14.3,T
27.6,32.2,22.4,87.3,1009.1,998.9,2.4,8.2,T

我想知道为什么会出现错误(类属性未设置!!)

I'm trying to use weka API in my java code. I use J48 tree classification to Classify my dataset in MySQL database, but I have this error:

Trying to add database driver (JDBC): RmiJdbc.RJDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): jdbc.idbDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): com.mckoi.JDBCDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): org.hsqldb.jdbcDriver - Error, not in CLASSPATH?
weka.core.UnassignedClassException: weka.classifiers.trees.j48.C45PruneableClassifierTree: Class attribute not set!
        at weka.core.Capabilities.test(Capabilities.java:1086)
        at weka.core.Capabilities.test(Capabilities.java:1018)
        at weka.core.Capabilities.testWithFail(Capabilities.java:1297)
.....

This is my code:

try{
       DatabaseLoader loader = new DatabaseLoader();
      loader.setSource("jdbc:mysql://localhost:3306/cuaca","root","491754");
       loader.setQuery("select * from data_training");
      Instances data = loader.getDataSet();

        jTextArea1.append(data.toString());

        String[] options = new String[1];
        options[0] = "U"; // unpruned tree
        J48 tree = new J48(); // new instance of tree
       tree.setOptions(options); // set the option
       tree.buildClassifier(data); // build classifier
       //Evaluation eval=new Evaluation(data);
       //eval.crossValidateModel(tree, data, 10, new Random(1));
       //jTextArea1.append(eval.toSummaryString("\n HASILNYA \n", false));

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

The dataset actually appears in the textarea:

@relation data_training

@attribute Tmean numeric
@attribute Tmax numeric
@attribute Td numeric
@attribute RH numeric
@attribute SLP numeric
@attribute STP numeric
@attribute Wind_ave numeric
@attribute Wind_max numeric
@attribute Hujan {Y,T}

@data
25.9,31.6,23.1,93.4,1008.5,998.2,2.6,12.9,Y
27.6,31.4,22.3,87,1009.6,999.3,0.8,3.1,T
27.4,32.6,21.9,86.1,1009.4,999.2,4.5,14.3,T
27.6,32.2,22.4,87.3,1009.1,998.9,2.4,8.2,T

I would to know why the error appear(Class Attribute Not Set!!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2024-12-22 03:33:45

在将数据集传递到分类器之前,您应该为数据集设置类索引。您的分类器必须知道哪个是您的结果变量。
在这些行之后...

loader.setQuery("select * from data_training");
Instances data = loader.getDataSet();

添加以下内容:

data.setClassIndex(data.numAttributes() - 1);

如果 Hujan 是您的类属性(结果变量)

请参阅 API 文档了解更多信息

You should set class index of for your dataset before passing it into classifier. Your classifier must know which is your outcome variable.
After these lines...

loader.setQuery("select * from data_training");
Instances data = loader.getDataSet();

Add the following:

data.setClassIndex(data.numAttributes() - 1);

If Hujan is your class attribute (outcome variable)

See Api Docs for more Info

梦在夏天 2024-12-22 03:33:45

试试

data.setClassIndex(1);
  1. 你的班级索引

try it

data.setClassIndex(1);
  1. you class index
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文