将 Python 绑定用于 SVM 库 LIBSVM 的示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4214868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An example using python bindings for SVM library, LIBSVM
提问by Hossein
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
我迫切需要一个在 python 中使用 LibSVM 的分类任务示例。我不知道 Input 应该是什么样子,哪个函数负责训练,哪个函数用于测试 谢谢
采纳答案by mossplix
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.
LIBSVM 从包含两个列表的元组中读取数据。第一个列表包含类,第二个列表包含输入数据。创建具有两个可能的类的简单数据集,您还需要通过创建 svm_parameter 来指定要使用的内核。
>> from libsvm import *
>> prob = svm_problem([1,-1],[[1,0,1],[-1,0,-1]])
>> param = svm_parameter(kernel_type = LINEAR, C = 10)
## training the model
>> m = svm_model(prob, param)
#testing the model
>> m.predict([1, 1, 1])
回答by doug
This example demonstrates a one-class SVM classifier; it's about as simple as possible while still showing the complete LIBSVM workflow.
这个例子演示了一个单类SVM 分类器;它尽可能简单,同时仍显示完整的 LIBSVM 工作流程。
Step 1: Import NumPy & LIBSVM
第 1 步:导入 NumPy 和 LIBSVM
import numpy as NP
from svm import *
Step 2:Generate synthetic data: for this example, 500 points within a given boundary (note: quite a few realdata sets are are provided on the LIBSVM website)
步骤2:生成合成数据:对于这个例子,给定边界内的500个点(注意:LIBSVM网站上提供了相当多的真实数据集)
Data = NP.random.randint(-5, 5, 1000).reshape(500, 2)
Step 3:Now, choose some non-linear decision boundary for a one-classclassifier:
第 3 步:现在,为一类分类器选择一些非线性决策边界:
rx = [ (x**2 + y**2) < 9 and 1 or 0 for (x, y) in Data ]
Step 4:Next, arbitrarily partition the data w/r/t this decision boundary:
第 4 步:接下来,根据此决策边界对数据进行任意分区:
Class I: those that lie onor withinan arbitrary circle
Class II: all points outsidethe decision boundary (circle)
I类:位于任意圆上或圆内的那些
第二类:决策边界外的所有点(圆)
The SVM Model Building begins here; all steps before this one were just to prepare some synthetic data.
SVM 模型构建从这里开始;在这之前的所有步骤只是准备一些合成数据。
Step 5: Construct the problem descriptionby calling svm_problem, passing in the decision boundary functionand the data, then bind this result to a variable.
步骤5:通过调用svm_problem构造问题描述,传入决策边界函数和数据,然后将此结果绑定到一个变量。
px = svm_problem(rx, Data)
Step 6:Select a kernel functionfor the non-linear mapping
步骤 6:为非线性映射选择核函数
For this exmaple, i chose RBF(radial basis function) as my kernel function
对于这个例子,我选择了RBF(径向基函数)作为我的核函数
pm = svm_parameter(kernel_type=RBF)
Step 7:Train the classifier, by calling svm_model, passing in the problem description(px) & kernel(pm)
步骤 7:通过调用svm_model训练分类器,传入问题描述(px)和内核(pm)
v = svm_model(px, pm)
Step 8:Finally, test the trained classifier by calling predicton the trained model object ('v')
步骤 8:最后,通过对训练好的模型对象 ('v')调用predict来测试训练好的分类器
v.predict([3, 1])
# returns the class label (either '1' or '0')
For the example above, I used version 3.0of LIBSVM(the current stable release at the time this answerwas posted).
对于上面的示例,我使用了LIBSVM 的3.0版(发布此答案时的当前稳定版本)。
Finally, w/r/t the part of your question regarding the choice of kernel function, Support Vector Machines are notspecific to a particular kernel function--e.g., i could have chosen a different kernel (gaussian, polynomial, etc.).
最后,w/r/t 关于内核函数选择的问题部分,支持向量机并不特定于特定的内核函数——例如,我可以选择不同的内核(高斯、多项式等)。
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_parameterand passing in a value for kernel_type(a three-letter abbreviation for the chosen kernel).
LIBSVM包括所有最常用的内核函数-这是一个很大的帮助,因为你可以看到所有可行的替代品,并选择一个为模型中使用,只需要调用的事svm_parameter和值传递的kernel_type(一所选内核的三个字母缩写)。
Finally, the kernel function you choose for training must match the kernel function used against the testing data.
最后,您选择用于训练的核函数必须与针对测试数据使用的核函数相匹配。
回答by agramfort
You might consider using
你可以考虑使用
http://scikit-learn.sourceforge.net/
http://scikit-learn.sourceforge.net/
That has a great python binding of libsvm and should be easy to install
它有一个很好的 libsvm 的 python 绑定,应该很容易安装
回答by ShinNoNtheitroad
The code examples listed here don't work with LibSVM 3.1, so I've more or less ported the example by mossplix:
此处列出的代码示例不适用于 LibSVM 3.1,因此我或多或少地通过 mossplix移植了该示例:
from svmutil import *
svm_model.predict = lambda self, x: svm_predict([0], [x], self)[0][0]
prob = svm_problem([1,-1], [[1,0,1], [-1,0,-1]])
param = svm_parameter()
param.kernel_type = LINEAR
param.C = 10
m=svm_train(prob, param)
m.predict([1,1,1])
回答by Nihar Sarangi
Adding to @shinNoNtheitroad :
添加到@shinNoNtheitroad :
param.kernel_type represents the type of kernel function you want to use, 0: Linear 1: polynomial 2: RBF 3: Sigmoid
param.kernel_type 代表你要使用的核函数的类型,0:线性 1:多项式 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)
还要记住, svm_problem(y,x) :这里 y 是类标签,x 是类实例,x 和 y 只能是列表、元组和字典。(没有 numpy 数组)
回答by Santosh Chanda
param = svm_parameter('-s 0 -t 2 -d 3 -c '+str(C)+' -g '+str(G)+' -p '+str(self.epsilon)+' -n '+str(self.nu))
I don't know about the earlier versions but in LibSVM 3.xxthe method svm_parameter('options')will takes just one argument.
我不知道早期版本,但在LibSVM 3.xx 中,该方法svm_parameter('options')只需要一个参数。
In my case C, G, pand nuare the dynamic values. You make changes according to your code.
就我而言C,G、p和nu是动态值。您可以根据自己的代码进行更改。
options:
选项:
-s svm_type : set type of SVM (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 : 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_set_file)
-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)
Source of documentation: https://www.csie.ntu.edu.tw/~cjlin/libsvm/
回答by Thamme Gowda
SVM via SciKit-learn:
通过 SciKit-learn 的 SVM:
from sklearn.svm import SVC
X = [[0, 0], [1, 1]]
y = [0, 1]
model = SVC().fit(X, y)
tests = [[0.,0.], [0.49,0.49], [0.5,0.5], [2., 2.]]
print(model.predict(tests))
# prints [0 0 1 1]
For more details here: http://scikit-learn.org/stable/modules/svm.html#svm
有关更多详细信息,请访问:http: //scikit-learn.org/stable/modules/svm.html#svm
回答by Amen
Here is a dummy example I mashed up:
这是我混搭的一个虚拟示例:
import numpy
import matplotlib.pyplot as plt
from random import seed
from random import randrange
import svmutil as svm
seed(1)
# Creating Data (Dense)
train = list([randrange(-10, 11), randrange(-10, 11)] for i in range(10))
labels = [-1, -1, -1, 1, 1, -1, 1, 1, 1, 1]
options = '-t 0' # linear model
# Training Model
model = svm.svm_train(labels, train, options)
# Line Parameters
w = numpy.matmul(numpy.array(train)[numpy.array(model.get_sv_indices()) - 1].T, model.get_sv_coef())
b = -model.rho.contents.value
if model.get_labels()[1] == -1: # No idea here but it should be done :|
w = -w
b = -b
print(w)
print(b)
# Plotting
plt.figure(figsize=(6, 6))
for i in model.get_sv_indices():
plt.scatter(train[i - 1][0], train[i - 1][1], color='red', s=80)
train = numpy.array(train).T
plt.scatter(train[0], train[1], c=labels)
plt.plot([-5, 5], [-(-5 * w[0] + b) / w[1], -(5 * w[0] + b) / w[1]])
plt.xlim([-13, 13])
plt.ylim([-13, 13])
plt.show()


