首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法处理多值标称类- JAVA

无法处理多值标称类- JAVA
EN

Stack Overflow用户
提问于 2014-11-23 21:06:06
回答 2查看 4.6K关注 0票数 4

我试图向LinearRegression对象传递一个.arff文件,同时它给了我这个异常,不能处理多值标称类!

实际发生的情况是,在这样做之后,我使用CFSSubsetEval计算程序执行属性选择,然后搜索为LinearRegression,将这些属性传递给LinearRegression,如下所示

代码语言:javascript
复制
LinearRegression rl=new LinearRegression(); rl.buildClassifier(data);    

data是一个实例对象,它拥有来自.arff文件的数据,以前仅使用weka将其转换为标称值。我在这里做错什么了吗?我试图在谷歌上搜索这个错误,但是找不到。

代码

代码语言:javascript
复制
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”属性的实例变量,所以我(为了程序运行)从同一个原始文件加载数据。

我不知道如何上传一个样本数据的文件,但是它看起来像这样。链接

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-23 22:31:56

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

您可能要做的事情,以确保您的给定数据集工作,运行它通过Weka资源管理器与线性回归,并看看是否产生所需的结果。在此之后,数据很有可能在代码中正确工作。

希望这能帮上忙!

票数 4
EN

Stack Overflow用户

发布于 2017-04-23 12:21:22

下面是用于LinearRegression (来源)的数据集示例

代码语言:javascript
复制
@RELATION house
@ATTRIBUTE houseSize NUMERIC
@ATTRIBUTE lotSize NUMERIC
@ATTRIBUTE bedrooms NUMERIC
@ATTRIBUTE granite NUMERIC
@ATTRIBUTE bathroom NUMERIC
@ATTRIBUTE sellingPrice NUMERIC

@DATA
3529,9191,6,0,0,205000
3247,10061,5,1,1,224900
4032,10150,5,0,1,197900
2397,14156,4,1,0,189900
2200,9600,4,0,1,195000
3536,19994,6,1,1,325000
2983,9365,5,0,1,230000
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27094301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档