python - 如何设置带有评估集的学习xgboost?

标签 python linux python-3.x typeerror xgboost

在使用 sklearn wrapper 时,这对我来说很容易做到:

import xgboost as xgb
clf = xgb.XGBClassifier( n_estimators=1500, learning_rate=0.015, gamma =0.3, min_child_weight = 3,nthread = 15,max_depth=150,
                        subsample=0.9, colsample_bytree=0.8, seed=2100,  eval_metric = "rmse")

VALID = True
if VALID == True:
    X_train, X_valid, y_train, y_valid = train_test_split(
        X, y, test_size = 0.19, random_state=23)
    model = xgb.train(X_train, y_train,  params,
                      evallist = [(X_valid, y_valid)], 
                      verbose_eval = 50, 
            early_stopping_rounds=50)

但是我不能使用 xgboost 的标准类来设置它:

params =   {
    'objective' : 'gpu:reg:linear',
    'learning_rate': 0.02, 
    'gamma' : 0.3, 
    'min_child_weight' : 3,
    'nthread' : 15,
    'max_depth' : 30,
    'subsample' : 0.9, 
    'colsample_bytree' : 0.8, 
    'seed':2100, 
    'eval_metric' : "rmse",
    'num_boost_round' : 300
}

VALID = True
if VALID == True:
    X_train, X_valid, y_train, y_valid = train_test_split(
        X, y, test_size = 0.19, random_state=23)
    model = xgb.train(X_train, y_train,  params,
                      evallist = [(X_valid, y_valid)], 
                      verbose_eval = 50, 
            early_stopping_rounds=50)

#error TypeError: train() got an unexpected keyword argument 'evallist'

最佳答案

只需要正确指定参数:

params =   {
    #'objective' : 'gpu:reg:linear',
    'tree_method':'gpu_hist',
    'learning_rate': 0.02, 
    'gamma' : 0.3, 
    'min_child_weight' : 3,
    'nthread' : 15,
    'max_depth' : 30,
    'subsample' : 0.9, 
    'colsample_bytree' : 0.8, 
    'seed':2100, 
    'eval_metric' : "rmse",
    'num_boost_round' : 300,
    'n_estimators':999,
    'max_leaves': 300
}

VALID = True
if VALID == True:
    X_train, X_valid, y_train, y_valid = train_test_split(
        X, y, test_size = 0.19, random_state=23)

    tr_data = xgb.DMatrix(X_train, y_train)
    va_data = xgb.DMatrix(X_valid, y_valid)


    #del X_train, X_valid, y_train, y_valid  ; gc.collect()

    watchlist = [(tr_data, 'train'), (va_data, 'valid')]

    model = xgb.train(params, tr_data, 300, watchlist, maximize=False, early_stopping_rounds = 30, verbose_eval=50)

关于python - 如何设置带有评估集的学习xgboost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51000911/

相关文章:

linux - 解析非常规命令输出

python - 使用 python 从多个列表中嵌套字典

合并两个重叠列表的Pythonic方法,保留顺序

python - Gensim Doc2Vec 异常 AttributeError : 'str' object has no attribute 'decode'

python - 为什么 Python 的标准库对发送电子邮件的支持很弱

linux - 我在哪里可以找到这个函数 G_STRUCT_OFFSET,我的意思是在哪个 ".c"中实现了这个函数

python - 具有同一对象的多个 View 集和路由器的 Django Rest Framework

ruby-on-rails - 在 Virtuablbox 中的小型 Linux 发行版上开发 Ruby Rails 应用程序

python - 从 Kubernetes pod 将大文件上传到 Google Storage GCE

python - 如何在pytest中捕获异常后打印消息