Sajith Vijesekara
Sajith Vijesekara

Reputation: 1372

Java Naive Bayes Classifier evaluation

i am new to weka data mining and evaluation.So far i have read data set.I want to predict my data based on data set.As a example i have used weather data set provided by weka tool.So i have used Naive Bayes Classifier for classification.Now i got probability values for my attributes.Now i want to predict data using data-set. As example when i give sunny,70,85,TRUE then i want to get probability of class value.So far i have done this part.Can anyone tell me how to used Naive Bayes classifier for data evaluation.

public static void ArfLoader(){
         ArffLoader loader = new ArffLoader();
         try {
             loader.setFile(new File("sampleData.txt"));
             Instances structure = loader.getStructure();
             structure.setClassIndex(structure.numAttributes() - 1);

             NaiveBayesUpdateable nb = new NaiveBayesUpdateable();
             nb.buildClassifier(structure);
             Instance current;
             while ((current = loader.getNextInstance(structure)) != null){
                 nb.updateClassifier(current); 
             }

             System.out.print(nb); 


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

Then this is my data set.

@relation weather

@attribute outlook {sunny, overcast, rainy}
@attribute temperature real
@attribute humidity real
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}

@data
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no
overcast,83,86,FALSE,yes
rainy,70,96,FALSE,yes
rainy,68,80,FALSE,yes
rainy,65,70,TRUE,no
overcast,64,65,TRUE,yes
sunny,72,95,FALSE,no
sunny,69,70,FALSE,yes
rainy,75,80,FALSE,yes
sunny,75,70,TRUE,yes
overcast,72,90,TRUE,yes
overcast,81,75,FALSE,yes
rainy,71,91,TRUE,no

Upvotes: 2

Views: 1583

Answers (1)

Matthew Spencer
Matthew Spencer

Reputation: 2270

You could try the classifyInstance method as outlined below for a separate testing set:

         ArffLoader testingData = new ArffLoader();
         testingData.setFile(new File("sample2.txt"));
         Instances testingStructure = testingData.getStructure();
         testingStructure.setClassIndex(structure.numAttributes() - 1);
         Instance test;
         while ((test = testingData.getNextInstance(testingStructure)) != null) {
            System.out.println(nb.classifyInstance(test));
         }

Hope this Helps!

UPDATE!

I sounds like you're looking for the probability distribution for each test case. Perhaps you could try the below instead:

         String[] options = new String[7];
         options[0] = "-t";
         options[1] = "sample.arff";
         options[2] = "-T";
         options[3] = "sample2.arff";
         options[4] = "-p";
         options[5] = "2";
         options[6] = "-distribution";

         System.out.println(Evaluation.evaluateModel(nb, options));

This will contain a listing of the probability distributions for each case (Training Data = sample.arff, Testing Data = sample2.arff, Output Test Predictions with Probability Distribution)

Upvotes: 1

Related Questions