LIBSVM做回归预测

1 LIBSVM是台湾大学林智仁(Lin Chih-Jen) 副教授 等开发设计的一个简单、易于使用和快速有效的SVM 模式识别 与回归的 软件 包,他不但提供了编译好的可在Windows系列系统的执行文件,还提供了 源代码 ,方便改进、修改以及在其它 操作系统 上应用;该软件对SVM所涉及的参数调节相对比较少,提供了很多的默认参数,利用这些默认参数可以解决很多问题;并提供了交互检验(Cross Validation)的功能。

  官网:http://www.csie.ntu.edu.tw/~cjlin/libsvm/

可以从官网上下到libSVM压缩包,还有很多的数据和sample。

2 安装方法

    在解压缩后,在libsvm-X.x(X.x版本号)\matlab下的README中有如下说明:

        matlab> mex -setup        (ps: MATLAB will show the following messages to setup default compiler.)        Please choose your compiler for building external interface (MEX) files:         Would you like mex to locate installed compilers [y]/n? y        Select a compiler:         [1] Microsoft Visual C/C++ version 7.1 in C:\Program Files\Microsoft Visual Studio         [0] None         Compiler: 1        Please verify your choices:         Compiler: Microsoft Visual C/C++ 7.1         Location: C:\Program Files\Microsoft Visual Studio         Are these correct?([y]/n): y

        matlab> make

       系统安装的vc版本不同以上的输出可能不同。

3 使用方法(摘自libsvm-X.x(X.x版本号)\matlab下的README

Examples
========


Train and test on the provided data heart_scale:


matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale');
matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07');
matlab> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model); % test the training data


For probability estimates, you need '-b 1' for training and testing:


matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale');
matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07 -b 1');
matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale');
matlab> [predict_label, accuracy, prob_estimates] = svmpredict(heart_scale_label, heart_scale_inst, model, '-b 1');


To use precomputed kernel, you must include sample serial number as
the first column of the training and testing data (assume your kernel
matrix is K, # of instances is n):


matlab> K1 = [(1:n)', K]; % include sample serial number as first column
matlab> model = svmtrain(label_vector, K1, '-t 4');
matlab> [predict_label, accuracy, dec_values] = svmpredict(label_vector, K1, model); % test the training data


We give the following detailed example by splitting heart_scale into
150 training and 120 testing data.  Constructing a linear kernel
matrix and then using the precomputed kernel gives exactly the same
testing error as using the LIBSVM built-in linear kernel.


matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale');
matlab>
matlab> % Split Data
matlab> train_data = heart_scale_inst(1:150,:);
matlab> train_label = heart_scale_label(1:150,:);
matlab> test_data = heart_scale_inst(151:270,:);
matlab> test_label = heart_scale_label(151:270,:);
matlab>
matlab> % Linear Kernel
matlab> model_linear = svmtrain(train_label, train_data, '-t 0');
matlab> [predict_label_L, accuracy_L, dec_values_L] = svmpredict(test_label, test_data, model_linear);
matlab>
matlab> % Precomputed Kernel
matlab> model_precomputed = svmtrain(train_label, [(1:150)', train_data*train_data'], '-t 4');
matlab> [predict_label_P, accuracy_P, dec_values_P] = svmpredict(test_label, [(1:120)', test_data*train_data'], model_precomputed);
matlab>
matlab> accuracy_L % Display the accuracy using linear kernel
matlab> accuracy_P % Display the accuracy using precomputed kernel

另外,http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/有很多的测试数据集,可以学习。

 我下载了

eunite2001:http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression.html#eunite2001

保存后测试代码如下:
close all;
clear;

min = 464; % smallest max-load in 1997-1998
max = 876; % largest max-load in 1997-1998

[y,x] = libsvmread('eunite2001.txt');
m = svmtrain(y, x, '-s 3 -c 4096 -g 0.0625 -p 0.5');

[ty, tx] = libsvmread('eunite2001.t');

p = zeros(31,1)
for i=1:31,
  if i==1,
    txi = tx(i,:);
  else
    txi = [tx(i,1:9) (p(i-1)-min)/(max-min) tx(i-1,10:15)];
  end
  p(i) = svmpredict(ty(i), txi, m);
end

mape = 100/31*sum(abs((p-ty)./ty))
mse = (p-ty)'*(p-ty)/31
plot((1:31)', p, '--', (1:31)', ty, '-');
legend('predicted', 'real');
set(gca, 'fontsize', 18) ; 
set(findobj('Type', 'line'), 'LineWidth', 3)  
结果
mape = 1.9379


mse = 352.8772

图:



参考:

1 很不错的博文

http://blog.sina.com.cn/s/blog_5980835e0100drt2.html

2 libsvm官网http://www.csie.ntu.edu.tw/~cjlin/libsvm/

3  .... 

你可能感兴趣的:(matlab,svm)