ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Weka抛出“UnassignedDatasetException”

2019-06-28 20:50:42  阅读:205  来源: 互联网

标签: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
来源: https://codeday.me/bug/20190628/1319279.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有