初学liblinear的使用方法

1、主要用到的函数如下:

A、按照libsvm的数据格式读取txt文件       [label_vector, instance_matrix] = libsvmread('data.txt');

B、将数据写成SVM规定的形式                 libsvmwrite('data.txt', label_vector, instance_matrix]

                           (The instance_matrix must be a sparse matrix. (type must be double))

C、训练函数 model = train(training_label_vector, training_instance_matrix [,'liblinear_options', 'col']);

-training_label_vector: An m by 1 vector of training labels. (type must be double)
-training_instance_matrix: An m by n matrix of m training instances with n features.

                                       It must be a sparse matrix. (type must be double)
-liblinear_options:A string of training options in the same format as that of LIBLINEAR.

options:
-s type : set type of solver (default 1)


for multi-class classification
0 -- L2-regularized logistic regression (primal)
1 -- L2-regularized L2-loss support vector classification (dual)
2 -- L2-regularized L2-loss support vector classification (primal)
3 -- L2-regularized L1-loss support vector classification (dual)
4 -- support vector classification by Crammer and Singer
5 -- L1-regularized L2-loss support vector classification
6 -- L1-regularized logistic regression
7 -- L2-regularized logistic regression (dual)


for regression
11 -- L2-regularized L2-loss support vector regression (primal)
12 -- L2-regularized L2-loss support vector regression (dual)
13 -- L2-regularized L1-loss support vector regression (dual)


-c cost : set the parameter C (default 1)
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
-e epsilon : set tolerance of termination criterion


-s 0 and 2
|f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,
where f is the primal function and pos/neg are # of
positive/negative data (default 0.01)


-s 11
|f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.001)


-s 1, 3, 4 and 7
Dual maximal violation <= eps; similar to libsvm (default 0.1)


-s 5 and 6
|f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,
where f is the primal function (default 0.01)


-s 12 and 13 "
|f'(alpha)|_1 <= eps |f'(alpha0)|,
where f is the dual function (default 0.1)


-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1)
-wi weight: weights adjust the parameter C of different classes (see README for details)
-v n: n-fold cross validation mode


-C : find parameter C (only for -s 0 and 2)


-q : quiet mode (no outputs)

-col:
if 'col' is set, each column of training_instance_matrix is a data instance. Otherwise each row is a data instance.

D、预测函数

[predicted_label, accuracy, decision_values/prob_estimates] = predict(testing_label_vector, testing_instance_matrix,model [, 'liblinear_options', 'col']);

 

2、示例:用liblinear自带的heart_scale做示例

[heart_scale_label, heart_scale_inst] = libsvmread('heart_scale'); %读数据

model=train(heart_scale_label, heart_scale_inst, '-c 1');

[predict_label, accuracy, dec_values] = predict(heart_scale_label, heart_scale_inst, model);

结果:Accuracy = 84.8148% (229/270)

 

寻优函数,对s=0或2

best = train(heart_scale_label, heart_scale_inst, '-C -s 0');

将最佳的c代入

 3、模型里面的具体参数:

原文地址:https://www.cnblogs.com/liulijin/p/6776962.html