python - 使用 Python 的字符串子序列内核和 SVM

标签 python machine-learning svm supervised-learning

如何使用子序列字符串内核 (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/

相关文章:

python - 使用 Python 分析音频文件

python - 我们如何使用 Decision_function(X) One-Class-SVM 计算异常分数

machine-learning - 使用 libsvm 进行一类分类

python - LabelBinarizer 在多类示例中产生不同的结果

matlab - SVM 中的 Gram 矩阵/核不是半正定的

python - 测试准确率较低但 AUC 分数较高的可能原因

python - 如何使用 Beautifulsoup 在某些文本中嵌入 p 标签?

python - 使用Python填充缺失数据

python函数在n行中分解一条消息

machine-learning - 两个 Keras 层之间的自定义(卷积)连接