Liblinear (SVM package) instructions for use (ii) MATLAB interface

Source: Internet
Author: User
Tags svm

Liblinear (SVM package) instructions for use (ii) MATLAB interface

Liblinear (SVM package) instructions for use (ii) MATLAB interface

[Email protected]

Http://blog.csdn.net/zouxy09

First, Introduction

Liblinear is a simple package for solving large-scale, linear classification and regression. This article describes how to use the package in MATLAB. (http://www.csie.ntu.edu.tw/~cjlin/liblinear)

Second, installation

In Windows systems, pre-compiled executables are in the. \ windows directory. But we provide 64bit, if your computer is 32bit, you need to follow the following steps to recompile:

We recommend using MAKE.M to compile. Run make directly in MATLAB or octave to generate four files in the current directory: ' Libsvmread.mex ', ' Libsvmwrite.mex ', ' Train.mex ', and ' Predict.mex '.

On MATLAB or Octave:

>> make

If in MATLAB, MAKE.M error, you can try to run ' mex-setup ' to choose a suitable compiler for MEX. You need to make sure that your compiler is available.

In Unix systems, if MAKE.M and mex-setup are not valid, use Makefile. Note that our MATLAB is installed in the '/usr/local/matlab '. If not, you need to change the makefile Matlabdir to our MATLAB installation directory.

Third, usage

1. Training

Matlab> model = Train (Training_label_vector, Training_instance_matrix [, ' liblinear_options ', ' col ']);

The parameters are as follows:

-training_label_vector:

The label of the training sample, MX1 vector. Note You must be a double type.

-training_instance_matrix:

Training sample, MXN matrix. Each sample is n-dimensional. It must be sparse and must be of type double.

-liblinear_options:

Training options in string format, as in format and Liblinear.

-col:

If ' col ' is set, each column of Training_instance_matrix is a sample. Otherwise, each row is a sample.

Note: This seems to accept only two string arguments. So when you pass multiple arguments, pass them in the following way, for example: (The Predict function is the same)

Train (train_y, train_x, '-C 1-v 5 ', ' col '); That's right

Train (train_y, train_x, '-C 1 ', '-V 5 ', ' col '); Error

Train (train_y, train_x, ' col '); Error

Train (train_y, train_x, ', ' col '); That's right

2. Testing

matlab> [Predicted_label, accuracy, decision_values/prob_estimates] = Predict (Testing_label_vector, testing_ Instance_matrix, model [, ' liblinear_options ', ' col ']);

The parameters are as follows:

-testing_label_vector:

The label of the test sample, MX1 vector. If unknown, then simply use random values. Note You must be a double type.

-testing_instance_matrix:

Training sample, MXN matrix. Each sample is n-dimensional. It must be sparse and must be of type double.

-model:

Trained to get the model.

-liblinear_options:

Test options in string format, in the same format as Liblinear.

-col:

If ' col ' is set, each column of Training_instance_matrix is a sample. Otherwise, each row is a sample.

IV. model structure of the return

The train function returns a model so that we can use it for future predictions. The model is stored in a structured form, organized as [Parameters, Nr_class, nr_feature, Bias, Label, W]:

-parameters: Parameters

-nr_class: Number of classes, if regression, then this number is 2.

-nr_feature: The sample dimension of the training data (excluding bias items).

-bias: If bias >= 0, we will add an extra feature at the end of each sample.

-label: The label of each class, for regression, is empty.

-W: a nr_w x n weight matrix. N is the Nr_feature (feature dimension) or nr_feature+1 (when a bias item exists). If nr_class=2, and-S is not 4 (not a multi-class SVM for Crammer and singer), then Nr_w is 1, and for other cases, Nr_w equals Nr_class.

If you specify-V, then is the cross-check mode, the returned model is a scalar, for the classifier is the cross-check accuracy rate, for the return, is the mean square error.

V. Forecast results

The function predict has three outputs. The first one is the vector Predicted_label of the predicted label composition. The second output is the accuracy rate, which is a vector containing the accuracy (for classification), the mean square error, and the square correlation coefficient (for regression). The third is a matrix that contains the decision value or probability value (if '-B 1 ' is specified). If k is the number of classes, K ' is the number of classifiers (if k=2 k ' = 1, otherwise k ' =k), that to the decision value matrix, each line includes the results of the K ' two classifier. for the probability value matrix, each row includes k values, each representing the probability values for each type of test sample. Note that the sequence of classes here is the same as the ' Label ' field in the model structure.

Vi. Other Tools

[Label_vector, Instance_matrix] = Libsvmread (' data.txt ');

The Libsvmread function can read files in LIBSVM format. The two outputs are label labels and sample set instances, which can be used as input to Svmtrain or svmpredict.

Libsvmwrite (' Data.txt ', Label_vector, Instance_matrix]

Libsvmwrite can write parameters to a file in LIBSVM format. Instance_matrix must be a sparse matrix. Type must be a double type. On windows, ' Libsvmread.mexw64 ' and ' libsvmwrite.mexw64 ' have been compiled after the directory '. \ Windows ' down.

Vii. examples

We provide data Heart_scale for training and testing.

matlab> [Heart_scale_label, Heart_scale_inst] = Libsvmread ('.. /heart_scale '); % Read Data

Matlab> model = Train (Heart_scale_label, Heart_scale_inst, '-C 1 '); % Training

matlab> [Predict_label, accuracy, dec_values] = Predict (Heart_scale_label, heart_scale_inst, model); % test

For probability estimation, you need to add the option '-B 1 ' to the test phase:

matlab> [Predict_label, accuracy, prob_estimates] = Predict (Heart_scale_label, heart_scale_inst, model, '-B 1 ');

It is important to note that the operation of the above instructions requires us to confirm our directory, and the directory where the Mex files are located. We can also create a new TEST_HEART_SCALE.M file in the MATLAB directory:

Clear CLC

Tic

[Heart_scale_label, Heart_scale_inst] = Libsvmread ('.. /heart_scale '); % Read Data

Model = Train (Heart_scale_label, Heart_scale_inst, '-C 1 '); % Training

[Predict_label, accuracy, dec_values] = Predict (Heart_scale_label, heart_scale_inst, model); % test

ToC

Run the above file, output:

.......*

Optimization finished, #iter = 71

Objective value =-121.108744

NSV = 194

accuracy = 84.0741% (227/270)

Elapsed time is 0.006373 seconds.

So fast!!!

Liblinear (SVM package) instructions for use (ii) MATLAB interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.