Search code examples
javaclassificationwekanaivebayes

Weka Classifier


I coded a simple bayes classifier. Here is the code:

public static void main(String[] args) throws Exception {
    Attribute Attribute1 = new Attribute("firstNumeric");
    Attribute Attribute2 = new Attribute("secondNumeric");

    // Declare a nominal attribute along with its values
    ArrayList<String> fvNominalVal = new ArrayList(3);
    fvNominalVal.add("blue");
    fvNominalVal.add("gray");
    fvNominalVal.add("black");
    Attribute Attribute3 = new Attribute("aNominal", fvNominalVal);

    // Declare the class attribute along with its values
    ArrayList<String> fvClassVal = new ArrayList(2);
    fvClassVal.add("positive");
    fvClassVal.add("negative");
    Attribute ClassAttribute = new Attribute("theClass", fvClassVal);

    // Declare the feature vector
    ArrayList<Attribute> fvWekaAttributes = new ArrayList(4);
    fvWekaAttributes.add(Attribute1);
    fvWekaAttributes.add(Attribute2);
    fvWekaAttributes.add(Attribute3);
    fvWekaAttributes.add(ClassAttribute);

    // Create an empty training set
    Instances isTrainingSet = new Instances("Rel", fvWekaAttributes, 10);
    // Set class index
    isTrainingSet.setClassIndex(3);

    // Create the instance
    Instance ex1 = new DenseInstance(4);
    ex1.setValue((Attribute) fvWekaAttributes.get(0), 1.0);
    ex1.setValue((Attribute) fvWekaAttributes.get(1), 5.5);
    ex1.setValue((Attribute) fvWekaAttributes.get(2), "gray");
    ex1.setValue((Attribute) fvWekaAttributes.get(3), "positive");
    
    Instance ex2 = new DenseInstance(4);
    ex1.setValue((Attribute) fvWekaAttributes.get(0), 1.0);
    ex1.setValue((Attribute) fvWekaAttributes.get(1), 5.5);
    ex1.setValue((Attribute) fvWekaAttributes.get(2), "blue");
    ex1.setValue((Attribute) fvWekaAttributes.get(3), "negative");

    // add the instance
    isTrainingSet.add(ex1);
    isTrainingSet.add(ex2);

    // Create a naïve bayes classifier
    Classifier cModel = (Classifier) new NaiveBayes();
    cModel.buildClassifier(isTrainingSet);
    
    Instance testData = new DenseInstance(4);
    testData.setValue((Attribute) fvWekaAttributes.get(0), 1.0);
    testData.setValue((Attribute) fvWekaAttributes.get(1), 5.5);
    testData.setValue((Attribute) fvWekaAttributes.get(2), "gray");

    Instances testDataSet = new Instances("Rel", fvWekaAttributes, 1);
    testDataSet.setClassIndex(3);
    testDataSet.add(testData);
    
    double[] a = cModel.distributionForInstance(testDataSet.firstInstance());
    for(int i=0;i<a.length;i++){
        System.out.println(a[i]);
    }
}

but the result does not seem to be true. Here is the result:

6.702810252023562E-151

1.0

even if I changed testData to this:

testData.setValue((Attribute) fvWekaAttributes.get(0), 1.0);
testData.setValue((Attribute) fvWekaAttributes.get(1), 5.5);
testData.setValue((Attribute) fvWekaAttributes.get(2), "blue");

the result is nearly this. As below:

3.351405126011781E-151

1.0


Solution

  • In my opinion here the problem is that you only have two instaneces in training set, and the naiv baies classifier cant learn a valuable model from it. Thats why you got a confision results. Try to generate at least 100 or more train instances, or here you can find some sample dataset to learn how to apply ML methods: http://storm.cis.fordham.edu/~gweiss/data-mining/datasets.html