我知道有一些库允许使用python代码中的支持向量机,但我专门寻找允许在线教学的库(这不需要一次性提供所有数据)。
有吗?
发布于 2009-11-24 00:09:45
LibSVM包括一个python包装器,它通过SWIG工作。
示例svm-test.py来自他们的发行版:
#!/usr/bin/env python
from svm import *
# a three-class problem
labels = [0, 1, 1, 2]
samples = [[0, 0], [0, 1], [1, 0], [1, 1]]
problem = svm_problem(labels, samples);
size = len(samples)
kernels = [LINEAR, POLY, RBF]
kname = ['linear','polynomial','rbf']
param = svm_parameter(C = 10,nr_weight = 2,weight_label = [1,0],weight = [10,1])
for k in kernels:
param.kernel_type = k;
model = svm_model(problem,param)
errors = 0
for i in range(size):
prediction = model.predict(samples[i])
probability = model.predict_probability
if (labels[i] != prediction):
errors = errors + 1
print "##########################################"
print " kernel %s: error rate = %d / %d" % (kname[param.kernel_type], errors, size)
print "##########################################"
param = svm_parameter(kernel_type = RBF, C=10)
model = svm_model(problem, param)
print "##########################################"
print " Decision values of predicting %s" % (samples[0])
print "##########################################"
print "Numer of Classes:", model.get_nr_class()
d = model.predict_values(samples[0])
for i in model.get_labels():
for j in model.get_labels():
if j>i:
print "{%d, %d} = %9.5f" % (i, j, d[i,j])
param = svm_parameter(kernel_type = RBF, C=10, probability = 1)
model = svm_model(problem, param)
pred_label, pred_probability = model.predict_probability(samples[1])
print "##########################################"
print " Probability estimate of predicting %s" % (samples[1])
print "##########################################"
print "predicted class: %d" % (pred_label)
for i in model.get_labels():
print "prob(label=%d) = %f" % (i, pred_probability[i])
print "##########################################"
print " Precomputed kernels"
print "##########################################"
samples = [[1, 0, 0, 0, 0], [2, 0, 1, 0, 1], [3, 0, 0, 1, 1], [4, 0, 1, 1, 2]]
problem = svm_problem(labels, samples);
param = svm_parameter(kernel_type=PRECOMPUTED,C = 10,nr_weight = 2,weight_label = [1,0],weight = [10,1])
model = svm_model(problem, param)
pred_label = model.predict(samples[0]) 发布于 2009-11-24 00:17:21
还没听说过呢。但是你真的需要在线学习吗?我使用支持向量机已经有很长一段时间了,从来没有遇到过必须使用在线学习的问题。通常,我会对训练样本的改变次数设置一个阈值(可能是100或1000),然后批量重新训练所有样本。
如果你的问题是大规模的,你必须使用在线学习,那么你可能想看看vowpal wabbit。
在评论之后,重新编辑如下:
Olivier Grisel建议在LaSVM周围使用ctype包装器。因为我以前不知道LaSVM,而且它看起来很酷,所以我很想在我自己的问题上尝试一下:)。
如果你被限制只能使用Python-VM (嵌入式设备,机器人),我建议使用投票/平均感知器,它的性能接近SVM,但很容易实现,默认情况下是“在线”的。
我刚看到Elefant有一些在线支持向量机的代码。
发布于 2011-01-06 21:57:48
虽然那里没有python绑定,但在http://leon.bottou.org/projects/sgd描述的算法是以在线方式训练的,并且可以很容易地使用numpy重新实现。
https://stackoverflow.com/questions/1783669
复制相似问题