如何在Weka中复制数据集的类值?

我有两个Instances数据集:data是原始数据集,stumpyInsts是我试图复制类值的目标数据集。以下是我的代码:

FastVector attributes = new FastVector();ArrayList<Instance> instances = new ArrayList<Instance>();for(int i = 0; i <= 100; i++){    Attribute newAttr = new Attribute("Stump" + i, i);    attributes.addElement(newAttr);}//make new instancesInstances stumpyInsts = new Instances("Stumps", attributes, data.numInstances());stumpyInsts.setClassIndex(stumpyInsts.numAttributes() - 1);Enumeration instEnum = stumpyInsts.enumerateInstances();Enumeration somethingElseLOL = data.enumerateInstances();while (instEnum.hasMoreElements()) {    Instance instance = (Instance) instEnum.nextElement();    Instance other = (Instance) somethingElseLOL.nextElement();    String s = other.stringValue(other.classIndex());    instance.setValue(instance.classIndex(), s);}

当我尝试设置值时,我总是得到以下错误:

`java.lang.IllegalArgumentException: Attribute neither nominal nor string!at weka.core.Instance.setValue(Instance.java:687)`

有人知道这是为什么吗?我在尝试在将实例添加到数据集之前使用setClassValue,或者创建新字符串时,也会得到同样的错误。对我来说这似乎不太合理,因为stringValue显然返回的是一个字符串。


回答:

这个错误

java.lang.IllegalArgumentException: Attribute neither nominal nor string!  at weka.core.Instance.setValue(Instance.java:687)

并不是指stringValue,而是指instance的类属性。当你执行

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

你只是告诉stumpyInsts它的类索引是什么,但没有说明它应该是名义或字符串属性。根据这个回答,你需要做类似以下的事情

FastVector classAttr = new FastVector();classAttr .addElement(new Attribute("class", (FastVector) null));

来创建一个具有字符串或名义值的(类)属性。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注