當前位置: 首頁>>代碼示例>>Python>>正文


Python LibSVM.train方法代碼示例

本文整理匯總了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
開發者ID:BrainTech,項目名稱:openbci,代碼行數:30,代碼來源:svm.py

示例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)
開發者ID:veniciusgrjr,項目名稱:oqtans_tools,代碼行數:59,代碼來源:kirmes.py

示例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
開發者ID:42MachineLearning,項目名稱:shogun,代碼行數:16,代碼來源:svm_params.py

示例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)
開發者ID:Argram,項目名稱:shogun,代碼行數:18,代碼來源:classifier_libsvm_minimal_modular.py

示例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
開發者ID:bdholt1,項目名稱:ml-benchmarks,代碼行數:19,代碼來源:bench_svm.py

示例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
開發者ID:behollis,項目名稱:muViewBranch,代碼行數:46,代碼來源:kernel_combined_custom_poly_modular.py

示例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()
開發者ID:harshitsyal,項目名稱:shogun,代碼行數:23,代碼來源:classifier_libsvm_modular.py

示例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)
開發者ID:boya888,項目名稱:oqtans_tools,代碼行數:23,代碼來源:EasySVM.py

示例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
開發者ID:AlexBinder,項目名稱:shogun,代碼行數:24,代碼來源:classifier_custom_kernel_modular.py

示例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()
開發者ID:memimo,項目名稱:shogun-liblinear,代碼行數:26,代碼來源:classifier_libsvm_modular.py

示例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()
開發者ID:alesis,項目名稱:shogun,代碼行數:33,代碼來源:prc.py

示例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
開發者ID:cwidmer,項目名稱:multitask,代碼行數:33,代碼來源:debug_shogun_dasvm.py

示例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))
開發者ID:cwidmer,項目名稱:multitask,代碼行數:33,代碼來源:debug_shogun_dasvm_linear.py

示例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
開發者ID:ishichan78,項目名稱:NLP,代碼行數:7,代碼來源:cross_validation.py


注:本文中的shogun.Classifier.LibSVM.train方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。