使用 Python 绑定 SVM 库 LIBSVM 的示例
我迫切需要一个在 python 中使用 LibSVM 的分类任务示例。我不知道输入应该是什么样子,也不知道哪个函数负责训练,哪个函数负责测试 谢谢
I am in dire need of a classification task example using LibSVM in python. I don't know how the Input should look like and which function is responsible for training and which one for testing
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
此处列出的代码示例不适用于 LibSVM 3.1,因此我或多或少移植了 mossplix 的示例:
The code examples listed here don't work with LibSVM 3.1, so I've more or less ported the example by mossplix:
此示例演示了单类SVM 分类器;它尽可能简单,同时仍然显示完整的 LIBSVM 工作流程。
第 1 步:导入 NumPy 和LIBSVM
第 2 步:生成合成数据:在本例中,给定边界内的 500 个点(注意:相当多的真实数据集是LIBSVM 网站上提供)
步骤3:现在,为一类分类器选择一些非线性决策边界:
第 4 步:接下来,根据此决策边界任意划分数据:
I 类:位于上或的数据在任意圆
II类:决策边界(圆圈)外部的所有点
SVM 模型构建从这里开始;在此之前的所有步骤都只是准备一些综合数据。
第 5 步:通过调用 构建问题描述 svm_problem,传入决策边界函数和数据,然后将此结果绑定到变量。
第 6 步:为非线性映射选择核函数
对于此示例,我选择了 RBF(径向基函数)作为我的核函数
第 7 步:训练分类器,
通过调用svm_model,传入问题描述 (px) & 内核(下午)
第 8 步:最后,通过在训练的模型对象上调用预测来测试训练的分类器( 'v')
对于上面的示例,我使用了LIBSVM版本3.0(当时的当前稳定版本这个答案< /em> 已发布)。
最后,关于您问题中有关内核函数选择的部分,支持向量机不特定于特定内核函数——例如,我可以选择不同的内核(高斯、多项式等)。
LIBSVM 包含所有最常用的内核函数——这是一个很大的帮助,因为您可以看到所有可能的替代方案,并选择一个用于您的模型,只需调用 svm_parameter 并传递kernel_type 的值(所选内核的三个字母缩写)。
最后,您选择用于训练的核函数必须与针对测试数据使用的核函数相匹配。
This example demonstrates a one-class SVM classifier; it's about as simple as possible while still showing the complete LIBSVM workflow.
Step 1: Import NumPy & LIBSVM
Step 2: Generate synthetic data: for this example, 500 points within a given boundary (note: quite a few real data sets are are provided on the LIBSVM website)
Step 3: Now, choose some non-linear decision boundary for a one-class classifier:
Step 4: Next, arbitrarily partition the data w/r/t this decision boundary:
Class I: those that lie on or within an arbitrary circle
Class II: all points outside the decision boundary (circle)
The SVM Model Building begins here; all steps before this one were just to prepare some synthetic data.
Step 5: Construct the problem description by calling svm_problem, passing in the decision boundary function and the data, then bind this result to a variable.
Step 6: Select a kernel function for the non-linear mapping
For this exmaple, i chose RBF (radial basis function) as my kernel function
Step 7: Train the classifier,
by calling svm_model, passing in the problem description (px) & kernel (pm)
Step 8: Finally, test the trained classifier by calling predict on the trained model object ('v')
For the example above, I used version 3.0 of LIBSVM (the current stable release at the time this answer was posted).
Finally, w/r/t the part of your question regarding the choice of kernel function, Support Vector Machines are not specific to a particular kernel function--e.g., i could have chosen a different kernel (gaussian, polynomial, etc.).
LIBSVM includes all of the most commonly used kernel functions--which is a big help because you can see all plausible alternatives and to select one for use in your model, is just a matter of calling svm_parameter and passing in a value for kernel_type (a three-letter abbreviation for the chosen kernel).
Finally, the kernel function you choose for training must match the kernel function used against the testing data.
LIBSVM 从包含两个列表的元组中读取数据。第一个列表包含类,第二个列表包含输入数据。创建具有两个可能的类的简单数据集
您还需要通过创建 svm_parameter 来指定要使用的内核。
LIBSVM reads the data from a tuple containing two lists. The first list contains the classes and the second list contains the input data. create simple dataset with two possible classes
you also need to specify which kernel you want to use by creating svm_parameter.
您可能会考虑使用
http://scikit-learn.sourceforge.net/
它有一个很棒的 python 绑定libsvm 的并且应该很容易安装
You might consider using
http://scikit-learn.sourceforge.net/
That has a great python binding of libsvm and should be easy to install
添加到 @shinNoNoir :
param.kernel_type 表示您要使用的内核函数的类型,
0:线性
1:多项式
2:径向基函数
3:Sigmoid
另请记住,svm_problem(y,x):这里 y 是类标签,x 是类实例,x 和 y 只能是列表、元组和字典。(没有 numpy 数组)
Adding to @shinNoNoir :
param.kernel_type represents the type of kernel function you want to use,
0: Linear
1: polynomial
2: RBF
3: Sigmoid
Also have in mind that, svm_problem(y,x) : here y is the class labels and x is the class instances and x and y can only be lists,tuples and dictionaries.(no numpy array)
通过 SciKit-learn 的 SVM:
有关更多详细信息,请访问:http://scikit-learn。 org/stable/modules/svm.html#svm
SVM via SciKit-learn:
For more details here: http://scikit-learn.org/stable/modules/svm.html#svm
这是我混搭的一个虚拟示例:
Here is a dummy example I mashed up:
我不知道早期版本,但在 LibSVM 3.xx 中,方法
svm_parameter('options')
仅接受一个参数。在我的例子中,
C
、G
、p
和nu
是动态值。您根据您的代码进行更改。选项:
文档来源: https://www.csie.ntu.edu.tw /~cjlin/libsvm/
I don't know about the earlier versions but in LibSVM 3.xx the method
svm_parameter('options')
will takes just one argument.In my case
C
,G
,p
andnu
are the dynamic values. You make changes according to your code.options:
Source of documentation: https://www.csie.ntu.edu.tw/~cjlin/libsvm/