3

Java で Weka を使用して、著者のブログを男性または女性によって書かれたものとして分類しようとしています。トレーニング セットで使用する属性を定義する Weka というクラスを作成し、メソッドを呼び出して、Excel シートから既知のすべてのデータを読み込みます。ファイル内のデータは次のように編成されています。各行のセル 0 にブログ テキストがあり、セル 1 に M または F があります。

ブログ テキスト M その他のテキスト F

私はこのチュートリアルにも少し従っていますWeka Javaチュートリアル

プログラムを実行すると、Eclipse のコンソール ウィンドウにテキストが表示され始めますが、突然、「指定された公称属性に対して値が定義されていません!」という赤いエラーが表示されます。なぜこれが起こるのかよくわかりません。テキストは行ごとに変化しているため、すべての公称属性を定義することはできないと思いました。ここで私が間違っていることや愚かなことを誰かが見ることができますか??? 助けていただければ幸いです。私はこれに数時間立ち往生しています。

コード:

    public class Weka
{
    static FastVector fvWekaAttributes;
    static Instances isTrainingSet;
    static Classifier cModel;

    public static void main(String[] args) throws Exception
    {



        // Declaring attributes
        Attribute stringAttribute = new Attribute("text", (FastVector) null);

        // Declaring a class attribute along with values
        FastVector fastVClassVal = new FastVector(2);
        fastVClassVal.addElement("M");
        fastVClassVal.addElement("F");

        Attribute classAttribute = new Attribute("theClass", fastVClassVal);

        // Declaring the feature vector
        fvWekaAttributes = new FastVector(2);
        fvWekaAttributes.addElement(stringAttribute);
        fvWekaAttributes.addElement(classAttribute);

        // create the training set
        isTrainingSet = new Instances("Rel", fvWekaAttributes, 10);

        // set class index
        isTrainingSet.setClassIndex(1);

        // create however many instances is in my excel file
        // and add it to the training set in a loop.
        Weka.LoadExcelWorkBook(isTrainingSet);
        Weka.TestSetWork();

    }

    public static void TestSetWork() throws Exception
    {
        // test the model
        Evaluation testing = new Evaluation(isTrainingSet);
        testing.evaluateModel(cModel, isTrainingSet);

        // printing the results....
        String strSummary = testing.toSummaryString();
        System.out.println(strSummary);

        // get confusion matrix.

        double[][] cmMatrix = testing.confusionMatrix();
        for (int i = 0; i < cmMatrix.length; i++)
        {
            for (int col = 0; col < cmMatrix.length; col++)
            {
                System.out.print(cmMatrix[i][col]);
                System.out.print("|");
            }
            System.out.println();
        }

    }

    public static void LoadExcelWorkBook(Instances trainingSet)
            throws Exception
    {
        System.out.println("LOADING EXCEL WORKBOOK!!!");
        Workbook wb = null;
        // opening excel file.

        try
        {
            wb = WorkbookFactory
                    .create(new File("C://blog-gender-dataset.xlsx"));

        } catch (IOException ieo)
        {
            ieo.printStackTrace();
        }

        // opening worksheet.
        Sheet sheet = wb.getSheetAt(0);

        StringToWordVector filter = new StringToWordVector();
        filter.setInputFormat(isTrainingSet);

        Instances dataFiltered = Filter.useFilter(isTrainingSet, filter);

        for (Row row : sheet)
        {

            Cell textCell = row.getCell(0);
            Cell MFCell = row.getCell(1);

            String blogText = textCell.getStringCellValue();
            String MFIndicator = MFCell.getStringCellValue();
            System.out.println("TEXT FROM EXCEL " + blogText);
            Instance iText = new Instance(2);

            iText.setValue((Attribute) fvWekaAttributes.elementAt(0), tweetText);
            iText.setValue((Attribute) fvWekaAttributes.elementAt(1),
                    MFIndicator);

            isTrainingSet.add(iText);

            cModel = (Classifier) new J48();
            cModel.buildClassifier(dataFiltered);

        }
    }

}
4

1 に答える 1

0

「指定された公称属性の値が定義されていません!」構成するインスタンスで、期待されるデータが、特定の名義属性の arff @attribute セクションで定義した値以外の値を持つ場合に到着します。たとえば、期待される値を "M" または "F" と定義しましたが、読み取った値は空 (N/A) などである可能性があります。解決策は、データを厳密に検証し、ロードしたものをデバッグ/トレースすることです。エラーが発生した属性を調べ、その値をその属性の可能な値に追加するか、または、これが体系的に表示される場合は、その属性をより一般的な型 (文字列、数値など) として定義します。

于 2015-11-24T16:37:12.633 に答える