python - 我对使用 Xgboost 的 hyperopt 包的 fmin() 函数有很大的兴趣

标签 python xgboost databricks hyperopt

使用databricks,我基本上从这里复制并粘贴了相同的代码:https://www.dataiku.com/learn/guide/code/python/advanced-xgboost-tuning.html

我遇到了这个错误:

train = data.sample(frac=0.70, random_state=123)
valid = data.loc[~data.index.isin(train.index), :]

y_train = train['target']
X_train = train.drop(['target'], axis=1) 
y_valid = test['target']
X_valid = test.drop(['target'], axis=1) 
def objective(space):

    clf = xgb.XGBClassifier(n_estimators = 10000,
                            max_depth = space['max_depth'],
                            min_child_weight = space['min_child_weight'],
                            subsample = space['subsample'])

    eval_set  = [( train, y_train), ( valid, y_valid)]

    clf.fit(train[col_train], y_train,
            eval_set=eval_set, eval_metric="auc",
            early_stopping_rounds=30)

    pred = clf.predict_proba(valid)[:,1]
    auc = roc_auc_score(y_valid, pred)
    print("SCORE:", auc)

    return{'loss':1-auc, 'status': STATUS_OK }


space ={
        'max_depth': hp.quniform("x_max_depth", 5, 30, 1),
        'min_child_weight': hp.quniform ('x_min_child', 1, 10, 1),
        'subsample': hp.uniform ('x_subsample', 0.8, 1)
    }


trials = Trials()
best = fmin(fn=objective,
            space =space,
            algo=tpe.suggest,
            max_evals=100,
            trials=trials)


print(best)

我遇到的错误如下:

ValueError: invalid number of arguments
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<command-3897094> in <module>()
      4             algo=tpe.suggest,
      5             max_evals=100,
----> 6             trials=trials)
      7 
      8 

ValueError: invalid number of arguments

任何见解都会有所帮助。关于堆栈溢出的第一个问题!

最佳答案

博客Advanced XGBoost tuning in Python您引用的是2016年8月22日的帖子,如下图。

enter image description here

我认为它适用于旧版本,可能不适合使用最新版本的 hyperopt 包。

因此,请参阅 FMin 的最新 hyperopt wiki 页面。 。下面是一个简单的示例代码。

from hyperopt import fmin, tpe, hp
best = fmin(fn=lambda x: x ** 2,
    space=hp.uniform('x', -10, 10),
    algo=tpe.suggest,
    max_evals=100)
print best

可以看到space类型应该是hyperopt.pyll.Apply,而不是Python字典,请参见code来自 GitHub 存储库,如下图。

enter image description here

关于python - 我对使用 Xgboost 的 hyperopt 包的 fmin() 函数有很大的兴趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58530665/

相关文章:

python - 用另一个 pandas DataFrame 更新存储在 Pytable 中的 pandas DataFrame

python运行字典中的值计数

python - 使用 sklearn xgboost gridsearchcv 的多个评分指标

machine-learning - XGBoost - n_estimators = 1 等于单树分类器?

python - 在 python 中使用 paramiko 打开远程文件慢

python - 如何在 conda environment.yml 文件中 pip 安装包?

machine-learning - XGBoostplot_importance无法显示特征名称

python - 如何将 sklearn 管道转换为 pyspark 管道?

apache-spark - 为 Databricks 生成数据库架构图

sql - 按日期列的子集对增量表进行分区