String Subsequence Kernel and SVM using Python

2019-03-30 13:09发布

How can I use Subsequence String Kernel (SSK) [Lodhi 2002] to train a SVM (Support Vector Machine) in Python?

4条回答
beautiful°
2楼-- · 2019-03-30 13:38

Recently, the String Subsequence Kernel (SSK) [Lodhi. et. al., 2002] has been added to Shogun Machine Learning toolbox and is made available for using in all modular interfaces including Python. You can find a working example of using this kernel for a DNA classification problem here using LibSVM.

查看更多
干净又极端
3楼-- · 2019-03-30 13:43

For future reference, the name of the Kernel in the current version of Shogun (3.2.0) is StringSubsequenceKernel.

Source: https://code.google.com/p/shogun-toolbox/source/browse/src/shogun/kernel/string/StringSubsequenceKernel.h

查看更多
疯言疯语
4楼-- · 2019-03-30 13:47

This is an update to gcedo's answer to work with the current version of shogun (Shogun 6.1.3).

Working example:

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)
查看更多
来,给爷笑一个
5楼-- · 2019-03-30 13:52

I have come to a solution using the Shogun Library. You have to install it from the commit 0891f5a38bcb as later revisions would mistakenly remove the needed classes.

This is a working example:

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
查看更多
登录 后发表回答