Пример #1
0
def run_classification(train, test, labels):
    lin = LibLinear(L2R_L2LOSS_SVC)
    lin.set_bias_enabled(True)
    lin.set_C(5., 5.)

    machine = LinearMulticlassMachine(MulticlassOneVsRestStrategy(), train, lin, labels)

    machine.train()

    pred = machine.apply_multiclass(test)
from modshogun import LibLinear, L2R_L2LOSS_SVC_DUAL

from numpy import random, mean

X_train = RealFeatures(random.randn(30, 100))
Y_train = BinaryLabels(random.randn(X_train.get_num_vectors()))

results = []

for C1_pow in range(-3, 1):
    for C2_pow in range(-3, 1):

        svm = LibLinear()
        svm.set_bias_enabled(False)
        svm.set_liblinear_solver_type(L2R_L2LOSS_SVC_DUAL)
        svm.set_C(10**C1_pow, 10**C2_pow)

        svm.set_features(X_train)
        svm.set_labels(Y_train)
        svm.train()

        Y_pred = svm.apply_binary(X_train)
        accuracy = mean(Y_train.get_labels() == Y_pred.get_labels())

        print 10**C1_pow, 10**C2_pow, accuracy
        results.append({"accuracy":accuracy, "svm":svm})

results.sort(key=lambda x:x["accuracy"], reverse=True)

best_svm = results[0]["svm"]
print "best machine weights:", best_svm.get_w()