0

I hava a problem with classifying a single instance in WEKA, here is my code :

public void addInstance() throws Exception {
    Scanner s = new Scanner(System.in);
    DenseInstance newInstance  = new DenseInstance(m_Training.numAttributes());
    for(int i = 0 ; i < m_Training.numAttributes()-1 ; i++)
    {
       double value = s.nextDouble();
       newInstance.setValue(i , value);
       //i is the index of attribute
       //value is the value that you want to set
    }
    newInstance.setValue(m_Training.numAttributes()-1, Float.NaN);
    //add the new instance to the main dataset at the last position
    m_Training.add(newInstance);

    // Print header and instances. 
    System.out.println("\nDataset:\n"); 
    System.out.println(m_Training);

    System.out.print("\n\nClassification result : ");
    System.out.println(m_Classifier.classifyInstance(m_Training.instance(150)));
    System.out.println(m_Training.instance(150));
    System.out.println(m_Training.classAttribute().value((int)m_Classifier.classifyInstance(m_Training.instance(150))));
}

But it always give a result of 0.0. I have read this tutorial : weka: how to get class name from testing single instance but still, the program gives me only 0.0 and the class name is always "iris-setosa".

I used Iris.arff for this program and the classifier(m_classifier) is J48. I don't know why it's not working, any help appreciated

Community
  • 1
  • 1
  • I would try to print the attributes of your new instance just to make sure it adds them as expected. Other than that your code seems fine. Maybe it is a classifier issue. Have you trained it properly? – xro7 Nov 02 '16 at 08:42
  • I have print it, and it's fine. Before I called the addInstance(), I had trained it with 10folds cross validation – Varian Caesar Nov 02 '16 at 09:08
  • Have you tried to classify your entire training set? Or better, a new testing set(don't know if it is provided). What results are you getting? – xro7 Nov 02 '16 at 10:11
  • I found the error, its because I use supervised Discretize for filtering and now I changing it into NominalToBinary and worked. Thanks anyways for your reply @xro7 – Varian Caesar Nov 02 '16 at 12:49

0 Answers0