Оптимизация гиперпараметров LinearSVC с использованием HyperOpt на python - PullRequest
0 голосов
/ 23 января 2019

Я пытаюсь оптимизировать гиперпараметр C LinearSVC, используя библиотеку HyperOpt на python, и я не знаю, какой диапазон поместить в C. Я использую логуниформное распределение, реализованное в библиотеке HyperOpt:

space= {'C': hp.loguniform('C',low= np.log(1e-7) , high=np.log(1e5))} 

Таким образом, я ожидаю, что диапазон будет между exp (log (1e-7)) и exp (log (1e5)), но у меня неожиданная ошибка:

*C:\Users\a70955\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\svm\liblinear.cp37-win32.pyd in sklearn.svm.liblinear.train_wrap()
ValueError: b'C <= 0'*

Может ли кто-нибудь мне помочьРешите эту проблему, пожалуйста?

Вот полный код:

**

# create a function to minimize.

def linearSVC_accuracy_cv(params, random_state=random_state, cv=kf, X=X_Singt3gram_vectorizer_train, y=y_cora_train):
    # the function gets a set of variable parameters in "param"
    params = {'C': int(params['C'])}
    # we use this params to create a new LinearSVC Classifier
    model = LinearSVC(random_state=random_state, **params)
    # and then conduct the cross validation with the same folds as before
    score = -cross_val_score(model, X, y, cv=cv, scoring="accuracy", n_jobs=-1).mean()
    return score
# possible values of parameters
space= {'C': hp.loguniform('C',low= np.log(1e-7) , high=np.log(1e5))}
# trials will contain logging information
trials = Trials()
best=fmin(fn=linearSVC_accuracy_cv, # function to optimize
          space=space, 
          algo=tpe.suggest, # optimization algorithm, hyperotp will select its parameters automatically
          max_evals=n_iter, # maximum number of iterations
          trials=trials, # logging
          rstate=np.random.RandomState(random_state) # fixing random state for the reproducibility
         )
# computing the score on the test set
model = LinearSVC(C=int(best['C']), random_state=random_state)
model.fit(X_Singt3gram_vectorizer_train,y_cora_train)
tpe_test_score=accuracy_score(y_cora_test, model.predict(X_Singt3gram_vectorizer_test))
print("Best Accuracy score {:.3f} params {}".format( linearSVC_accuracy_cv(best), best))

**

...