libsvm python

  1. 下载 (https://github.com/shijing888/LibSVM)

  2. 安装gnuplot:
    点击 gp503-win32-mingw.exe 安装,安装后 gnuplot.exe 的路径,我的路径为:E:\PROC\gnuplot\bin\gnuplot.exe。修改gnuplot路径,打开 tools 文件夹,分别打开 easy.py 和grid.py 在 修改gnuplot 路径

  3. 执行小程序测试:

import os
os.chdir("F:\git\svm\libsvm-3.20\python")
from svmutil import *

y, x = svm_read_problem('../heart_scale')
m=svm_train(y[:200],x[:200],'-c 4')
p_label,p_acc,p_val=svm_predict(y[:200],x[:200],m)

运行结果:
Accuracy = 91% (182/200) (classification)

optimization finished, #iter = 257
nu = 0.351161
obj = -225.628984, rho = 0.636110
nSV = 91, nBSV = 49
Total nSV = 91

常用函数

svm_read_problem() 
svm_train()   
svm_predict()    
svm_load_model()  
svm_save_model()  
evaluations()   

svm_train 三种训练写法

- model = svm_train( y, x, [training_options] )
- model = svm_train( prob, [training_options] )
- model = svm_train( prob, param)

svm_options

  • -s svm_type : (default 0)
    0 – C-SVC (multi-class classification)
    1 – nu-SVC (multi-class classification)
    2 – one-class SVM
    3 – epsilon-SVR (regression)
    4 – nu-SVR (regression)

  • -t kernel_type : (default 2)
    0 – linear: uv
    1 – polynomial: ( γuv+coef0)degree
    2 – radial basis function: exp(γ|uv|2)
    3 – sigmoid: tanh(γuv+coef0)
    4 – precomputed kernel (kernel values in training_set_file)

  • -d degree :degree in kernel function (default 3) 多项式核最高次degree

  • -g gamma : gamma in kernel function (default 1/num_features)
  • -r coef0 : coef0 in kernel function (default 0) 多项式核和sigmoid核
  • -c cost : parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
  • -n nu : parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
  • -p epsilon : epsilon in loss function of epsilon-SVR (default 0.1)
  • -m cachesize : cache memory size in MB (default 100)
  • -e epsilon : tolerance of termination criterion (default 0.001)
  • -h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
  • -b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
  • -wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
  • -v n: n-fold cross validation mode
  • -q : quiet mode (no outputs)

提高精度

  1. scale变换
F:\git\svm\libsvm-3.20\windows>svm-scale.exe -l -1 -u 1 -s 2 ..\data\train1.txt>train1_scale.txt

F:\git\svm\libsvm-3.20\windows>svm-scale.exe -l -1 -u 1 -s 2 ..\data\test1.txt>test1_scale.txt

参数说明:

-l :lower 变换后的下限(defaut: -1)
-u :upper 变换后的上限(defaut: 1)
-s svm_type
-y y_lower y_upper : y scaling limits (default: no y scaling)
-s save_filename : save scaling parameters to save_filename
-r restore_filename : restore scaling parameters from restore_filename

  1. 选最优参数
    find_parameter

参考:
http://www.codexiu.cn/python/blog/35440/
http://www.cnblogs.com/Dzhouqi/p/3653823.html

你可能感兴趣的:(python,svm,机器学习)