libsvm python linux,python下libsvm的使用

python下libsvm的使用

使用LibSVM的一些准备工作

平台

• Win32+python+pnuplot

• Linux+python+pnuplot

数据

• Training Set

• Test Set

SVM基础知识

样本文件格式

每行格式:label feature1:value1 index2:value2 …

• label为类别标号,feature为特征序号,value为特征的值

• value为0时该项可以省略(大规模数据时节省存储空间)

示例:iris.tr(UCI / Iris Plant, 4 features, 3 classes)

1 1:-0.555556 2:0.5 3:-0.694915 4:-0.75

3 1:-0.166667 2:-0.333333 3:0.38983 4:0.916667

2 1:-0.333333 2:-0.75 3:0.0169491 4:-4.03573e-08

1 1:-0.833333 3:-0.864407 4:-0.916667

1 1:-0.611111 2:0.0833333 3:-0.864407 4:-0.916667

3 1:0.611111 2:0.333333 3:0.728813 4:1

3 1:0.222222 3:0.38983 4:0.583333

2 1:0.222222 2:-0.333333 3:0.220339 4:0.166667

2 1:-0.222222 2:-0.333333 3:0.186441 4:-4.03573e-08

格式检验脚本 checkdata.py

python checkdata.py iris.tr.txt

数据标准化

为何需要数据标准化?

1. 去除量纲

2. 简化运算

常见的标准化方法

0818b9ca8b590ca3270a3433284dd417.png

一些经验之谈

1.训练集与测试集一起标准化!

2.对于新来的测试集,怎么办?

3.对于回归问题,量纲大的标签也需要标准化,此时预测值需要反标

准化

svm-scale命令(使用的是极值标准化)

格式:svm-scale [options] filename

options:

• 上下界(默认[-1,1]):-l lower -u upper

• 存储标准化尺度:-s scalefile

• 加载标准化尺度:-r scalefile

• 标签的标准化(用于回归):-y lower upper

使用提醒

• 训练集和测试集一起scale,将所有数据缩放到[lower, upper]

• 新来的测试样本,应该使用训练时候的标准化尺度进行标准化

示例

svm-scale -s iris.scale iris.train

svm-scale -l -0.8 -u 0.8 -s iris.scale iris.train > iris.train.scaled

svm-scale -r iris.scale iris.test

svm-train 命令

svm-train 命令

格式:svm-train [options] filename [modelfile]

options(部分)

• -s svm_type : set type of SVM

• -t kernel_type : set type of kernel function

• -v n: n-fold cross validation mode

• -g gamma ( ) : set gamma in kernel function (default 1/k)

• -c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)

• -b probabilityestimates: whether to train a SVC or SVR model for probability

estimates, 0 or 1 (default 0)

• -m cachesize : set cache memory size in MB (default 100)

LibSVM 训练

示例

最简单的训练,所有参数均默认

svm-train iris.train

任务类型默认(C-SVC)、核函数默认(RBF),10-fold, c=100, g=0.01,

保存训练模型

svm-train -v 10 iris.train iris.model

任务类型默认(C-SVC),选用线性核函数,10-fold, c=100

svm-train -t 0 –v 10 –c 100 –g 0.01 iris.train iris.model

采用概率模型,其余同上

svm-train –b 1 -t 0 –v 10 –c 100 –g 0.01 iris.train iris.model

一个重要问题,怎么选择参数?

1.自动:libSVM提供的寻优脚本

2.手动: 用试的!!

LibSVM 训练参数寻优

参数自动寻优的训练脚本 grid.py (Cross-validation and Grid-search)

适用任务:分类问题 & RBF核(或linear核)

格式:python [options] grid.py trainingfilename

options

• -svmtrain pathname

• -gnuplot pathname

• -out pathname

• -png pathname

• -log2c begin,end,step

• -log2g begin,end,step

• additional same options for svm-train

示例

python grid.py iris.train

python grid.py –svmtrain d:\libsvm\svm-train.exe –gnuplot d:\gnuplot\bin\pgnuplot.exe -png d:\iris.gird.png –log2c -8,8,2 –log2g 8,-8,-2 –v 10 iris.train

linear核怎么使用?——设置一个虚拟的gamma参数: -log2g 1,1,1

python grid.py –log2c -8,8,2 –log2g 1,1,1 –t 0 –v 10 iris.train

0818b9ca8b590ca3270a3433284dd417.png

LibSVM 测试

格式:svm-predict [options] testfile modelfile resultfile

options

-b probability : -b 0只输出类别;-b 1输出各类概率

返回结果

1.各测试样本的类别(-b 1时为各类后验概率)

2.正确率

示例

svm-predict iris.test iris.model iris.result

svm-predict –b 1 iris.test iris.model iris.result

值得注意的

1. 部分任务不支持概率输出(比如SVR, one-class SVM)

2. -b参数需要svm-train的时候建立概率模型与之对应

svm-train –b 1 iris.train iris.model

新来的样本按照训练集标准化的尺度进行标准化

svm-scale -r iris.train.scale iris.test > iris.test.scaled

怎么用LibSVM 做回归?

数据标准化

对label同时进行标准化(量纲较小的时候可以忽略该步)

svm-scale -y -1 1 regression.train.scaled regression.model

参数寻优

脚本:grid.py-> gridregression.py

寻优的参数:-c -g-> -c -g –p

python gridregression.py –log2c -8,8,2 –log2g 8,-8,-2 –log2p -8,8,2 –v 10 regression.train

训练建模

1. 任务的选择:-s 3 (epsilon - SVR)

2 . 核函数的选择:通常选择 -t 2 (RBF核,默认)或-t 0(linear核)

3. 与分类任务相比,多了一个基本参数p,建模时就用寻优找到的参数

svm-train –s 3 -c 100 –g 0.01 –p 0.1 regression.train.scaled regression.model

测试

评估指标: Rate-> MSE

svm-predict regression.test regression.model regression.result

返回值需要经过反标准化,恢复原来的量纲:好像要你自己做了!

Python 平台下的libsvm - 1

安装

1.将已经编译好的svmc.pyd文件(位于libsvm\windows\python\目录下)

2.拷贝到系统环境变量目录(如python根目录)

3. 将svm.py文件拷贝到当前文件夹

4.将cross_validation.py拷贝到当前文件夹

使用

1. 导入模块

from svm import *

2.设置训练参数(包含svm_type, kernel_type, gamma, C…)

param = svm_parameter(svm_type = C_SVC, kernel_type = LINEAR)

param.kernel_type = RBF

3 加载数据

ListLable = [1, -1]

ListValue = [[1, 0, 1], [-1, 0 ,-1]]

# ListValue = [{1:1, 3:1}, {1:-1, 3:-1}]

prob = svm_problem(ListLabel, ListValue)

建模

mod = svm_model(prob, param)

target=cross_validation (prob, param, n)

模型保存与加载

mod.save(‘modelfile’)

mod2=svm_model(‘modelfile’)

测试

r = mod.predict ([1, 1, 1])

d = mod.predict_values([1, 1, 1])

prd, prb = m.predict_probability([1, 1, 1])

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值