title: using weka j48 in java code
body: <p>I am trying to use <em>Weka</em> in java code. I have successfully built the model for J48 and saved into the drive for testing purpose..however the output of classfiyinstance of a classifier is bound to one single value itself..any suggestions why is it so? </p>
  
  <pre><code>            build model function also works fine as it produces the evaluation summary which i have uploaded at the bottom
                  public J48 buildModel() throws Exception {
                          J48 j48 = new J48();
                          j48.setMinNumObj(minNumObj);
                          j48.setConfidenceFactor(confidenceFactor);
                          j48.setNumFolds(numFolds);
                          j48.setUnpruned(unPruned);
  
                          fc.setClassifier(j48);
                          fc.buildClassifier(train);
  
                          Evaluation evaluation = new Evaluation(train);
  
                          evaluation.evaluateModel(fc, train);
  
                          System.out.println(evaluation.toSummaryString());
                          attribMap.modelResult = evaluation.toSummaryString();
  
                          return j48;
                      }
  </code></pre>
  
  <p>save model also works fine ,just added for reference</p>
  
  <pre><code>                    public void saveModel(J48 j48) throws Exception {
                          String fileName = attribMap.path + "\\" + attribMap.fileName;
                          System.out.println("j48 path:" + fileName);
                          fileName = fileName + "tem.model";
                          weka.core.SerializationHelper.write(fileName, j48);
  
                      }
  </code></pre>
  
  <p>this is test the model function to which the arff file path and model file path are sent</p>
  
  <pre><code>                    public String testModel(BufferedReader reader1, String modelPath, String classAtrib) {
                          double pred = 0.0;
                          try {
                              String result = "";
                              test = new Instances(reader1);
                              test.setClassIndex(test.numAttributes() - 1);
                              System.out.println("numattrib:" + test.numAttributes());
                              System.out.println("model path:" + modelPath);
                              J48 cls = (J48) weka.core.SerializationHelper.read("H:\\C-DAC\\autoGeneratedFiles\\tem.model");
                              System.out.println(cls.toSummaryString());
                              System.out.println("test num instances:" + test.numInstances());
                              System.out.println("b4 pred: " + test.instance(0));
                              Evaluation eTest = new Evaluation(test);
                              double[] d = eTest.evaluateModel(cls, test);
                              for (int i = 0; i &lt; d.length; i++) {
                                  System.out.println("val " + d[i]);
                              }
  
                              for (int i = 0; i &lt; test.numInstances(); i++) {
                                  System.out.println(", predicted: " + "pred value" + pred + "::::" + test.classAttribute().value((int) pred));
  
                              }
                              System.out.println("af pred:" + pred);
                              result = test.classAttribute().value((int) pred);
                              attribMap.modelTestResult = "Predicted value for " + classAtrib + " is " + result + "for the given inputs of ";
                              return result;
  
                          } catch (Exception e) {
                              e.printStackTrace();
                              System.out.println("PR`enter code here`EDDD : " + pred + " " + test.numInstances());
  
                          }
                          return null;
                      }
  </code></pre>
  
  <p>Here the output will be generated but the pred variable always sticks onto one single value</p>
  
  <p>here is the arff file that i am trying to test 
          Train Data<br>
                  @relation j48</p>
  
  <pre><code>            @attribute category_name {OBC,GEN,SC,ST}
              @attribute gender numeric
              @attribute paper numeric
              @attribute state_permanent numeric
              @attribute age numeric
  
              @data
              OBC,1,11,2,0.01
              GEN,2,11,35,0.01
              OBC,2,11,21,0.01
              OBC,2,5,32,0.01
              OBC,2,17,16,0.01
              GEN,2,5,34,0.01
              SC,2,8,21,0.01
              GEN,1,11,20,0.01
              OBC,1,5,12,0.01
              OBC,1,10,18,0.01
              GEN,2,10,2,0.01
              GEN,1,14,16,0.01
              OBC,2,17,17,0.01
              OBC,2,17,21,0.01
              OBC,2,17,32,0.01
              SC,1,8,32,0.01
  </code></pre>
  
  <p>This summary is for the for training data</p>
  
  <pre><code>        evaluation summary 
          Correctly Classified Instances         362               53.4712 %
          Incorrectly Classified Instances       315               46.5288 %
          Kappa statistic                          0.1393
          Mean absolute error                      0.2959
          Root mean squared error                  0.3846
          Relative absolute error                 93.3196 %
          Root relative squared error             96.654  %
          Coverage of cases (0.95 level)         100      %
          Mean rel. region size (0.95 level)      92.9838 %
          Total Number of Instances              677
  </code></pre>
  
  <p>Here is Test Data that is being given to the model,Hence i used test.numattributes-1 as my last attribute is the class attribute</p>
  
  <pre><code>       @relation j48
  
  
              @attribute gender numeric
              @attribute paper numeric
              @attribute state_permanent numeric
              @attribute age numeric
        @attribute category_name {OBC,GEN,SC,ST}
  
              @data
              1,11,2,0.01,?
  
           This is test data where ? tends to be the value that model has to predict but the problem is model is predicting the same value for any given input
  </code></pre>