java - Weka 抛出 "UnassignedDatasetException"

标签 java weka

我正在使用 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();
        }
    }

根据错误消息,我认为我需要为我的数据集分配一些东西,但我不知道是什么或如何分配。 有人可以指出我的错误吗? 谢谢。

最佳答案

我已经找到了我自己的问题的解决方案,因此我在这里提供信息,以便它可以帮助其他人。

我最初的问题是我遇到了“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);

现在可以了。它预测了正确的虹膜(至少对于我试过的那个)。如果弹出其他内容,我将编辑此问题/答案。

干杯!

关于java - Weka 抛出 "UnassignedDatasetException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27444077/

相关文章:

java - 如何将属性文件中的值注入(inject)到字段中?

java - JSON Android java.lang.NullPointerException

java - 在 avro 文件中存储空值

WEKA:这些树叶中 '/' 之后的数字代表什么?

java - 读取已训练的 SMO 模型会出现 StreamoutofException 错误

java - Java 中的成本矩阵

machine-learning - 20newsgroup-18828 数据集有什么问题?

java - ANTLR V4 + Java8 语法 -> OutOfMemoryException

java - 为什么 operator < 有 Java 泛型的编译器错误?

machine-learning - 如何在SVM中对不同大小的特征向量进行编码