如何使用字符串子序列核(SSK)[Lodhi 2002]在Python中训练支持向量机(SVM)?
回答:
这是对gcedo的回答的更新,以适应当前版本的shogun(Shogun 6.1.3)。
工作示例:
import numpy as npfrom shogun import StringCharFeatures, RAWBYTEfrom shogun import BinaryLabelsfrom shogun import SubsequenceStringKernelfrom shogun import LibSVMstrings = ['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是n,0.5是lambda,如Lodhi 2002中所述sk = SubsequenceStringKernel(features, features, 3, 0.5)# 训练支持向量机labels = BinaryLabels(train_labels)C = 1.0svm = LibSVM(C, sk, labels)svm.train()# 预测predicted_labels = svm.apply(test_features).get_labels()print(predicted_labels)