使用Python的字符串子序列核和支持向量机

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注