如何使用子序列字符串内核 (SSK) [Lodhi 2002] 在 Python 中训练 SVM(支持向量机)?
问问题
4446 次
4 回答
5
我已经找到了使用 Shogun Library 的解决方案。您必须从提交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
于 2014-02-10T10:55:07.610 回答
3
最近,字符串子序列内核 (SSK) [Lodhi. 等。al., 2002] 已添加到Shogun 机器学习工具箱中,并可用于包括 Python 在内的所有模块化接口。您可以在此处使用 LibSVM找到使用此内核解决 DNA 分类问题的工作示例。
于 2014-02-12T14:59:57.270 回答
2
这是对gcedo使用当前版本的 shogun (Shogun 6.1.3) 的回答的更新。
工作示例:
import numpy as np
from shogun import StringCharFeatures, RAWBYTE
from shogun import BinaryLabels
from shogun import SubsequenceStringKernel
from shogun import LibSVM
strings = ['cat', 'doom', 'car', 'boom','caboom','cartoon','cart']
test = ['bat', 'soon', 'it is your doom', 'i love your cat cart','i love loonytoons']
train_labels = np.array([1, -1, 1, -1,-1,-1,1])
test_labels = np.array([1, -1, -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 = SubsequenceStringKernel(features, features, 3, 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)
于 2018-05-11T00:06:37.497 回答
1
为了将来参考,当前版本的 Shogun (3.2.0) 中的内核名称是StringSubsequenceKernel。
于 2014-05-08T17:20:24.497 回答