当前位置:网站首页>【无标题】
【无标题】
2022-07-06 19:49:00 【weixin_41958486】
使用LibSVM
LibSVM的使用非常简单,只需调用有限的接口
示例1:
from libsvm.python.svmutil import *
from libsvm.python.svm import *
y, x = [1,-1], [{1:1, 2:1}, {1:-1,2:-1}]
prob = svm_problem(y, x)
param = svm_parameter('-t 0 -c 4 -b 1')
model = svm_train(prob, param)
yt = [1]
xt = [{1:1, 2:1}]
p_label, p_acc, p_val = svm_predict(yt, xt, model)
print(p_label)
输出结果:
optimization finished, #iter = 1
nu = 0.062500
obj = -0.250000, rho = 0.000000
nSV = 2, nBSV = 0
Total nSV = 2
test:
Model supports probability estimates, but disabled in predicton.
Accuracy = 100% (1/1) (classification)
[1.0]
在SVM数据中下载train1.txt和test1.txt。
LibSVM可以在文件中读取训练数据,这样便于大规模数据的使用。
示例:
from libsvm.python.svmutil import *
from libsvm.python.svm import *
y, x = svm_read_problem('train1.txt')
yt, xt = svm_read_problem('test1.txt')
model = svm_train(y, x )
print('test:')
p_label, p_acc, p_val = svm_predict(yt[200:202], xt[200:202], model)
print(p_label)
可以看到输出:
optimization finished, #iter = 5371
nu = 0.606150
obj = -1061.528918, rho = -0.495266
nSV = 3053, nBSV = 722
Total nSV = 3053
test:
Accuracy = 40.809% (907/2225) (classification)
LibSVM接口
训练数据格式
libsvm的训练数据格式如下:
<label> <index1>:<value1> <index2>:<value2> ...
示例:
1 1:2.927699e+01 2:1.072510e+02 3:1.149632e-01 4:1.077885e+02
主要类型
svm_problem
保存定义SVM模型的训练数据
svm_parameter
存储训练SVM模型所需的各种参数
svm_model
完成训练的SVM模型
svm_node
模型中一个特征的值,只包含一个整数索引和一个浮点值属性。
主要接口:
-svm_problem(y, x)
由训练数据y,x创建svm_problem对象
svm_train()
svm_train有3个重载:
model = svm_train(y, x [, 'training_options'])
model = svm_train(prob [, 'training_options'])
model = svm_train(prob, param)
用于训练svm_model模型
- `svm_parameter(cmd)
创建svm_parameter对象,参数为字符串。
示例:
param = svm_parameter('-t 0 -c 4 -b 1')
svm_predict()
调用语法:
p_labs, p_acc, p_vals = svm_predict(y, x, model [,'predicting_options'])
参数:
y
测试数据的标签
x
测试数据的输入向量
model
为训练好的SVM模型。
返回值:
p_labs
是存储预测标签的列表。
p_acc
存储了预测的精确度,均值和回归的平方相关系数。
p_vals
在指定参数'-b 1'时将返回判定系数(判定的可靠程度)。
这个函数不仅是测试用的接口,也是应用状态下进行分类的接口。比较奇葩的是需要输入测试标签y才能进行预测,因为y不影响预测结果可以用0向量代替。
svm_read_problem
读取LibSVM格式的训练数据:
y, x = svm_read_problem('data.txt')
svm_save_model
将训练好的svm_model存储到文件中:
svm_save_model('model_file', model)
model_file的内容:
svm_type c_svc
kernel_type linear
nr_class 2
total_sv 2
rho 0
label 1 -1
probA 0.693147
probB 2.3919e-16
nr_sv 1 1
SV
0.25 1:1 2:1
-0.25 1:-1 2:-1
svm_load_model
读取存储在文件中的svm_model:
model = svm_load_model('model_file')
调整SVM参数
LibSVM在训练和预测过程中需要一系列参数来调整控制。
svm_train的参数:
-s
SVM的类型(svm_type)0 -- C-SVC(默认)
使用惩罚因子(Cost)的处理噪声的多分类器
1 -- nu-SVC(多分类器)
按照错误样本比例处理噪声的多分类器
2 -- one-class SVM
一类支持向量机,可参见"SVDD"的相关内容
3 -- epsilon-SVR(回归)
epsilon支持向量回归
4 -- nu-SVR(回归)
-t
核函数类型(kernel_type)0 -- linear(线性核):
u'*v
1 -- polynomial(多项式核):
(gamma*u'*v + coef0)^degree
2 -- radial basis function(RBF,径向基核/高斯核):
exp(-gamma*|u-v|^2)
3 -- sigmoid(S型核):
tanh(gamma*u'*v + coef0)
4 -- precomputed kernel(预计算核):
核矩阵存储在
training_set_file
中
下面是调整SVM或核函数中参数的选项:
-d
调整核函数的degree参数,默认为3-g
调整核函数的gamma参数,默认为1/num_features
-r
调整核函数的coef0参数,默认为0
-c
调整C-SVC, epsilon-SVR 和 nu-SVR中的Cost参数,默认为1
-n
调整nu-SVC, one-class SVM 和 nu-SVR中的错误率nu参数,默认为0.5
-p
调整epsilon-SVR的loss function中的epsilon参数,默认0.1
-m
调整内缓冲区大小,以MB为单位,默认100
-e
调整终止判据,默认0.001
-wi
调整C-SVC中第i个特征的Cost参数
调整算法功能的选项:
-b
是否估算正确概率,取值0 - 1,默认为0
-h
是否使用收缩启发式算法(shrinking heuristics),取值0 - 1,默认为0
-v
交叉校验-q
静默模式
Matlab
LibSVM的Matlab接口用法类似,Matlab丰富的标准工具箱提供了各种方便。
Statistic Tools工具箱提供了svmtrain和svmclassify函数进行SVM分类。
traindata = [0 1; -1 0; 2 2; 3 3; -2 -1;-4.5 -4; 2 -1; -1 -3];
group = [1 1 -1 -1 1 1 -1 -1]';
testdata = [5 2;3 1;-4 -3];
svm_struct = svmtrain(traindata,group);
Group = svmclassify(svm_struct,testdata);
svmtrain接受traindata和group两个参数,traindata以一行表示一个样本,group是与traindata中样本对应的分类结果,用1和-1表示。
svmtrain返回一个存储了训练好的svm所需的参数的结构体svm_struct。
svmclassify接受svm_struct和以一行表示一个样本的testdata,并以1和-1列向量的形式返回分类结果。
Add SVDD to svm module #
边栏推荐
- 首届“量子计算+金融科技应用”研讨会在京成功举办
- 杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
- Contribution of Writing Series
- Kubernetes source code analysis (II) -- resource
- 左程云 递归+动态规划
- Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform
- SQL Tuning Advisor一个错误ORA-00600: internal error code, arguments: [kesqsMakeBindValue:obj]
- sshd[12282]: fatal: matching cipher is not supported: [email protected] [preauth]
- 【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
- LeetCode 77:组合
猜你喜欢
【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
「小样本深度学习图像识别」最新2022综述
Oauth2协议中如何对accessToken进行校验
MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
Don't you know the relationship between JSP and servlet?
美国空军研究实验室《探索深度学习系统的脆弱性和稳健性》2022年最新85页技术报告
How to verify accesstoken in oauth2 protocol
Left path cloud recursion + dynamic planning
ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法
Redis入门完整教程:问题定位与优化
随机推荐
The solution of unable to create servlet file after idea restart
MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
杰理之RTC 时钟开发【篇】
leetcode-02(链表题)
sshd[12282]: fatal: matching cipher is not supported: [email protected] [preauth]
换个姿势做运维!GOPS 2022 · 深圳站精彩内容抢先看!
Software testing -- common assertions of JMeter interface testing
Use of tensorboard
uniapp适配问题
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
Convert widerperson dataset to Yolo format
Form validation of uniapp
tensorboard的使用
上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
Examples of how to use dates in Oracle
PSINS中19维组合导航模块sinsgps详解(滤波部分)
Redis getting started complete tutorial: replication configuration
Kysl Haikang camera 8247 H9 ISAPI test