在使用Hyperopt进行Logistic回归时,无法拾取模块对象。

问题描述 投票:0回答:1

我正试图在Python中使用Hyperopt对Logistic回归进行微调。请找到下面的优化函数。

def objective(params, n_folds = N_FOLDS):
    """Objective function for Logistic Regression Hyperparameter Tuning"""

    # Perform n_fold cross validation with hyperparameters
    # Use early stopping and evaluate based on ROC AUC

    clf = LogisticRegression(**params,random_state=0,verbose =0)
    cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
    score = cross_val_score(clf, X_train, y_train, cv=cv, scoring='f1_macro')

    # Extract the best score
    best_score = max(score)

    # Loss must be minimized
    loss = 1 - best_score

    # Dictionary with information for evaluation
    return {'loss': loss, 'params': params, 'status': STATUS_OK}

当运行该算法时,它显示出以下错误。

TypeError: can't pickle module objects

TypeError                                 Traceback (most recent call last)
<ipython-input-22-934e4c484469> in <module>
      6 
      7 # Optimize
----> 8 best = fmin(fn = objective, space = space, algo = tpe.suggest, max_evals = MAX_EVALS, trials = bayes_trials)

<ipython-input-20-d9ea5e676676> in objective(params, n_folds)
      7     clf = LogisticRegression(**params,random_state=0,verbose =0)
      8     cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
----> 9     score = cross_val_score(clf, X_train, y_train, cv=cv, scoring='f1_macro')
      10 
      11     # Extract the best score

我怎样才能解决这个问题?

python machine-learning data-science logistic-regression hyperopt
1个回答
0
投票

我把空间中的类权重参数去掉,就解决了这个问题。

space = {

    'warm_start' : hp.choice('warm_start', [True, False]),
    'fit_intercept' : hp.choice('fit_intercept', [True, False]),
    'tol' : hp.uniform('tol', 0.00001, 0.0001),
    'C' : hp.uniform('C', 0.05, 3),
    'solver' : hp.choice('solver', ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga']),
    'max_iter' : hp.choice('max_iter', range(5,1000))
}
© www.soinside.com 2019 - 2024. All rights reserved.