title: Libsvm java training testing example(also in real time)
body: <p>Anybody can help me by providing libsvm java example for training and testing. I am new in Machine learning and need help regarding the same. Earlier provided example by @machine learner have error giving only one class result. I don't want to use weka as suggestion given in earlier post.</p>
  
  <p>Or can you rectify error in this code it always predict one class in result.(I want to perform multiclassification). <br><br>This example is given by "Machine learner"</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  import libsvm.*;
  
  public class Test{
      public static void main(String[] args) throws Exception{
  
          // Preparing the SVM param
          svm_parameter param=new svm_parameter();
          param.svm_type=svm_parameter.C_SVC;
          param.kernel_type=svm_parameter.RBF;
          param.gamma=0.5;
          param.nu=0.5;
          param.cache_size=20000;
          param.C=1;
          param.eps=0.001;
          param.p=0.1;
  
          HashMap&lt;Integer, HashMap&lt;Integer, Double&gt;&gt; featuresTraining=new HashMap&lt;Integer, HashMap&lt;Integer, Double&gt;&gt;();
          HashMap&lt;Integer, Integer&gt; labelTraining=new HashMap&lt;Integer, Integer&gt;();
          HashMap&lt;Integer, HashMap&lt;Integer, Double&gt;&gt; featuresTesting=new HashMap&lt;Integer, HashMap&lt;Integer, Double&gt;&gt;();
  
          HashSet&lt;Integer&gt; features=new HashSet&lt;Integer&gt;();
  
          //Read in training data
          BufferedReader reader=null;
          try{
              reader=new BufferedReader(new FileReader("a1a.train"));
              String line=null;
              int lineNum=0;
              while((line=reader.readLine())!=null){
                  featuresTraining.put(lineNum, new HashMap&lt;Integer,Double&gt;());
                  String[] tokens=line.split("\\s+");
                  int label=Integer.parseInt(tokens[0]);
                  labelTraining.put(lineNum, label);
                  for(int i=1;i&lt;tokens.length;i++){
                      String[] fields=tokens[i].split(":");
                      int featureId=Integer.parseInt(fields[0]);
                      double featureValue=Double.parseDouble(fields[1]);
                      features.add(featureId);
                      featuresTraining.get(lineNum).put(featureId, featureValue);
                  }
              lineNum++;
              }
  
              reader.close();
          }catch (Exception e){
  
          }
  
          //Read in test data
          try{
              reader=new BufferedReader(new FileReader("a1a.t"));
              String line=null;
              int lineNum=0;
              while((line=reader.readLine())!=null){
  
                  featuresTesting.put(lineNum, new HashMap&lt;Integer,Double&gt;());
                  String[] tokens=line.split("\\s+");
                  for(int i=1; i&lt;tokens.length;i++){
                      String[] fields=tokens[i].split(":");
                      int featureId=Integer.parseInt(fields[0]);
                      double featureValue=Double.parseDouble(fields[1]);
                      featuresTesting.get(lineNum).put(featureId, featureValue);
                  }
              lineNum++;
              }
              reader.close();
          }catch (Exception e){
  
          }
  
          //Train the SVM model
          svm_problem prob=new svm_problem();
          int numTrainingInstances=featuresTraining.keySet().size();
          prob.l=numTrainingInstances;
          prob.y=new double[prob.l];
          prob.x=new svm_node[prob.l][];
  
          for(int i=0;i&lt;numTrainingInstances;i++){
              HashMap&lt;Integer,Double&gt; tmp=featuresTraining.get(i);
              prob.x[i]=new svm_node[tmp.keySet().size()];
              int indx=0;
              for(Integer id:tmp.keySet()){
                  svm_node node=new svm_node();
                  node.index=id;
                  node.value=tmp.get(id);
                  prob.x[i][indx]=node;
                  indx++;
              }
  
              prob.y[i]=labelTraining.get(i);
          }
  
          svm_model model=svm.svm_train(prob,param);
  
          for(Integer testInstance:featuresTesting.keySet()){
              HashMap&lt;Integer, Double&gt; tmp=new HashMap&lt;Integer, Double&gt;();
              int numFeatures=tmp.keySet().size();
              svm_node[] x=new svm_node[numFeatures];
              int featureIndx=0;
              for(Integer feature:tmp.keySet()){
                  x[featureIndx]=new svm_node();
                  x[featureIndx].index=feature;
                  x[featureIndx].value=tmp.get(feature);
                  featureIndx++;
              }
  
              double d=svm.svm_predict(model, x);
  
              System.out.println(testInstance+"\t"+d);
          }
  
      }
  }
  </code></pre>