Weka throws “UnassignedDatasetException”

ε祈祈猫儿з 提交于 2020-01-14 12:42:47

问题


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.


回答1:


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

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...

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.

Cheers!



来源:https://stackoverflow.com/questions/27444077/weka-throws-unassigneddatasetexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!