def mkl_multiclass_modular (fm_train_real, fm_test_real, label_train_multiclass,
	width, C, epsilon, num_threads, mkl_epsilon, mkl_norm):

	from modshogun import CombinedFeatures, RealFeatures, MulticlassLabels
	from modshogun import CombinedKernel, GaussianKernel, LinearKernel,PolyKernel
	from modshogun import MKLMulticlass

	kernel = CombinedKernel()
	feats_train = CombinedFeatures()
	feats_test = CombinedFeatures()

	subkfeats_train = RealFeatures(fm_train_real)
	subkfeats_test = RealFeatures(fm_test_real)
	subkernel = GaussianKernel(10, width)
	feats_train.append_feature_obj(subkfeats_train)
	feats_test.append_feature_obj(subkfeats_test)
	kernel.append_kernel(subkernel)

	subkfeats_train = RealFeatures(fm_train_real)
	subkfeats_test = RealFeatures(fm_test_real)
	subkernel = LinearKernel()
	feats_train.append_feature_obj(subkfeats_train)
	feats_test.append_feature_obj(subkfeats_test)
	kernel.append_kernel(subkernel)

	subkfeats_train = RealFeatures(fm_train_real)
	subkfeats_test = RealFeatures(fm_test_real)
	subkernel = PolyKernel(10,2)
	feats_train.append_feature_obj(subkfeats_train)
	feats_test.append_feature_obj(subkfeats_test)
	kernel.append_kernel(subkernel)

	kernel.init(feats_train, feats_train)

	labels = MulticlassLabels(label_train_multiclass)

	mkl = MKLMulticlass(C, kernel, labels)

	mkl.set_epsilon(epsilon);
	mkl.parallel.set_num_threads(num_threads)
	mkl.set_mkl_epsilon(mkl_epsilon)
	mkl.set_mkl_norm(mkl_norm)

	mkl.train()

	kernel.init(feats_train, feats_test)

	out =  mkl.apply().get_labels()
	return out
Exemple #2
0
def evaluation_cross_validation_multiclass_storage (traindat=traindat, label_traindat=label_traindat):
    from modshogun import CrossValidation, CrossValidationResult
    from modshogun import CrossValidationPrintOutput
    from modshogun import CrossValidationMKLStorage, CrossValidationMulticlassStorage
    from modshogun import MulticlassAccuracy, F1Measure
    from modshogun import StratifiedCrossValidationSplitting
    from modshogun import MulticlassLabels
    from modshogun import RealFeatures, CombinedFeatures
    from modshogun import GaussianKernel, CombinedKernel
    from modshogun import MKLMulticlass
    from modshogun import Statistics, MSG_DEBUG, Math

    Math.init_random(1)

    # 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=MulticlassLabels(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=MKLMulticlass(1.0,kernel,labels);
    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, 3)

    # evaluation method
    evaluation_criterium=MulticlassAccuracy()

    # 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)
    multiclass_storage=CrossValidationMulticlassStorage()
    multiclass_storage.append_binary_evaluation(F1Measure())
    cross_validation.add_cross_validation_output(multiclass_storage)
    cross_validation.set_num_runs(3)

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

    roc_0_0_0 = multiclass_storage.get_fold_ROC(0,0,0)
    #print roc_0_0_0
    auc_0_0_0 = multiclass_storage.get_fold_evaluation_result(0,0,0,0)
    #print auc_0_0_0
    return roc_0_0_0, auc_0_0_0
Exemple #3
0
def mkl(train_features,
        train_labels,
        test_features,
        test_labels,
        width=5,
        C=1.2,
        epsilon=1e-2,
        mkl_epsilon=0.001,
        mkl_norm=2):
    from modshogun import CombinedKernel, CombinedFeatures
    from modshogun import GaussianKernel, LinearKernel, PolyKernel
    from modshogun import MKLMulticlass, MulticlassAccuracy

    kernel = CombinedKernel()
    feats_train = CombinedFeatures()
    feats_test = CombinedFeatures()

    feats_train.append_feature_obj(train_features)
    feats_test.append_feature_obj(test_features)
    subkernel = GaussianKernel(10, width)
    kernel.append_kernel(subkernel)

    feats_train.append_feature_obj(train_features)
    feats_test.append_feature_obj(test_features)
    subkernel = LinearKernel()
    kernel.append_kernel(subkernel)

    feats_train.append_feature_obj(train_features)
    feats_test.append_feature_obj(test_features)
    subkernel = PolyKernel(10, 2)
    kernel.append_kernel(subkernel)

    kernel.init(feats_train, feats_train)
    mkl = MKLMulticlass(C, kernel, train_labels)

    mkl.set_epsilon(epsilon)
    mkl.set_mkl_epsilon(mkl_epsilon)
    mkl.set_mkl_norm(mkl_norm)

    mkl.train()
    train_output = mkl.apply()

    kernel.init(feats_train, feats_test)

    test_output = mkl.apply()
    evaluator = MulticlassAccuracy()
    print 'MKL training error is %.4f' % (
        (1 - evaluator.evaluate(train_output, train_labels)) * 100)
    print 'MKL test error is %.4f' % (
        (1 - evaluator.evaluate(test_output, test_labels)) * 100)
Exemple #4
0
def mkl(train_features, train_labels, test_features, test_labels, width=5, C=1.2, epsilon=1e-2, mkl_epsilon=0.001, mkl_norm=2):
	from modshogun import CombinedKernel, CombinedFeatures
	from modshogun import GaussianKernel, LinearKernel, PolyKernel
	from modshogun import MKLMulticlass, MulticlassAccuracy

	kernel = CombinedKernel()
	feats_train = CombinedFeatures()
	feats_test = CombinedFeatures()

	feats_train.append_feature_obj(train_features)
	feats_test.append_feature_obj(test_features)
	subkernel = GaussianKernel(10,width)
	kernel.append_kernel(subkernel)

	feats_train.append_feature_obj(train_features)
	feats_test.append_feature_obj(test_features)
	subkernel = LinearKernel()
	kernel.append_kernel(subkernel)

	feats_train.append_feature_obj(train_features)
	feats_test.append_feature_obj(test_features)
	subkernel = PolyKernel(10,2)
	kernel.append_kernel(subkernel)

	kernel.init(feats_train, feats_train)
	mkl = MKLMulticlass(C, kernel, train_labels)

	mkl.set_epsilon(epsilon);
	mkl.set_mkl_epsilon(mkl_epsilon)
	mkl.set_mkl_norm(mkl_norm)

	mkl.train()
	train_output = mkl.apply()

	kernel.init(feats_train, feats_test)

	test_output = mkl.apply()
	evaluator = MulticlassAccuracy()
	print 'MKL training error is %.4f' % ((1-evaluator.evaluate(train_output, train_labels))*100)
	print 'MKL test error is %.4f' % ((1-evaluator.evaluate(test_output, test_labels))*100)