libsvm 标记;

用 libsvm 做回归的时候, sample code 如下:

>> x = [0: 0.01: 1];
>> x = x';
>> y = x.^5 - x.^4 + x.^3 - x.^2 + x - 1;
>> plot(x,  y, 'r');
>> svmtrain(y, x, '-s 3 -t 1 -d 5 -g 1 -r 0.1 -c 100 -p 0.1')

参数含义如下:

"Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');\n"
 "libsvm_options:\n"
 "-s svm_type : set type of SVM (default 0)\n"
 " 0 -- C-SVC\n"
 " 1 -- nu-SVC\n"
 " 2 -- one-class SVM\n"
 " 3 -- epsilon-SVR\n"
 " 4 -- nu-SVR\n"
 "-t kernel_type : set type of kernel function (default 2)\n"
 " 0 -- linear: u'*v\n"
 " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n"
 " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n"
 " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n"
 " 4 -- precomputed kernel (kernel values in training_instance_matrix)\n"
 "-d degree : set degree in kernel function (default 3)\n"
 "-g gamma : set gamma in kernel function (default 1/k)\n"
 "-r coef0 : set coef0 in kernel function (default 0)\n"
 "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n"
 "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n"
 "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n"
 "-m cachesize : set cache memory size in MB (default 100)\n"
 "-e epsilon : set tolerance of termination criterion (default 0.001)\n"
 "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n"
 "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n"
 "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n"
 "-v n : n-fold cross validation mode\n"
 "-q : quiet mode (no outputs)\n"

以上选择的核函数是: (g*u'*v + r)^d   ==> g , r, d 为我要的参数, 即为 (1*u'*v + 0.1)^5 

 

返回值:

    Parameters: [5x1 double]    // 计算内积的时候要用
      nr_class: 2                          //  2 类问题, 回归问题也是转化为 2 类问题处理的
       totalSV: 6                            // 6 个支持向量
           rho: 0.8905                   //  y = w'* f(x) + b ; 中的 b
         Label: []
         ProbA: []
         ProbB: []
           nSV: []
       sv_coef: [6x1 double] 
           SVs: [6x1 double]

svmpredict 如下:

  double *sv_coef = model->sv_coef[0];
  double sum = 0;
  for(int i=0;i<model->l;i++)
   sum += sv_coef[i] * Kernel::k_function(x,model->SV[i],model->param);
  sum -= model->rho[0];
  *dec_values = sum;

  if(model->param.svm_type == ONE_CLASS)
   return (sum>0)?1:-1;
  else
   return sum;

 

其中: Kernel::k_function(x,model->SV[i],model->param);  就是上面的核函数;

 

具体计算一个吧:

[y mse] = svmpredict(1, 0.5, m);    y = -0.7464

计算如下:

 

rho = 0.8905

>> m.sv_coef

ans =

 -100.0000
  100.0000
    1.8509
   -0.3086
   -0.3178
   -1.2245

>> m.SVs

ans =

// 第一个没有, 那就是 0 了;

   (2,1)       0.4100  

   (3,1)       0.4200
   (4,1)       0.9500
   (5,1)       0.9600
   (6,1)       0.9700

 

算 0.5 ,过程如下:

y1 = -100*(1*0.5*0 + 0.1)^5 + 100*(1*0.5*0.4100 + 0.1)^5 + 1.8509*(1*0.5*0.4200 + 0.1)^5 -0.3086*(1*0.5*0.9500 + 0.1)^5-0.3178*(1*0.5*0.9600 + 0.1)^5 - 1.2245*(1*0.5*0.9700 + 0.1)^5 - 0.8905 = 0.7464;

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值