如何使用子序列字符串内核 (SSK) [Lodhi 2002] 在 Python 中训练 SVM(支持向量机)?
最佳答案
我找到了使用 Shogun 库的解决方案。您必须从提交 0891f5a38bcb 安装它因为以后的修订会错误地删除所需的类。
这是一个工作示例:
from shogun.Features import *
from shogun.Kernel import *
from shogun.Classifier import *
from shogun.Evaluation import *
from modshogun import StringCharFeatures, RAWBYTE
from shogun.Kernel import SSKStringKernel
strings = ['cat', 'doom', 'car', 'boom']
test = ['bat', 'soon']
train_labels = numpy.array([1, -1, 1, -1])
test_labels = numpy.array([1, -1])
features = StringCharFeatures(strings, RAWBYTE)
test_features = StringCharFeatures(test, RAWBYTE)
# 1 is n and 0.5 is lambda as described in Lodhi 2002
sk = SSKStringKernel(features, features, 1, 0.5)
# Train the Support Vector Machine
labels = BinaryLabels(train_labels)
C = 1.0
svm = LibSVM(C, sk, labels)
svm.train()
# Prediction
predicted_labels = svm.apply(test_features).get_labels()
print predicted_labels
关于python - 使用 Python 的字符串子序列内核和 SVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21675240/