Weka 抛出“UnassignedDatasetException"; [英] Weka throws "UnassignedDatasetException"

查看:27
本文介绍了Weka 抛出“UnassignedDatasetException";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Weka 3.6.11,但我遇到了一个错误,我无法弄清楚是什么导致了它.我遵循了 Weka 手册中的第 202-204 页,并像他们说的那样构建了我的数据.仍然当我尝试对数据进行分类时,我得到了一个错误.

I am working with Weka 3.6.11 and I am having an error which I can't figure out what is causing it. I have followed pages 202-204 in the Weka manual and have constructed my data like they say. Still when I try t classify the data I get an error.

weka.core.UnassignedDatasetException: Instance doesn't have access to a dataset!

这是我到目前为止的代码:

Here is the code I have so far:

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();
        }
    }

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

From the error message I take it that I need to assign my dataset something but I do not know what or how. Can someone point out my mistake? Thanks.

推荐答案

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

I have found the solution to my own question and thus I am providing the information here so that it might help someone else.

我最初的问题是我收到了UnsignedDataSetException".为了解决这个问题,我向 setDataSet 添加了一个方法调用,如下所示:

My original problem was that I was getting an "UnsignedDataSetException". To solve that I added a method call to setDataSet like so:

....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 的方法,如下所示:

After that I got another exception called UnassignedClassException. That means that you have not explicitly set the attribute which is to be used as the outcome of the prediction. Usually it is the last attribute so we add a method called setClassIndex like so:

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);

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

Now it works. It predicts the correct iris (at least for the one I have tried). If something else pops up I will edit this question/answer.

干杯!

这篇关于Weka 抛出“UnassignedDatasetException";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆