使用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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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