1

I train and create a J48 model use WEKA Java Api. Then, I use classifyInstance() to classify my instance. but the result is wrong. my code id following:

    Instances train = reader.getDataSet();
    Instances test = reader_test.getDataSet();

    train.setClassIndex(train.numAttributes() - 1);
    Classifier cls = new J48();
    cls.buildClassifier(train);

    test.setClassIndex(test.numAttributes() - 1);

    for(int i = 0; i < test.numInstances(); i++){
        Instance inst = test.instance(i);
        double result = cls.classifyInstance(inst);
        System.out.println(train.classAttribute().value((int)r));
    }

The result always equal 0.0

Finally, I use test.insertAttributeAt() before test.setClassIndex(). as following:

test.insertAttributeAt(train.attribute(train.numAttributes() - 1), test.numAttributes());

The result become right. I am very surprising! however, most documents are not use the function to inserAttribute. I want to understand why the result become right suddenly.

1 Answers1

3

It will help you.

BufferedReader datafile = readDataFile(TrainingFile);
Instances train = new Instances(datafile);
data.setClassIndex(data.numAttributes() - 1);

Classifier cls = new J48();
cls.buildClassifier(train);

DataSource testDataset = new DataSource(Test);
Instances test = testDataset.getDataSet();
Testdata.setClassIndex(Testdata.numAttributes() - 1);

for(int i = 0; i < test.numInstances(); i++){

    Instance inst = test.instance(i);

    double actualClassValue  = test.instance(i).classValue();

    //it will print your class value
    String actual=test.classAttribute().value((int)actualClassValue);

    double result = cls.classifyInstance(inst);

    //will print your predicted value
    String prediction=test.classAttribute().value((int)result );


}

you don't need to use insertAttributeAt now.

File Conversion Code

    // load CSV
    CSVLoader loader = new CSVLoader();
    String InputFilename = "TrainingFileName";
    loader.setSource(new File(InputFilename));
    Instances data = loader.getDataSet(); 

    // save ARFF
    ArffSaver saver = new ArffSaver();
    saver.setInstances(data);
    String FileT = Filename+".arff";
    saver.setFile(new File(Path+Directory+"\\"+FileT));
    saver.writeBatch();     

Change accordingly.

Thanks

Pandit
  • 748
  • 1
  • 7
  • 22
  • thank you for your help.but the result still is wrong in my computer.I forget tell you, the test.csv is not have class value. If add the last column of class manually, the result will be right. I want to predict the result ,so i not add the class. – zhenshun chen Nov 25 '15 at 01:59
  • According to the weka documentatio, the last column doesn't matter whether you add or not. Try to convert the file in .arff and just give the **@attribute status{PASS,FAIL}** or what ever values you are expecting. you can also copy class values for the last column from training set. and dont add any value in the last column. I will add the File conversion code below. – Pandit Nov 25 '15 at 15:43
  • i edited the answer. Please add your code. So that i can see whats wrong. also, you can convert from GUI as well – Pandit Nov 25 '15 at 15:50