在不决定内核的情况下调整svc中的optunity示例错误

时间:2017-01-14 14:06:46

标签: scikit-learn optunity

我正在寻找如何调整svm参数但是在一篇文章中说,在sklearn中天生的gridsearch不是最有效的调整方法,但是有了优先权,所以如果找到这个http://optunity.readthedocs.io/en/latest/notebooks/notebooks/sklearn-svc.html#tune-svc-without-deciding-the-kernel-in-advance我适应可能代码进入这一个,但我遇到了一个错误:

"ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" I do not know what is wrong.


#here is the source code 

      import optunity
import optunity.metrics
import numpy as np

# k nearest neighbours
from sklearn.neighbors import KNeighborsClassifier
# support vector machine classifier
from sklearn.svm import SVC
# Naive Bayes
from sklearn.naive_bayes import GaussianNB
# Random Forest
from sklearn.ensemble import RandomForestClassifier
from sklearn.externals import joblib
import sklearn
im_features,image_classes,training_names,stdSlr,kmeans,k = joblib.load("others.pkl")

n = len(image_classes)


data = im_features
labels = np.array(image_classes)


cv_decorator = optunity.cross_validated(x=data, y=labels, num_folds=5)

space = {'kernel': {'linear': {'C': [0, 2]},
                    'rbf': {'logGamma': [-5, 0], 'C': [0, 10]},
                    'poly': {'degree': [2, 5], 'C': [0, 5], 'coef0': [0, 2]}
                    }
         }


def train_model(x_train, y_train, kernel, C, logGamma, degree, coef0):
    """A generic SVM training function, with arguments based on the chosen kernel."""
    if kernel == 'linear':
        model = sklearn.svm.SVC(kernel=kernel, C=C)
    elif kernel == 'poly':
        model = sklearn.svm.SVC(kernel=kernel, C=C, degree=degree, coef0=coef0)
    elif kernel == 'rbf':
        model = sklearn.svm.SVC(kernel=kernel, C=C, gamma=10 ** logGamma)
    else:
        raise ArgumentError("Unknown kernel function: %s" % kernel)
    model.fit(x_train, y_train)
    return model


def svm_tuned_auroc(x_train, y_train, x_test, y_test, kernel='linear', C=0, logGamma=0, degree=0, coef0=0):
    model = train_model(x_train, y_train, kernel, C, logGamma, degree, coef0)
    decision_values = model.decision_function(x_test)
    return optunity.metrics.roc_auc(y_test, decision_values)

svm_tuned_auroc = cv_decorator(svm_tuned_auroc)

optimal_svm_pars, info, _ = optunity.maximize_structured(svm_tuned_auroc, space, num_evals=150)
print("Optimal parameters" + str(optimal_svm_pars))
print("AUROC of tuned SVM: %1.3f" % info.optimum)

我的代码出了什么问题。我只在示例代码中替换了datalabels。任何人都可以帮我这个。我非常需要调试这个。谢谢你

完整错误在这里:

Traceback error

1 个答案:

答案 0 :(得分:0)

您需要使用labelencoder

对标签进行编码