Skip to content

Instantly share code, notes, and snippets.

@Winterflower
Created July 17, 2014 11:05
Show Gist options
  • Save Winterflower/7353e44b418555a52c0f to your computer and use it in GitHub Desktop.
Save Winterflower/7353e44b418555a52c0f to your computer and use it in GitHub Desktop.
try:
from shogun.PreProc import SortWordString, SortUlongString
except ImportError:
from shogun.Preprocessor import SortWordString, SortUlongString
from shogun.Kernel import CommWordStringKernel, CommUlongStringKernel, \
CombinedKernel
from shogun.Features import StringWordFeatures, StringUlongFeatures, \
StringCharFeatures, CombinedFeatures, DNA, Labels
from shogun.Classifier import MSG_INFO, MSG_ERROR
try:
from shogun.Classifier import SVMLight
except ImportError:
from shogun.Classifier import LibSVM
train_dna=['ACGT',
'TGTG',
'GCGC',
'GGGG']
train_labels=[1.0,-1.0,1.0,-1.0]
test_dna=['GGGG',
'GTGT',
'AACT',
'GGTG']
char_feats_train=StringCharFeatures(train_dna,DNA)
char_feats_test=StringCharFeatures(test_dna,DNA)
feats_train=StringWordFeatures(DNA)
feats_test=StringWordFeatures(DNA)
feats_train.obtain_from_char(char_feats_train,1,2,0,False)
feats_test.obtain_from_char(char_feats_test,1,2,0,False)
#preprocessing for kernel
preproc = SortWordString()
preproc.init(feats_train)
preproc.init(feats_test)
feats_train.add_preproc(preproc)
feats_train.apply_preproc()
feats_test.add_preproc(preproc)
feats_test.apply_preproc()
#KERNEL
spectrum_kernel=CommWordStringKernel(feats_train,feats_train)
#svm initialization
svm=SVMLight(options.svmC, spectrum_kernel, Labels(numpy.array(labels, dtype=numpy.double)))
#use get_labels to obtain scores for test sequences
spectrum_kernel.init(feats_train,feats_test)
svm.apply().get_labels()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment