def classifier_multiclassmachine_modular(fm_train_real=traindat,
                                         fm_test_real=testdat,
                                         label_train_multiclass=label_traindat,
                                         width=2.1,
                                         C=1,
                                         epsilon=1e-5):
    from modshogun import RealFeatures, MulticlassLabels
    from modshogun import GaussianKernel
    from modshogun import LibSVM, KernelMulticlassMachine, MulticlassOneVsRestStrategy

    feats_train = RealFeatures(fm_train_real)
    feats_test = RealFeatures(fm_test_real)
    kernel = GaussianKernel(feats_train, feats_train, width)

    labels = MulticlassLabels(label_train_multiclass)

    classifier = LibSVM()
    classifier.set_epsilon(epsilon)
    #print labels.get_labels()
    mc_classifier = KernelMulticlassMachine(MulticlassOneVsRestStrategy(),
                                            kernel, classifier, labels)
    mc_classifier.train()

    kernel.init(feats_train, feats_test)
    out = mc_classifier.apply().get_labels()
    return out
def runShogunSVMDNAOligoStringKernel(train_xt, train_lt, test_xt):
	"""
	run svm with spectrum kernel
	"""

    ##################################################
    # set up svm
	feats_train = StringCharFeatures(train_xt, DNA)
	feats_test = StringCharFeatures(test_xt, DNA)
	
	kernel=OligoStringKernel(10, K, WIDTH)
	kernel.io.set_loglevel(MSG_DEBUG)
	kernel.init(feats_train, feats_train)

    # init kernel
	labels = BinaryLabels(train_lt)
	
	# run svm model
	print "Ready to train!"
	svm=LibSVM(SVMC, kernel, labels)
	svm.io.set_loglevel(MSG_DEBUG)
	svm.train()

	# predictions
	print "Making predictions!"
	out1DecisionValues = svm.apply(feats_train)
	out1=out1DecisionValues.get_labels()
	kernel.init(feats_train, feats_test)
	out2DecisionValues = svm.apply(feats_test)
	out2=out2DecisionValues.get_labels()

	return out1,out2,out1DecisionValues,out2DecisionValues
def runShogunSVMDNALocalAlignmentKernel(train_xt, train_lt, test_xt):
	"""
	run svm with spectrum kernel
	"""

    ##################################################
    # set up svm
	feats_train = StringCharFeatures(train_xt, DNA)	
	feats_test = StringCharFeatures(test_xt, DNA)
	
	kernel=LocalAlignmentStringKernel(feats_train, feats_train)
	kernel.io.set_loglevel(MSG_DEBUG)

    # init kernel
	labels = BinaryLabels(train_lt)
	
	# run svm model
	print "Ready to train!"
	svm=LibSVM(SVMC, kernel, labels)
	svm.io.set_loglevel(MSG_DEBUG)
	svm.train()

	# predictions
	print "Making predictions!"
	out1=svm.apply(feats_train).get_labels()
	kernel.init(feats_train, feats_test)
	out2=svm.apply(feats_test).get_labels()

	return out1,out2
def classifier_libsvm_minimal_modular (train_fname=traindat,test_fname=testdat,label_fname=label_traindat,width=2.1,C=1):
	from modshogun import RealFeatures, BinaryLabels
	from modshogun import LibSVM, GaussianKernel, CSVFile
	from modshogun import ErrorRateMeasure

	feats_train=RealFeatures(CSVFile(train_fname))
	feats_test=RealFeatures(CSVFile(test_fname))
	labels=BinaryLabels(CSVFile(label_fname))
	kernel=GaussianKernel(feats_train, feats_train, width);

	svm=LibSVM(C, kernel, labels);
	svm.train();

	out=svm.apply(feats_train);
	evaluator = ErrorRateMeasure()
	testerr = evaluator.evaluate(out,labels)
Beispiel #5
0
def modelselection_grid_search_kernel(num_subsets, num_vectors, dim_vectors):
    # init seed for reproducability
    Math.init_random(1)
    random.seed(1)

    # create some (non-sense) data
    matrix = random.rand(dim_vectors, num_vectors)

    # create num_feautres 2-dimensional vectors
    features = RealFeatures()
    features.set_feature_matrix(matrix)

    # create labels, two classes
    labels = BinaryLabels(num_vectors)
    for i in range(num_vectors):
        labels.set_label(i, 1 if i % 2 == 0 else -1)

    # create svm
    classifier = LibSVM()

    # splitting strategy
    splitting_strategy = StratifiedCrossValidationSplitting(
        labels, num_subsets)

    # accuracy evaluation
    evaluation_criterion = ContingencyTableEvaluation(ACCURACY)

    # cross validation class for evaluation in model selection
    cross = CrossValidation(classifier, features, labels, splitting_strategy,
                            evaluation_criterion)
    cross.set_num_runs(1)

    # print all parameter available for modelselection
    # Dont worry if yours is not included, simply write to the mailing list
    #classifier.print_modsel_params()

    # model parameter selection
    param_tree = create_param_tree()
    #param_tree.print_tree()

    grid_search = GridSearchModelSelection(cross, param_tree)

    print_state = False
    best_combination = grid_search.select_model(print_state)
    #print("best parameter(s):")
    #best_combination.print_tree()

    best_combination.apply_to_machine(classifier)

    # larger number of runs to have tighter confidence intervals
    cross.set_num_runs(10)
    cross.set_conf_int_alpha(0.01)
    result = cross.evaluate()
    casted = CrossValidationResult.obtain_from_generic(result)
    #print "result mean:", casted.mean

    return classifier, result, casted.mean
def evaluation_cross_validation_mkl_weight_storage(
        traindat=traindat, label_traindat=label_traindat):
    from modshogun import CrossValidation, CrossValidationResult
    from modshogun import CrossValidationPrintOutput
    from modshogun import CrossValidationMKLStorage
    from modshogun import ContingencyTableEvaluation, ACCURACY
    from modshogun import StratifiedCrossValidationSplitting
    from modshogun import BinaryLabels
    from modshogun import RealFeatures, CombinedFeatures
    from modshogun import GaussianKernel, CombinedKernel
    from modshogun import LibSVM, MKLClassification

    # training data, combined features all on same data
    features = RealFeatures(traindat)
    comb_features = CombinedFeatures()
    comb_features.append_feature_obj(features)
    comb_features.append_feature_obj(features)
    comb_features.append_feature_obj(features)
    labels = BinaryLabels(label_traindat)

    # kernel, different Gaussians combined
    kernel = CombinedKernel()
    kernel.append_kernel(GaussianKernel(10, 0.1))
    kernel.append_kernel(GaussianKernel(10, 1))
    kernel.append_kernel(GaussianKernel(10, 2))

    # create mkl using libsvm, due to a mem-bug, interleaved is not possible
    svm = MKLClassification(LibSVM())
    svm.set_interleaved_optimization_enabled(False)
    svm.set_kernel(kernel)

    # splitting strategy for 5 fold cross-validation (for classification its better
    # to use "StratifiedCrossValidation", but the standard
    # "StratifiedCrossValidationSplitting" is also available
    splitting_strategy = StratifiedCrossValidationSplitting(labels, 5)

    # evaluation method
    evaluation_criterium = ContingencyTableEvaluation(ACCURACY)

    # cross-validation instance
    cross_validation = CrossValidation(svm, comb_features, labels,
                                       splitting_strategy,
                                       evaluation_criterium)
    cross_validation.set_autolock(False)

    # append cross vlaidation output classes
    #cross_validation.add_cross_validation_output(CrossValidationPrintOutput())
    mkl_storage = CrossValidationMKLStorage()
    cross_validation.add_cross_validation_output(mkl_storage)
    cross_validation.set_num_runs(3)

    # perform cross-validation
    result = cross_validation.evaluate()

    # print mkl weights
    weights = mkl_storage.get_mkl_weights()
def classifier_custom_kernel_modular (C=1,dim=7):
	from modshogun import RealFeatures, BinaryLabels, CustomKernel, 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
def runShogunSVMProteinPolyMatchSpectrumKernel(train_xt, train_lt, test_xt):
	"""
	run svm with spectrum kernel
	"""

    ##################################################
    # set up svm
	charfeat_train = StringCharFeatures(train_xt, DNA)
	feats_train = StringWordFeatures(DNA)
	feats_train.obtain_from_char(charfeat_train, K-1, K, GAP, False)
	preproc=SortWordString()
	preproc.init(feats_train)
	feats_train.add_preprocessor(preproc)
	feats_train.apply_preprocessor()
	
	charfeat_test = StringCharFeatures(test_xt, DNA)
	feats_test=StringWordFeatures(DNA)
	feats_test.obtain_from_char(charfeat_test, K-1, K, GAP, False)
	feats_test.add_preprocessor(preproc)
	feats_test.apply_preprocessor()
	
	kernel=PolyMatchWordStringKernel(feats_train, feats_train, DEGREE, True)
	kernel.io.set_loglevel(MSG_DEBUG)

    # init kernel
	labels = BinaryLabels(train_lt)
	
	# run svm model
	print "Ready to train!"
	svm=LibSVM(SVMC, kernel, labels)
	svm.io.set_loglevel(MSG_DEBUG)
	svm.train()

	# predictions
	print "Making predictions!"
	out1DecisionValues = svm.apply(feats_train)
	out1=out1DecisionValues.get_labels()
	kernel.init(feats_train, feats_test)
	out2DecisionValues = svm.apply(feats_test)
	out2=out2DecisionValues.get_labels()

	return out1,out2,out1DecisionValues,out2DecisionValues
Beispiel #9
0
def classifier_libsvm_minimal_modular(train_fname=traindat,
                                      test_fname=testdat,
                                      label_fname=label_traindat,
                                      width=2.1,
                                      C=1):
    from modshogun import RealFeatures, BinaryLabels
    from modshogun import LibSVM, GaussianKernel, CSVFile
    from modshogun import ErrorRateMeasure

    feats_train = RealFeatures(CSVFile(train_fname))
    feats_test = RealFeatures(CSVFile(test_fname))
    labels = BinaryLabels(CSVFile(label_fname))
    kernel = GaussianKernel(feats_train, feats_train, width)

    svm = LibSVM(C, kernel, labels)
    svm.train()

    out = svm.apply(feats_train)
    evaluator = ErrorRateMeasure()
    testerr = evaluator.evaluate(out, labels)
def classifier_multiclassmachine_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,width=2.1,C=1,epsilon=1e-5):
	from modshogun import RealFeatures, MulticlassLabels
	from modshogun import GaussianKernel
	from modshogun import LibSVM, KernelMulticlassMachine, MulticlassOneVsRestStrategy

	feats_train=RealFeatures(fm_train_real)
	feats_test=RealFeatures(fm_test_real)
	kernel=GaussianKernel(feats_train, feats_train, width)

	labels=MulticlassLabels(label_train_multiclass)

	classifier = LibSVM()
	classifier.set_epsilon(epsilon)
	#print labels.get_labels()
	mc_classifier = KernelMulticlassMachine(MulticlassOneVsRestStrategy(),kernel,classifier,labels)
	mc_classifier.train()

	kernel.init(feats_train, feats_test)
	out = mc_classifier.apply().get_labels()
	return out
def runShogunSVMDNAWDKernel(train_xt, train_lt, test_xt):
    """
	run svm with string kernels
	"""

    ##################################################
    # set up svm
    feats_train = StringCharFeatures(train_xt, DNA)
    feats_test = StringCharFeatures(test_xt, DNA)

    kernel = WeightedDegreePositionStringKernel(feats_train, feats_train,
                                                DEGREE)
    kernel.io.set_loglevel(MSG_DEBUG)
    kernel.set_shifts(NUMSHIFTS * ones(len(train_xt[0]), dtype=int32))
    kernel.set_position_weights(ones(len(train_xt[0]), dtype=float64))

    # init kernel
    labels = BinaryLabels(train_lt)

    # run svm model
    print "Ready to train!"
    svm = LibSVM(SVMC, kernel, labels)
    svm.io.set_loglevel(MSG_DEBUG)
    svm.train()

    # predictions
    print "Making predictions!"
    out1DecisionValues = svm.apply(feats_train)
    out1 = out1DecisionValues.get_labels()
    kernel.init(feats_train, feats_test)
    out2DecisionValues = svm.apply(feats_test)
    out2 = out2DecisionValues.get_labels()

    return out1, out2, out1DecisionValues, out2DecisionValues
Beispiel #12
0
def runShogunSVMDNAWDNoPositionKernel(train_xt, train_lt, test_xt):
    """
	run svm with non-position WD kernel
	"""

    ##################################################
    # set up svm
    feats_train = StringCharFeatures(train_xt, DNA)
    feats_test = StringCharFeatures(test_xt, DNA)

    kernel = WeightedDegreeStringKernel(feats_train, feats_train, DEGREE)
    kernel.io.set_loglevel(MSG_DEBUG)

    weights=arange(1,DEGREE+1,dtype=double)[::-1]/ \
     sum(arange(1,DEGREE+1,dtype=double))
    kernel.set_wd_weights(weights)

    # init kernel
    labels = BinaryLabels(train_lt)

    # run svm model
    print "Ready to train!"
    svm = LibSVM(SVMC, kernel, labels)
    svm.io.set_loglevel(MSG_DEBUG)
    svm.train()

    # predictions
    print "Making predictions!"
    out1 = svm.apply(feats_train).get_labels()
    kernel.init(feats_train, feats_test)
    out2 = svm.apply(feats_test).get_labels()

    return out1, out2
def classifier_ssk_modular(fm_train_dna=traindat,
                           fm_test_dna=testdat,
                           label_train_dna=label_traindat,
                           C=1,
                           maxlen=1,
                           decay=1):
    from modshogun import StringCharFeatures, BinaryLabels
    from modshogun import LibSVM, StringSubsequenceKernel, DNA
    from modshogun import ErrorRateMeasure

    feats_train = StringCharFeatures(fm_train_dna, DNA)
    feats_test = StringCharFeatures(fm_test_dna, DNA)
    labels = BinaryLabels(label_train_dna)
    kernel = StringSubsequenceKernel(feats_train, feats_train, maxlen, decay)

    svm = LibSVM(C, kernel, labels)
    svm.train()

    out = svm.apply(feats_train)
    evaluator = ErrorRateMeasure()
    trainerr = evaluator.evaluate(out, labels)
    # print(trainerr)

    kernel.init(feats_train, feats_test)
    predicted_labels = svm.apply(feats_test).get_labels()
    # print predicted_labels

    return predicted_labels
def runShogunSVMDNASubsequenceStringKernel(train_xt, train_lt, test_xt):
    """
	run svm with spectrum kernel
	"""

    ##################################################
    # set up svm
    feats_train = StringCharFeatures(train_xt, DNA)
    feats_test = StringCharFeatures(test_xt, DNA)

    kernel = SubsequenceStringKernel(feats_train, feats_train, MAXLEN, DECAY)
    kernel.io.set_loglevel(MSG_DEBUG)
    kernel.init(feats_train, feats_train)

    # init kernel
    labels = BinaryLabels(train_lt)

    # run svm model
    print "Ready to train!"
    svm = LibSVM(SVMC, kernel, labels)
    svm.io.set_loglevel(MSG_DEBUG)
    svm.train()

    # predictions
    print "Making predictions!"
    out1DecisionValues = svm.apply(feats_train)
    out1 = out1DecisionValues.get_labels()
    kernel.init(feats_train, feats_test)
    out2DecisionValues = svm.apply(feats_test)
    out2 = out2DecisionValues.get_labels()

    return out1, out2, out1DecisionValues, out2DecisionValues
def runShogunSVMDNALinearStringKernel(train_xt, train_lt, test_xt):
    """
	run svm with spectrum kernel
	"""

    ##################################################
    # set up svm
    feats_train = StringCharFeatures(train_xt, DNA)
    feats_test = StringCharFeatures(test_xt, DNA)

    kernel = LinearStringKernel(feats_train, feats_train)
    kernel.io.set_loglevel(MSG_DEBUG)

    # init kernel
    labels = BinaryLabels(train_lt)

    # run svm model
    print "Ready to train!"
    svm = LibSVM(SVMC, kernel, labels)
    svm.io.set_loglevel(MSG_DEBUG)
    svm.train()

    # predictions
    print "Making predictions!"
    out1 = svm.apply(feats_train).get_labels()
    kernel.init(feats_train, feats_test)
    out2 = svm.apply(feats_test).get_labels()

    return out1, out2
def kernel_combined_custom_poly_modular(train_fname=traindat,
                                        test_fname=testdat,
                                        train_label_fname=label_traindat):
    from modshogun import CombinedFeatures, RealFeatures, BinaryLabels
    from modshogun import CombinedKernel, PolyKernel, CustomKernel
    from modshogun import LibSVM, CSVFile

    kernel = CombinedKernel()
    feats_train = CombinedFeatures()

    tfeats = RealFeatures(CSVFile(train_fname))
    tkernel = PolyKernel(10, 3)
    tkernel.init(tfeats, tfeats)
    K = tkernel.get_kernel_matrix()
    kernel.append_kernel(CustomKernel(K))

    subkfeats_train = RealFeatures(CSVFile(train_fname))
    feats_train.append_feature_obj(subkfeats_train)
    subkernel = PolyKernel(10, 2)
    kernel.append_kernel(subkernel)

    kernel.init(feats_train, feats_train)

    labels = BinaryLabels(CSVFile(train_label_fname))
    svm = LibSVM(1.0, kernel, labels)
    svm.train()

    kernel = CombinedKernel()
    feats_pred = CombinedFeatures()

    pfeats = RealFeatures(CSVFile(test_fname))
    tkernel = PolyKernel(10, 3)
    tkernel.init(tfeats, pfeats)
    K = tkernel.get_kernel_matrix()
    kernel.append_kernel(CustomKernel(K))

    subkfeats_test = RealFeatures(CSVFile(test_fname))
    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
def classifier_ssk_modular (fm_train_dna=traindat,fm_test_dna=testdat,
		label_train_dna=label_traindat,C=1,maxlen=1,decay=1):
	from modshogun import StringCharFeatures, BinaryLabels
	from modshogun import LibSVM, StringSubsequenceKernel, DNA
	from modshogun import ErrorRateMeasure

	feats_train=StringCharFeatures(fm_train_dna, DNA)
	feats_test=StringCharFeatures(fm_test_dna, DNA)
	labels=BinaryLabels(label_train_dna)
	kernel=StringSubsequenceKernel(feats_train, feats_train, maxlen, decay);

	svm=LibSVM(C, kernel, labels);
	svm.train();

	out=svm.apply(feats_train);
	evaluator = ErrorRateMeasure()
	trainerr = evaluator.evaluate(out,labels)
	# print(trainerr)

	kernel.init(feats_train, feats_test)
	predicted_labels=svm.apply(feats_test).get_labels()
	# print predicted_labels

	return predicted_labels
def classifier_libsvm_modular (train_fname=traindat,test_fname=testdat,label_fname=label_traindat,width=2.1,C=1,epsilon=1e-5):
	from modshogun import RealFeatures, BinaryLabels
	from modshogun import GaussianKernel, LibSVM, CSVFile

	feats_train=RealFeatures(CSVFile(train_fname))
	feats_test=RealFeatures(CSVFile(test_fname))
	labels=BinaryLabels(CSVFile(label_fname))
	kernel=GaussianKernel(feats_train, feats_train, width)

	svm=LibSVM(C, kernel, labels)
	svm.set_epsilon(epsilon)
	svm.train()

	supportvectors = sv_idx=svm.get_support_vectors()
	alphas=svm.get_alphas()
	predictions = svm.apply(feats_test)
	#print predictions.get_labels()
	return predictions, svm, predictions.get_labels()
Beispiel #19
0
def runShogunSVMDNACombinedSpectrumKernel(train_xt, train_lt, test_xt):
	"""
	run svm with combined spectrum kernel
	"""

    ##################################################
    # set up svm
	kernel=CombinedKernel()
	feats_train=CombinedFeatures()
	feats_test=CombinedFeatures()
	
	for K in KList:
		# Iterate through the K's and make a spectrum kernel for each
		charfeat_train = StringCharFeatures(train_xt, DNA)
		current_feats_train = StringWordFeatures(DNA)
		current_feats_train.obtain_from_char(charfeat_train, K-1, K, GAP, False)
		preproc=SortWordString()
		preproc.init(current_feats_train)
		current_feats_train.add_preprocessor(preproc)
		current_feats_train.apply_preprocessor()
		feats_train.append_feature_obj(current_feats_train)
	
		charfeat_test = StringCharFeatures(test_xt, DNA)
		current_feats_test=StringWordFeatures(DNA)
		current_feats_test.obtain_from_char(charfeat_test, K-1, K, GAP, False)
		current_feats_test.add_preprocessor(preproc)
		current_feats_test.apply_preprocessor()
		feats_test.append_feature_obj(current_feats_test)
	
		current_kernel=CommWordStringKernel(10, False)
		kernel.append_kernel(current_kernel)
	
	kernel.io.set_loglevel(MSG_DEBUG)

    # init kernel
	labels = BinaryLabels(train_lt)
	
	# run svm model
	print "Ready to train!"
	kernel.init(feats_train, feats_train)
	svm=LibSVM(SVMC, kernel, labels)
	svm.io.set_loglevel(MSG_DEBUG)
	svm.train()

	# predictions
	print "Making predictions!"
	out1DecisionValues = svm.apply(feats_train)
	out1=out1DecisionValues.get_labels()
	kernel.init(feats_train, feats_test)
	out2DecisionValues = svm.apply(feats_test)
	out2=out2DecisionValues.get_labels()

	return out1,out2,out1DecisionValues,out2DecisionValues
def classifier_libsvm_modular(train_fname=traindat,
                              test_fname=testdat,
                              label_fname=label_traindat,
                              width=2.1,
                              C=1,
                              epsilon=1e-5):
    from modshogun import RealFeatures, BinaryLabels
    from modshogun import GaussianKernel, LibSVM, CSVFile

    feats_train = RealFeatures(CSVFile(train_fname))
    feats_test = RealFeatures(CSVFile(test_fname))
    labels = BinaryLabels(CSVFile(label_fname))
    kernel = GaussianKernel(feats_train, feats_train, width)

    svm = LibSVM(C, kernel, labels)
    svm.set_epsilon(epsilon)
    svm.train()

    supportvectors = sv_idx = svm.get_support_vectors()
    alphas = svm.get_alphas()
    predictions = svm.apply(feats_test)
    #print predictions.get_labels()
    return predictions, svm, predictions.get_labels()
def kernel_combined_custom_poly_modular (train_fname = traindat,test_fname = testdat,train_label_fname=label_traindat):
    from modshogun import CombinedFeatures, RealFeatures, BinaryLabels
    from modshogun import CombinedKernel, PolyKernel, CustomKernel
    from modshogun import LibSVM, CSVFile
   
    kernel = CombinedKernel()
    feats_train = CombinedFeatures()
    
    tfeats = RealFeatures(CSVFile(train_fname))
    tkernel = PolyKernel(10,3)
    tkernel.init(tfeats, tfeats)
    K = tkernel.get_kernel_matrix()
    kernel.append_kernel(CustomKernel(K))
        
    subkfeats_train = RealFeatures(CSVFile(train_fname))
    feats_train.append_feature_obj(subkfeats_train)
    subkernel = PolyKernel(10,2)
    kernel.append_kernel(subkernel)

    kernel.init(feats_train, feats_train)
    
    labels = BinaryLabels(CSVFile(train_label_fname))
    svm = LibSVM(1.0, kernel, labels)
    svm.train()

    kernel = CombinedKernel()
    feats_pred = CombinedFeatures()

    pfeats = RealFeatures(CSVFile(test_fname))
    tkernel = PolyKernel(10,3)
    tkernel.init(tfeats, pfeats)
    K = tkernel.get_kernel_matrix()
    kernel.append_kernel(CustomKernel(K))

    subkfeats_test = RealFeatures(CSVFile(test_fname))
    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
Beispiel #22
0
def runShogunSVMDNAUlongSpectrumKernel(train_xt, train_lt, test_xt):
    """
	run svm with ulong spectrum kernel
	"""

    ##################################################
    # set up svr
    charfeat_train = StringCharFeatures(train_xt, DNA)
    feats_train = StringUlongFeatures(DNA)
    feats_train.obtain_from_char(charfeat_train, K - 1, K, GAP, False)
    preproc = SortUlongString()
    preproc.init(feats_train)
    feats_train.add_preprocessor(preproc)
    feats_train.apply_preprocessor()

    charfeat_test = StringCharFeatures(test_xt, DNA)
    feats_test = StringUlongFeatures(DNA)
    feats_test.obtain_from_char(charfeat_test, K - 1, K, GAP, False)
    feats_test.add_preprocessor(preproc)
    feats_test.apply_preprocessor()

    kernel = CommUlongStringKernel(feats_train, feats_train, False)
    kernel.io.set_loglevel(MSG_DEBUG)

    # init kernel
    labels = BinaryLabels(train_lt)

    # run svm model
    print "Ready to train!"
    svm = LibSVM(SVMC, kernel, labels)
    svm.io.set_loglevel(MSG_DEBUG)
    svm.train()

    # predictions
    print "Making predictions!"
    out1DecisionValues = svm.apply(feats_train)
    out1 = out1DecisionValues.get_labels()
    kernel.init(feats_train, feats_test)
    out2DecisionValues = svm.apply(feats_test)
    out2 = svm.apply(feats_test).get_labels()

    return out1, out2, out1DecisionValues, out2DecisionValues
def classifier_custom_kernel_modular(C=1, dim=7):
    from modshogun import RealFeatures, BinaryLabels, CustomKernel, 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
def runShogunSVMProteinPolyMatchSpectrumKernel(train_xt, train_lt, test_xt):
    """
	run svm with spectrum kernel
	"""

    ##################################################
    # set up svm
    charfeat_train = StringCharFeatures(train_xt, PROTEIN)
    feats_train = StringWordFeatures(PROTEIN)
    feats_train.obtain_from_char(charfeat_train, K - 1, K, GAP, False)
    preproc = SortWordString()
    preproc.init(feats_train)
    feats_train.add_preprocessor(preproc)
    feats_train.apply_preprocessor()

    charfeat_test = StringCharFeatures(test_xt, PROTEIN)
    feats_test = StringWordFeatures(PROTEIN)
    feats_test.obtain_from_char(charfeat_test, K - 1, K, GAP, False)
    feats_test.add_preprocessor(preproc)
    feats_test.apply_preprocessor()

    kernel = PolyMatchWordStringKernel(feats_train, feats_train, 2, True)
    kernel.io.set_loglevel(MSG_DEBUG)

    # init kernel
    labels = BinaryLabels(train_lt)

    # run svm model
    print "Ready to train!"
    svm = LibSVM(SVMC, kernel, labels)
    svm.io.set_loglevel(MSG_DEBUG)
    svm.train()

    # predictions
    print "Making predictions!"
    out1DecisionValues = svm.apply(feats_train)
    out1 = out1DecisionValues.get_labels()
    kernel.init(feats_train, feats_test)
    out2DecisionValues = svm.apply(feats_test)
    out2 = out2DecisionValues.get_labels()

    return out1, out2, out1DecisionValues, out2DecisionValues
from numpy import random
from modshogun import LibSVM
from modshogun import RealFeatures, Labels
from modshogun import LinearKernel


num_feats=23
num_vec=42

scale=2.1
size_cache=10

C=0.017
epsilon=1e-5
tube_epsilon=1e-2
svm=LibSVM()
svm.set_C(C, C)
svm.set_epsilon(epsilon)
svm.set_tube_epsilon(tube_epsilon)

for i in range(3):
	data_train=random.rand(num_feats, num_vec)
	data_test=random.rand(num_feats, num_vec)
	feats_train=RealFeatures(data_train)
	feats_test=RealFeatures(data_test)
	labels=Labels(random.rand(num_vec).round()*2-1)

	svm.set_kernel(LinearKernel(size_cache, scale))
	svm.set_labels(labels)

	kernel=svm.get_kernel()
Beispiel #26
0
from modshogun import LibSVM, LDA
from modshogun 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.apply(),labels)
Beispiel #27
0
from modshogun import LibSVM, LDA
from modshogun import ROCEvaluation
import util

util.set_title('ROC 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 ROC for SVM
subplot(223)
ROC_evaluation=ROCEvaluation()
ROC_evaluation.evaluate(svm.apply(),labels)