我正在使用Weka 3.6.11,我有一个错误,我不知道是什么原因造成的。我遵循了Weka手册中的202-204页,并像他们说的那样构建了我的数据。不过,当我试图对数据进行分类时,会出现一个错误。
weka.core.UnassignedDatasetException: Instance doesn't have access to a dataset!下面是我到目前为止掌握的代码:
public static void classifyTest()
{
try
{
Classifier classifier = (Classifier)weka.core.SerializationHelper.read("iris120.model");
System.Console.WriteLine("----------------------------");
weka.core.Attribute sepallength = new weka.core.Attribute("sepallength");
weka.core.Attribute sepalwidth = new weka.core.Attribute("sepalwidth");
weka.core.Attribute petallength = new weka.core.Attribute("petallength");
weka.core.Attribute petalwidth = new weka.core.Attribute("petalwidth");
FastVector labels = new FastVector();
labels.addElement("Iris-setosa");
labels.addElement("Iris-versicolor");
labels.addElement("Iris-virginica");
weka.core.Attribute cls = new weka.core.Attribute("class", labels);
FastVector attributes = new FastVector();
attributes.addElement(sepallength);
attributes.addElement(sepalwidth);
attributes.addElement(petallength);
attributes.addElement(petalwidth);
attributes.addElement(cls);
Instances dataset = new Instances("TestInstances", attributes, 0);
double[] values = new double[dataset.numAttributes()];
values[0] = 5.0;
values[1] = 3.5;
values[2] = 1.3;
values[3] = 0.3;
Instance inst = new Instance(1,values);
dataset.add(inst);
// Here I try to classify the data that I have constructed.
try
{
double predictedClass = classifier.classifyInstance(inst);
System.Console.WriteLine("Class1: (irisSetosa): " + predictedClass);
}
catch (java.lang.Exception ex)
{
ex.printStackTrace();
}
System.Console.ReadLine();
}
catch (java.lang.Exception ex)
{
ex.printStackTrace();
System.Console.ReadLine();
}
}从错误消息中,我认为我需要分配数据集一些内容,但不知道是什么或如何分配的。有人能指出我的错误吗?谢谢。
发布于 2014-12-12 13:37:45
我已经找到了我自己的问题的解决方案,因此我在这里提供信息,以便它可以帮助其他人。
我最初的问题是我得到了一个"UnsignedDataSetException“。为了解决这个问题,我向setDataSet添加了一个方法调用,如下所示:
....previous code omitted, can be seen in the question...
Instance inst = new Instance(1.0,values);
dataset.add(inst);
inst.setDataset(dataset);
....following code omitted, can be seen in the question...之后,我得到了另一个名为UnassignedClassException的异常。这意味着您没有显式设置要用作预测结果的属性。通常是最后一个属性,因此我们添加了一个名为setClassIndex的方法,如下所示:
Instances dataset = new Instances("TestInstances", attributes, 0);
// Assign the prediction attribute to the dataset. This attribute will
// be used to make a prediction.
dataset.setClassIndex(dataset.numAttributes() - 1);现在起作用了。它预测正确的虹膜(至少对我尝试过的虹膜)。如果有别的事情出现,我会编辑这个问题/答案。
干杯!
https://stackoverflow.com/questions/27444077
复制相似问题