jml.classification
Class MultiClassSVM

java.lang.Object
  extended by jml.classification.Classifier
      extended by jml.classification.MultiClassSVM
All Implemented Interfaces:
java.io.Serializable

public class MultiClassSVM
extends Classifier

Multi-class SVM using LIBLINEAR library.

Version:
1.0, Jan. 3rd, 2013
Author:
Mingjie Qian
See Also:
Serialized Form

Field Summary
private  double bias
          Dummy feature, aiming to remove the equality constraint for the dual problem of SVM.
(package private)  double C
          Parameter for loss term of linear multi-class SVM.
(package private)  double eps
          Convergence tolerance.
(package private)  de.bwaldvogel.liblinear.Feature[][] features
          Feature 2D array, indices start from 1.
(package private)  de.bwaldvogel.liblinear.Model model
          SVM model.
(package private)  de.bwaldvogel.liblinear.Parameter parameter
          A Parameter instance for linear multi-class SVM.
(package private)  de.bwaldvogel.liblinear.Problem problem
          A Problem object for this MCSVM classifier.
private static long serialVersionUID
           
 
Fields inherited from class jml.classification.Classifier
epsilon, IDLabelMap, labelIDs, labels, nClass, nExample, nFeature, W, X, Y
 
Constructor Summary
MultiClassSVM()
           
MultiClassSVM(double C, double eps)
          Maximal feature index, not including the bias feature.
 
Method Summary
(package private) static double atof(java.lang.String s)
           
(package private) static int atoi(java.lang.String s)
           
private static de.bwaldvogel.liblinear.Problem constructProblem(java.util.List<java.lang.Integer> vy, java.util.List<de.bwaldvogel.liblinear.Feature[]> vx, int max_index, double bias)
           
static org.apache.commons.math.linear.RealMatrix features2Matrix(de.bwaldvogel.liblinear.Feature[][] features)
          Convert features to original data matrix.
static org.apache.commons.math.linear.RealMatrix features2Matrix(de.bwaldvogel.liblinear.Feature[][] features, double bias)
          Convert features to matrix including bias features if bias is nonnegative.
static org.apache.commons.math.linear.RealMatrix features2MatrixWithoutBias(de.bwaldvogel.liblinear.Feature[][] features)
          Convert features to original data matrix.
static org.apache.commons.math.linear.RealMatrix features2MatrixWithoutBias(de.bwaldvogel.liblinear.Feature[][] features, double bias)
          Convert features to matrix excluding bias features if bias is nonnegative.
 void feedData(org.apache.commons.math.linear.RealMatrix X)
          Feed training data with original data matrix for this classifier.
 void feedProblem(java.util.ArrayList<java.lang.String> feaArray)
          Feed a problem from a string array.
 void feedProblem(de.bwaldvogel.liblinear.Problem problem)
          Feed a problem for this SVM classifier.
 void feedProblem(java.lang.String filePath)
          Feed a problem from a file with the LIBLINEAR input data format.
static int getMaxRawFeatureIndex(de.bwaldvogel.liblinear.Feature[][] features, double bias)
          Get the maximal feature index not including the bias feature.
 void loadModel(java.lang.String filePath)
          Load the model for a classifier.
 void loadSVMModel(java.lang.String filePath)
           
static void main(java.lang.String[] args)
           
static de.bwaldvogel.liblinear.Feature[][] matrix2Features(org.apache.commons.math.linear.RealMatrix A)
          Convert original data matrix into Feature[][] including bias features.
static de.bwaldvogel.liblinear.Feature[][] matrix2Features(org.apache.commons.math.linear.RealMatrix A, double bias)
          Convert original data matrix into Feature[][] including bias features if bias is nonnegative.
 int[] predict(de.bwaldvogel.liblinear.Feature[][] features)
          Predict labels for the test data formated as a feature 2D array.
 int[] predict2(de.bwaldvogel.liblinear.Feature[][] data, int[] labels)
          Deprecated. 
static int[] predict2(de.bwaldvogel.liblinear.Model model, de.bwaldvogel.liblinear.Feature[][] data, int[] labels)
          Deprecated. 
 int[] predict2(org.apache.commons.math.linear.RealMatrix Xt)
          Deprecated. 
 int[] predict2(org.apache.commons.math.linear.RealMatrix Xt, int[] labels)
          Deprecated. 
 org.apache.commons.math.linear.RealMatrix predictLabelScoreMatrix(org.apache.commons.math.linear.RealMatrix Xt)
          Predict label matrix for column vectors of an original data matrix.
static de.bwaldvogel.liblinear.Problem readProblem(java.lang.String filePath)
          Read a problem from a file with the LIBSVM data format.
static de.bwaldvogel.liblinear.Problem readProblemFromFile(java.io.File file, double bias)
          Data format (index starts from 1):

static de.bwaldvogel.liblinear.Problem readProblemFromStringArray(java.util.ArrayList<java.lang.String> feaArray)
          Read a problem from a string array with default bias feature (1.0).
static de.bwaldvogel.liblinear.Problem readProblemFromStringArray(java.util.ArrayList<java.lang.String> feaArray, double bias)
          Read a problem from a string array.
private  void reconstructProjectionMatrix()
           
static void run(java.lang.String[] args)
          Run this module with arguments in a String array.
 void saveModel(java.lang.String filePath)
          Save the model for a classifier.
 void saveSVMModel(java.lang.String filePath)
           
private static void showUsage()
          Show usage.
 void train()
          Train the classifier.
 
Methods inherited from class jml.classification.Classifier
calcNumClass, feedData, feedLabels, feedLabels, feedLabels, getAccuracy, getIDLabelMap, getLabelIDMap, getProjectionMatrix, getTrainingLabelMatrix, labelIndexArray2LabelMatrix, labelScoreMatrix2LabelIndexArray, predict, predict, predictLabelMatrix, predictLabelMatrix, predictLabelScoreMatrix
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

problem

de.bwaldvogel.liblinear.Problem problem
A Problem object for this MCSVM classifier.


features

de.bwaldvogel.liblinear.Feature[][] features
Feature 2D array, indices start from 1.


C

double C
Parameter for loss term of linear multi-class SVM.


eps

double eps
Convergence tolerance.


parameter

de.bwaldvogel.liblinear.Parameter parameter
A Parameter instance for linear multi-class SVM.


bias

private double bias
Dummy feature, aiming to remove the equality constraint for the dual problem of SVM.


model

de.bwaldvogel.liblinear.Model model
SVM model.

Constructor Detail

MultiClassSVM

public MultiClassSVM(double C,
                     double eps)
Maximal feature index, not including the bias feature.


MultiClassSVM

public MultiClassSVM()
Method Detail

main

public static void main(java.lang.String[] args)
Parameters:
args -

run

public static void run(java.lang.String[] args)
Run this module with arguments in a String array.

Parameters:
args - command line arguments

showUsage

private static void showUsage()
Show usage.


feedProblem

public void feedProblem(java.util.ArrayList<java.lang.String> feaArray)
Feed a problem from a string array.

Parameters:
feaArray - a string array of which each element is a index value feature representation for an example with the LIBSVM input data format

feedProblem

public void feedProblem(java.lang.String filePath)
Feed a problem from a file with the LIBLINEAR input data format.

Parameters:
filePath - file path for a file with the LIBLINEAR input data format

feedProblem

public void feedProblem(de.bwaldvogel.liblinear.Problem problem)
Feed a problem for this SVM classifier.

Parameters:
problem - a Problem object

feedData

public void feedData(org.apache.commons.math.linear.RealMatrix X)
Feed training data with original data matrix for this classifier. For MultiClassSVM, the original data matrix should not contain the bias dummy feature.

Overrides:
feedData in class Classifier
Parameters:
X - original data matrix without bias dummy features

getMaxRawFeatureIndex

public static int getMaxRawFeatureIndex(de.bwaldvogel.liblinear.Feature[][] features,
                                        double bias)
Get the maximal feature index not including the bias feature.

Parameters:
features - a 2D feature array
bias - dummy bias feature value
Returns:
maximal raw feature index not including the bias feature

train

public void train()
Description copied from class: Classifier
Train the classifier.

Specified by:
train in class Classifier

predict2

@Deprecated
public static int[] predict2(de.bwaldvogel.liblinear.Model model,
                                        de.bwaldvogel.liblinear.Feature[][] data,
                                        int[] labels)
Deprecated. 


predict2

@Deprecated
public int[] predict2(de.bwaldvogel.liblinear.Feature[][] data,
                                 int[] labels)
Deprecated. 


predict2

@Deprecated
public int[] predict2(org.apache.commons.math.linear.RealMatrix Xt)
Deprecated. 


predict2

@Deprecated
public int[] predict2(org.apache.commons.math.linear.RealMatrix Xt,
                                 int[] labels)
Deprecated. 


readProblemFromFile

public static de.bwaldvogel.liblinear.Problem readProblemFromFile(java.io.File file,
                                                                  double bias)
Data format (index starts from 1):

 +1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1 
 -1 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1 
 

Parameters:
file -
bias -
Returns:
a Problem instance holding features and labels

readProblem

public static de.bwaldvogel.liblinear.Problem readProblem(java.lang.String filePath)
Read a problem from a file with the LIBSVM data format.

Parameters:
filePath - file path
Returns:
a Problem instance holding features and labels

matrix2Features

public static de.bwaldvogel.liblinear.Feature[][] matrix2Features(org.apache.commons.math.linear.RealMatrix A)
Convert original data matrix into Feature[][] including bias features. Bias is always set to 1 automatically.

Parameters:
A - original data matrix with each column being a sample
Returns:
a 2D feature array

matrix2Features

public static de.bwaldvogel.liblinear.Feature[][] matrix2Features(org.apache.commons.math.linear.RealMatrix A,
                                                                  double bias)
Convert original data matrix into Feature[][] including bias features if bias is nonnegative.

Parameters:
A - original data matrix with each column being a sample
bias - dummy bias feature, i.e., 1
Returns:
a 2D feature array

readProblemFromStringArray

public static de.bwaldvogel.liblinear.Problem readProblemFromStringArray(java.util.ArrayList<java.lang.String> feaArray)
Read a problem from a string array with default bias feature (1.0).

Parameters:
feaArray - a ArrayList<String>, each element is a string with LIBSVM data format
Returns:
a Problem instance holding features and labels

readProblemFromStringArray

public static de.bwaldvogel.liblinear.Problem readProblemFromStringArray(java.util.ArrayList<java.lang.String> feaArray,
                                                                         double bias)
Read a problem from a string array.

Parameters:
feaArray - a ArrayList<String>, each element is a string with LIBSVM data format
bias - a real number to append the feature vector e.g., 1
Returns:
a Problem instance holding features and labels

constructProblem

private static de.bwaldvogel.liblinear.Problem constructProblem(java.util.List<java.lang.Integer> vy,
                                                                java.util.List<de.bwaldvogel.liblinear.Feature[]> vx,
                                                                int max_index,
                                                                double bias)

atof

static double atof(java.lang.String s)
Parameters:
s - the string to parse for the double value
Throws:
java.lang.IllegalArgumentException - if s is empty or represents NaN or Infinity
java.lang.NumberFormatException - see Double.parseDouble(String)

atoi

static int atoi(java.lang.String s)
         throws java.lang.NumberFormatException
Parameters:
s - the string to parse for the integer value
Throws:
java.lang.IllegalArgumentException - if s is empty
java.lang.NumberFormatException - see Integer.parseInt(String)

features2Matrix

public static org.apache.commons.math.linear.RealMatrix features2Matrix(de.bwaldvogel.liblinear.Feature[][] features)
Convert features to original data matrix. Features must always have bias of 1.

Parameters:
features - a 2D feature array
Returns:
a real matrix

features2Matrix

public static org.apache.commons.math.linear.RealMatrix features2Matrix(de.bwaldvogel.liblinear.Feature[][] features,
                                                                        double bias)
Convert features to matrix including bias features if bias is nonnegative. Feature indices start from 1.

Parameters:
features - a 2D feature array
bias - dummy bias feature
Returns:
a real matrix

features2MatrixWithoutBias

public static org.apache.commons.math.linear.RealMatrix features2MatrixWithoutBias(de.bwaldvogel.liblinear.Feature[][] features)
Convert features to original data matrix. Features must always have bias of 1.

Parameters:
features - a 2D feature array
Returns:
a real matrix

features2MatrixWithoutBias

public static org.apache.commons.math.linear.RealMatrix features2MatrixWithoutBias(de.bwaldvogel.liblinear.Feature[][] features,
                                                                                   double bias)
Convert features to matrix excluding bias features if bias is nonnegative.

Parameters:
features - a 2D feature array
bias - dummy bias feature
Returns:
a real matrix

predictLabelScoreMatrix

public org.apache.commons.math.linear.RealMatrix predictLabelScoreMatrix(org.apache.commons.math.linear.RealMatrix Xt)
Predict label matrix for column vectors of an original data matrix. The input matrix must not include bias, i.e., original data matrix is expected. Note that the columns of W is arranged according to label IDs. Thus the first column of W will be the projector vector for the label with ID 0, i.e., the first observed class label.

Specified by:
predictLabelScoreMatrix in class Classifier
Parameters:
Xt - test data matrix with each column being a feature vector
Returns:
predicted N x K label matrix, where N is the number of test samples, and K is the number of classes

predict

public int[] predict(de.bwaldvogel.liblinear.Feature[][] features)
Predict labels for the test data formated as a feature 2D array. The original data matrix should not has bias features. How do the returned labels look like depends on model.labels, which is an IDLabelMap.

Parameters:
features - a 2D feature array
Returns:
a label array with original integer label code

loadSVMModel

public void loadSVMModel(java.lang.String filePath)

reconstructProjectionMatrix

private void reconstructProjectionMatrix()

saveSVMModel

public void saveSVMModel(java.lang.String filePath)

loadModel

public void loadModel(java.lang.String filePath)
Description copied from class: Classifier
Load the model for a classifier.

Specified by:
loadModel in class Classifier
Parameters:
filePath - file path to load the model

saveModel

public void saveModel(java.lang.String filePath)
Description copied from class: Classifier
Save the model for a classifier.

Specified by:
saveModel in class Classifier
Parameters:
filePath - file path to save the model