Weka throws a "UnassignedDatasetException"

I work with Weka 3.6.11, and I have a mistake, and I can’t understand what causes it. I followed pages 202-204 in the Weka manual and built my data as they say. However, when I try to classify the data, I get an error message.

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

Here is the code that I still have:

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 understand that I need to assign my data set, but I do not know what and how. Can someone point out my mistake? Thank.

+4
source share
1 answer

I found a solution to my question and therefore I am providing information here so that it can help someone else.

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

. ( , , ). - , /.

!

+3

All Articles