4

.arffファイルをLinearRegressionオブジェクトに渡そうとしていますが、そうしている間にこの例外が発生します 複数値の名義クラスを処理できません! .

実際に起こっているのは、 CFSSubsetEval 評価子を使用して属性選択を実行し、その後、 GreedyStepwiseとして検索し、次のようにそれらの属性を LinearRegression に渡すことです。

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

data は、weka のみを使用して以前に公称値に変換された .arff ファイルからのデータを持つインスタンス オブジェクトです。私はここで何か悪いことをしていますか? Googleでこのエラーを検索しようとしましたが、見つかりませんでした。

コード

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);
    }
}

:「インデックス」属性を持つインスタンス変数を作成するコードを書いていないので、(プログラムを実行するために)同じ元のファイルからデータをロードしています。

サンプルデータ用のファイルのアップロード方法がわかりませんが、こんな感じです。[リンク] ( https://scontent-a-dfw.xx.fbcdn.net/hphotos-xfa1/t31.0-8/p552x414/10496920_756438941076936_8448023649960186530_o.jpg )

4

2 に答える 2

4

データに基づいて、最後の属性は公称データ型のようです (ほとんどの数値が含まれていますが、いくつかの文字列も含まれています)。 LinearRegressionは、名義クラスの予測を許可しません。

特定のデータセットが機能することを確認するために潜在的にできることは、線形回帰を使用して Weka Explorer を実行し、目的の結果が生成されるかどうかを確認することです。これに続いて、データがコードで正しく機能する可能性が高くなります。

お役に立てれば!

于 2014-11-23T22:31:56.917 に答える