ICode9

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

无法处理多值名义类–JAVA

2019-06-24 07:48:20  阅读:206  来源: 互联网

标签:java runtime-error weka multinomial


我正在尝试将.arff文件传递给LinearRegression对象,同时这样做它给了我这个异常无法处理多值的名义类!

实际发生的是我正在使用CFSSubsetEval求值程序执行属性选择,并在执行此操作后搜索为GreedyStepwise,将这些属性传递给LinearRegression,如下所示

LinearRegression rl=new LinearRegression(); rl.buildClassifier(data);    

data是Instance对象,它具有.arff文件中的数据,该文件以前仅使用weka转换为标称值.我在这做错什么吗?我试图在谷歌上搜索这个错误,但找不到.

package com.attribute;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;

import weka.attributeSelection.AttributeSelection;
import weka.attributeSelection.CfsSubsetEval;
import weka.attributeSelection.GreedyStepwise;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.LinearRegression;
import weka.classifiers.meta.AttributeSelectedClassifier;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.Utils;
import weka.filters.supervised.attribute.NominalToBinary;

/**
 * performs attribute selection using CfsSubsetEval and GreedyStepwise
 * (backwards) and trains J48 with that. Needs 3.5.5 or higher to compile.
 * 
 * @author FracPete (fracpete at waikato dot ac dot nz)
 */
public class AttributeSelectionTest2 {

    /**
     * uses the meta-classifier
     */
    protected static void useClassifier(Instances data) throws Exception {
        System.out.println("\n1. Meta-classfier");
        AttributeSelectedClassifier classifier = new AttributeSelectedClassifier();
        CfsSubsetEval eval = new CfsSubsetEval();
        GreedyStepwise search = new GreedyStepwise();
        search.setSearchBackwards(true);
        J48 base = new J48();
        classifier.setClassifier(base);
        classifier.setEvaluator(eval);
        classifier.setSearch(search);
        Evaluation evaluation = new Evaluation(data);
        evaluation.crossValidateModel(classifier, data, 10, new Random(1));
        System.out.println(evaluation.toSummaryString());
    }

    /**
     * uses the low level approach
     */
    protected static void useLowLevel(Instances data) throws Exception {
        System.out.println("\n3. Low-level");
        AttributeSelection attsel = new AttributeSelection();
        CfsSubsetEval eval = new CfsSubsetEval();
        GreedyStepwise search = new GreedyStepwise();
        search.setSearchBackwards(true);
        attsel.setEvaluator(eval);
        attsel.setSearch(search);
        attsel.SelectAttributes(data);
        int[] indices = attsel.selectedAttributes();
        System.out.println("selected attribute indices (starting with 0):\n"
                + Utils.arrayToString(indices));
        useLinearRegression(indices, data);
    }

    protected static void useLinearRegression(int[] indices, Instances data) throws Exception{
        System.out.println("\n 4. Linear-Regression on above selected attributes");

        BufferedReader reader = new BufferedReader(new FileReader(
                "C:/Entertainement/MS/Fall 2014/spdb/project 4/healthcare.arff"));
        Instances data1 = new Instances(reader);
        data.setClassIndex(data.numAttributes() - 1);
        /*NominalToBinary nb = new NominalToBinary();
        for(int i=0;i<=20; i++){
         //Still coding left here, create an Instance variable to store the data from 'data' variable for given indices
            Instances data_lr=data1.
        }*/
        LinearRegression rl=new LinearRegression(); //Creating a LinearRegression Object to pass data1
        rl.buildClassifier(data1);
    }
    /**
     * takes a dataset as first argument
     * 
     * @param args
     *            the commandline arguments
     * @throws Exception
     *             if something goes wrong
     */
    public static void main(String[] args) throws Exception {
        // load data
        System.out.println("\n0. Loading data");
        BufferedReader reader = new BufferedReader(new FileReader(
                "C:/Entertainement/MS/Fall 2014/spdb/project 4/healthcare.arff"));
        Instances data = new Instances(reader);

        if (data.classIndex() == -1)
            data.setClassIndex(data.numAttributes() - 14);

        // 1. meta-classifier
        useClassifier(data);

        // 2. filter
        //useFilter(data);

        // 3. low-level
        useLowLevel(data);
    }
}

注意:因为我没有编写代码来构建具有’indices’属性的实例变量,所以我(为了运行程序)从同一个原始文件加载数据.

我不知道如何为样本数据上传文件,但它看起来像这样. [链接](https://scontent-a-dfw.xx.fbcdn.net/hphotos-xfa1/t31.0-8/p552x414/10496920_756438941076936_8448023649960186530_o.jpg)

解决方法:

根据您的数据,您的最后一个属性似乎是名义数据类型(主要包含数字,但也有一些字符串). LinearRegression将不允许预测名义类别.

您可以采取的措施确保您的给定数据集正常工作,通过带有线性回归的Weka Explorer运行它,并查看是否生成了所需的结果.在此之后,数据很可能在您的代码中正常运行.

希望这可以帮助!

标签:java,runtime-error,weka,multinomial
来源: https://codeday.me/bug/20190624/1276528.html

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

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

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

ICode9版权所有