LibSVM

Download LibSVM here

svm_model = libsvmtrain(supery, newx',sprintf(['-s 0 -t 0 -c'...
 'f -w1 %.9f -q'], mining_params.SVMC, wpos));

parameter C in the SVM optimization problem is a positive cost factor that penalizes misclassified training examples. A larger discourages misclassification more than a smaller .

LIBSVM使用的数据格式
该软件使用的训练数据和检验数据文件格式如下,一行一条记录数据:
[label] [index1]:[value1] [index2]:[value2] …
[label] [index1]:[value1] [index2]:[value2] …

e.g.
3 1: 0.736000 2: 0.12600 3:2
意思是(0.736000, 0.12600, 2)属于class为3的类别

如果数据不是很大,在libsvm的tools文件中为大家提供了easy.py。它可以自动完成从归一化到参数选择的一切所需操作。
使用方法为:

easy.py training_file [testing_file]

e.g. DOS-CMD 下:
>python grid.py tdata
跑完后会得到交叉验证所得的参数C和G

参数含义:
-s svm类型:设置SVM 类型,默认值为0,可选类型有:

0 -- C- SVC
1 -- nu - SVC
2 -- one-class-SVM
3 -- e - SVR
4 -- nu-SVR

-t 核函数类型:设置核函数类型,默认值为2,可选类型有:

0 -- 线性核:u'*v
1 -- 多项式核:(g*u'*v+ coef0)degree
2 -- RBF 核:exp(-||u-v||*||u-v||/g*g)
3 -- sigmoid 核:tanh(g*u'*v+ coef 0)

-c cost:设置C-SVC、e-SVR、n-SVR 中惩罚系数C,默认值为1
-wi weight:对各类样本的惩罚系数C加权,默认值为1

训练后的模型保存为文件*.model,用记事本打开其内容如下:
svm_type c_svc % 训练所采用的svm类型,此处为C-SVC
kernel_type rbf %训练采用的核函数类型,此处为RBF核
gamma 0.0769231 %设置核函数中的g ,默认值为1/ k
nr_class 2 %分类时的类别数,此处为两分类问题
total_sv 132 %总共的支持向量个数
rho 0.424462 %决策函数中的常数项b
label 1 -1%类别标签
nr_sv 64 68 %各类别标签对应的支持向量个数
SV %以下为支持向量

1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1
0.5104832128985164 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5
1 1:0.333333 2:1 3:-1 4:-0.245283 5:-0.506849 6:-1 7:-1 8:0.129771 9:-1 10:-0.16129 12:0.333333 13:-1
1 1:0.208333 2:1 3:0.333333 4:-0.660377 5:-0.525114 6:-1 7:1 8:0.435115 9:-1 10:-0.193548 12:-0.333333 13:1

libsvm used in Matlab: 下载libsvm-mat-3.0-1 然后在matlab下运行make.m 将c文件编译成mex文件,此时就可以在matlab命令窗口下测试是否make成功:

>> svmtrain
Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');
libsvm_options:
-s svm_type : set type of SVM (default 0)
0 -- C-SVC
1 -- nu-SVC
2 -- one-class SVM
3 -- epsilon-SVR
4 -- nu-SVR
-t kernel_type : set type of kernel function (default 2)
0 -- linear: u'*v
1 -- polynomial: (gamma*u'*v + coef0)^degree
2 -- radial basis function: exp(-gamma*|u-v|^2)
3 -- sigmoid: tanh(gamma*u'*v + coef0)
4 -- precomputed kernel (kernel values in training_instance_matrix)
-d degree : set degree in kernel function (default 3)
-g gamma : set gamma in kernel function (default 1/num_features)
-r coef0 : set coef0 in kernel function (default 0)
-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
-m cachesize : set cache memory size in MB (default 100)
-e epsilon : set 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)
Wi 给各类样本加权,比如测试数据有两类,标签分别为1 -1 如果之前设置了c=14,此时再用 -w1 0.5 -w-1 10 参数设置 则此时标签为1的这类的惩罚参数变为0.5*14 = 7,标签为-1的这类的惩罚参数变为10*14 = 140,-1类的样本远小于1类样本,故加大惩罚系数
ans =
[]
How to use SVM for unbalanced datasets

手动使用libsvm进行多分类的方法
下面方法是通过分析easy.py所得,均在“运行”cmd命令行执行(svm-train、svm-scale、svm-predict等名字为windows文件夹中exe文件的名字):
1.规范化训练样本
CODE:

svm-scale -s svmtra_iris.txt.range svmtra_iris.txt > svmtra_iris.txt.scale

2.交叉验证寻找最优参数
CODE:

grid.py -svmtrain svm-train -gnuplot gnuplot svmtra_iris.txt.scale

3.利用寻找的参数训练模型
CODE:
svm-train -c 8.0 -g 0.03125 svmtra_iris.txt.scale svmtra_iris.txt.model
4.规范化测试样本
CODE:

svm-scale -r svmtra_iris.txt.range svmtes_iris.txt > svmtes_iris.txt.scale

5.使用模型进行分类预测
CODE:

svm-predict svmtes_iris.txt.scale svmtra_iris.txt.model svmtes_iris.txt.predict
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: