Friday, 27 September 2013

Retrieve weka weitghts from Linear Regression

Weka function to retrieve the linear regression weights:

import java.util.List;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.SMOreg;
import weka.classifiers.functions.supportVector.Kernel
.....


public double[] getCoefficients(){
       String svmClassifier = svm.toString();
       List<Double> coefficents = new ArrayList<Double>();
       double[] coeficcientsArray = null;
       if(svmClassifier.contains("weights (not support vectors):")){
          String[] svmClassStrings = svmClassifier.split(System.lineSeparator());
          int i = 0;
          boolean lastFound = false;
          while( i < svmClassStrings.length && !lastFound){
              String currentString = svmClassStrings[i];
              if(currentString.contains("weights (not support vectors):")){
                  i++;
                  while(i < svmClassStrings.length && !lastFound){
                      currentString = svmClassStrings[i];
                      String[] parts = currentString.split("\\s+");
                      if(parts.length < 5){
                          lastFound = true;
                      }else{
                          String number = parts[1] + parts[2];
                          coefficents.add(Double.parseDouble(number));
                      }
                      i++;
                  }
              }
              i++;
          }
        }