本文整理汇总了Python中shogun.Classifier.LibSVM.train方法的典型用法代码示例。如果您正苦于以下问题:Python LibSVM.train方法的具体用法?Python LibSVM.train怎么用?Python LibSVM.train使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shogun.Classifier.LibSVM
的用法示例。
在下文中一共展示了LibSVM.train方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: libsvm
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def libsvm ():
print 'LibSVM'
from shogun.Features import RealFeatures, Labels
from shogun.Kernel import GaussianKernel
from shogun.Evaluation import PerformanceMeasures
from shogun.Classifier import LibSVM
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
width=2.1
kernel=GaussianKernel(feats_train, feats_train, width)
C=1
epsilon=1e-5
labels=Labels(label_train_twoclass)
svm=LibSVM(C, kernel, labels)
svm.set_epsilon(epsilon)
svm.train()
#kernel.init(feats_train, feats_test)
output = svm.classify(feats_test)#.get_labels()
#output_vector = output.get_labels()
out=svm.classify().get_labels()
testerr=mean(sign(out)!=testlab)
print testerr
示例2: training_run
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def training_run(options):
"""Conduct a training run and return a trained SVM kernel"""
settings = MotifFinderSettings(kirmes_ini.MOTIF_LENGTH, options.window_width, options.replace)
positives = MotifFinder(finder_settings=settings)
positives.setFastaFile(options.positives)
positives.setMotifs(options.pgff)
pmotifs, ppositions = positives.getResults()
negatives = MotifFinder(finder_settings=settings)
negatives.setFastaFile(options.negatives)
negatives.setMotifs(options.ngff)
nmotifs, npositions = negatives.getResults()
wds_kparams = kirmes_ini.WDS_KERNEL_PARAMETERS
wds_svm = EasySVM.EasySVM(wds_kparams)
num_positives = len(pmotifs.values()[0])
num_negatives = len(nmotifs.values()[0])
# Creating Kernel Objects
kernel = CombinedKernel()
features = CombinedFeatures()
kernel_array = []
motifs = pmotifs.keys()
motifs.sort()
# Adding Kmer Kernels
for motif in motifs:
all_examples = pmotifs[motif] + nmotifs[motif]
motif_features = wds_svm.createFeatures(all_examples)
wds_kernel = WeightedDegreePositionStringKernel(motif_features, motif_features, wds_kparams["degree"])
wds_kernel.set_shifts(wds_kparams["shift"] * ones(wds_kparams["seqlength"], dtype=int32))
features.append_feature_obj(motif_features)
kernel_array.append(wds_kernel)
kernel.append_kernel(wds_kernel)
rbf_svm = EasySVM.EasySVM(kirmes_ini.RBF_KERNEL_PARAMETERS)
positions = array(ppositions + npositions, dtype=float64).T
position_features = rbf_svm.createFeatures(positions)
features.append_feature_obj(position_features)
motif_labels = append(ones(num_positives), -ones(num_negatives))
complete_labels = Labels(motif_labels)
rbf_kernel = GaussianKernel(position_features, position_features, kirmes_ini.RBF_KERNEL_PARAMETERS["width"])
kernel_array.append(rbf_kernel)
kernel.append_kernel(rbf_kernel)
# Kernel init
kernel.init(features, features)
kernel.set_cache_size(kirmes_ini.K_CACHE_SIZE)
svm = LibSVM(kirmes_ini.K_COMBINED_C, kernel, complete_labels)
svm.parallel.set_num_threads(kirmes_ini.K_NUM_THREADS)
# Training
svm.train()
if not os.path.exists(options.output_path):
os.mkdir(options.output_path)
html = {}
if options.contrib:
html["contrib"] = contrib(svm, kernel, motif_labels, kernel_array, motifs)
if options.logos:
html["poims"] = poims(svm, kernel, kernel_array, motifs, options.output_path)
if options.query:
html["query"] = evaluate(options, svm, kernel, features, motifs)
htmlize(html, options.output_html)
示例3: svm_train
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def svm_train(kernel, labels, C1, C2=None):
"""Trains a SVM with the given kernel"""
num_threads = 1
kernel.io.disable_progress()
svm = LibSVM(C1, kernel, labels)
if C2:
svm.set_C(C1, C2)
svm.parallel.set_num_threads(num_threads)
svm.io.disable_progress()
svm.train()
return svm
示例4: classifier_libsvm_minimal_modular
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def classifier_libsvm_minimal_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_twoclass=label_traindat,width=2.1,C=1):
from shogun.Features import RealFeatures, BinaryLabels
from shogun.Classifier import LibSVM
from shogun.Kernel import GaussianKernel
feats_train=RealFeatures(fm_train_real);
feats_test=RealFeatures(fm_test_real);
kernel=GaussianKernel(feats_train, feats_train, width);
labels=BinaryLabels(label_train_twoclass);
svm=LibSVM(C, kernel, labels);
svm.train();
kernel.init(feats_train, feats_test);
out=svm.apply().get_labels();
testerr=mean(sign(out)!=label_train_twoclass)
示例5: bench_shogun
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def bench_shogun(X, y, T, valid):
#
# .. Shogun ..
#
from shogun.Classifier import LibSVM
from shogun.Features import RealFeatures, Labels
from shogun.Kernel import GaussianKernel
start = datetime.now()
feat = RealFeatures(X.T)
feat_test = RealFeatures(T.T)
labels = Labels(y.astype(np.float64))
kernel = GaussianKernel(feat, feat, sigma)
shogun_svm = LibSVM(1., kernel, labels)
shogun_svm.train()
dec_func = shogun_svm.classify(feat_test).get_labels()
score = np.mean(np.sign(dec_func) == valid)
return score, datetime.now() - start
示例6: kernel_combined_custom_poly_modular
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def kernel_combined_custom_poly_modular(fm_train_real = traindat,fm_test_real = testdat,fm_label_twoclass=label_traindat):
from shogun.Features import CombinedFeatures, RealFeatures, BinaryLabels
from shogun.Kernel import CombinedKernel, PolyKernel, CustomKernel
from shogun.Classifier import LibSVM
kernel = CombinedKernel()
feats_train = CombinedFeatures()
tfeats = RealFeatures(fm_train_real)
tkernel = PolyKernel(10,3)
tkernel.init(tfeats, tfeats)
K = tkernel.get_kernel_matrix()
kernel.append_kernel(CustomKernel(K))
subkfeats_train = RealFeatures(fm_train_real)
feats_train.append_feature_obj(subkfeats_train)
subkernel = PolyKernel(10,2)
kernel.append_kernel(subkernel)
kernel.init(feats_train, feats_train)
labels = BinaryLabels(fm_label_twoclass)
svm = LibSVM(1.0, kernel, labels)
svm.train()
kernel = CombinedKernel()
feats_pred = CombinedFeatures()
pfeats = RealFeatures(fm_test_real)
tkernel = PolyKernel(10,3)
tkernel.init(tfeats, pfeats)
K = tkernel.get_kernel_matrix()
kernel.append_kernel(CustomKernel(K))
subkfeats_test = RealFeatures(fm_test_real)
feats_pred.append_feature_obj(subkfeats_test)
subkernel = PolyKernel(10, 2)
kernel.append_kernel(subkernel)
kernel.init(feats_train, feats_pred)
svm.set_kernel(kernel)
svm.apply()
km_train=kernel.get_kernel_matrix()
return km_train,kernel
示例7: classifier_libsvm_modular
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def classifier_libsvm_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_twoclass=label_traindat,width=2.1,C=1,epsilon=1e-5):
from shogun.Features import RealFeatures, Labels
from shogun.Kernel import GaussianKernel
from shogun.Classifier import LibSVM
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
kernel=GaussianKernel(feats_train, feats_train, width)
labels=Labels(label_train_twoclass)
svm=LibSVM(C, kernel, labels)
svm.set_epsilon(epsilon)
svm.train()
kernel.init(feats_train, feats_test)
labels = svm.apply().get_labels()
supportvectors = sv_idx=svm.get_support_vectors()
alphas=svm.get_alphas()
predictions = svm.apply()
return predictions, svm, predictions.get_labels()
示例8: train
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def train(self, trainexamples, trainlabels):
"""Trains a SVM with the given kernel"""
kernel_cache_size = 500
num_threads = 6
feats_train = self.createFeatures(trainexamples)
if self.kparam['name'] == 'wd':
self.kparam['seqlength'] = len(trainexamples[0])
self.createKernel(feats_train)
self.kernel.io.disable_progress()
self.kernel.set_cache_size(int(kernel_cache_size))
labels = Labels(numpy.array(trainlabels, numpy.double))
svm = LibSVM(self.getC(), self.kernel, labels)
svm.parallel.set_num_threads(num_threads)
svm.io.disable_progress()
svm.train()
return (svm, feats_train)
示例9: classifier_custom_kernel_modular
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def classifier_custom_kernel_modular (C=1,dim=7):
from shogun.Features import RealFeatures, BinaryLabels
from shogun.Kernel import CustomKernel
from shogun.Classifier import LibSVM
from numpy import diag,ones,sign
from numpy.random import rand,seed
seed((C,dim))
lab=sign(2*rand(dim) - 1)
data=rand(dim, dim)
symdata=data*data.T + diag(ones(dim))
kernel=CustomKernel()
kernel.set_full_kernel_matrix_from_full(data)
labels=BinaryLabels(lab)
svm=LibSVM(C, kernel, labels)
svm.train()
predictions =svm.apply()
out=svm.apply().get_labels()
return svm,out
示例10: libsvm
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def libsvm ():
print 'LibSVM'
from shogun.Features import RealFeatures, Labels
from shogun.Kernel import GaussianKernel
from shogun.Classifier import LibSVM
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
width=2.1
kernel=GaussianKernel(feats_train, feats_train, width)
C=1
epsilon=1e-5
labels=Labels(label_train_twoclass)
svm=LibSVM(C, kernel, labels)
svm.set_epsilon(epsilon)
svm.train()
kernel.init(feats_train, feats_test)
svm.classify().get_labels()
sv_idx=svm.get_support_vectors()
alphas=svm.get_alphas()
示例11: subplots_adjust
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
from shogun.Evaluation import PRCEvaluation
import util
util.set_title('PRC example')
util.DISTANCE=0.5
subplots_adjust(hspace=0.3)
pos=util.get_realdata(True)
neg=util.get_realdata(False)
features=util.get_realfeatures(pos, neg)
labels=util.get_labels()
# classifiers
gk=GaussianKernel(features, features, 1.0)
svm = LibSVM(1000.0, gk, labels)
svm.train()
lda=LDA(1,features,labels)
lda.train()
## plot points
subplot(211)
plot(pos[0,:], pos[1,:], "r.")
plot(neg[0,:], neg[1,:], "b.")
grid(True)
title('Data',size=10)
# plot PRC for SVM
subplot(223)
PRC_evaluation=PRCEvaluation()
PRC_evaluation.evaluate(svm.classify(),labels)
PRC = PRC_evaluation.get_PRC()
示例12: StringCharFeatures
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
labels_presvm[15] = 1
labels_presvm[8] = 1
labels_presvm[19] = 1
feat_presvm = StringCharFeatures(DNA)
feat_presvm.set_features(examples_presvm)
wdk_presvm = WeightedDegreeStringKernel(feat_presvm, feat_presvm, 1)
lab_presvm = Labels(numpy.array(labels_presvm))
presvm = SVMLight(1, wdk_presvm, lab_presvm)
presvm.train()
presvm2 = LibSVM(1, wdk_presvm, lab_presvm)
presvm2.train()
print "svmlight", presvm.get_objective()
print "libsvm", presvm2.get_objective()
assert(abs(presvm.get_objective() - presvm2.get_objective())<= 0.001)
print "simple svm", presvm.get_objective()
print "len(examples_presvm)", len(examples_presvm)
print "##############"
#############################################
# compute linear term manually
示例13: LinearKernel
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
wdk_presvm = LinearKernel(feat_presvm, feat_presvm)
presvm_liblinear = LibLinear(1, feat_presvm, lab_presvm)
presvm_liblinear.set_max_iterations(10000)
presvm_liblinear.set_bias_enabled(False)
presvm_liblinear.train()
presvm_libsvm = LibSVM(1, wdk_presvm, lab_presvm)
#presvm_libsvm = SVMLight(1, wdk_presvm, lab_presvm)
#presvm_libsvm.io.set_loglevel(MSG_DEBUG)
presvm_libsvm.set_bias_enabled(False)
presvm_libsvm.train()
my_w = presvm_liblinear.get_w()
presvm_liblinear = LibLinear(1, feat_presvm, lab_presvm)
presvm_liblinear.set_w(my_w)
#############################################
# compute linear term manually
#############################################
examples = numpy.array(examples, dtype=numpy.float64)
examples = numpy.transpose(examples)
feat = RealFeatures(examples)
lab = Labels(numpy.array(labels))
示例14: svm_train
# 需要导入模块: from shogun.Classifier import LibSVM [as 别名]
# 或者: from shogun.Classifier.LibSVM import train [as 别名]
def svm_train(kernel, lab, C=1):
labels = BinaryLabels(lab)
svm = LibSVM(C,kernel,labels)
svm.train()
return svm