使用Xgboost进行调优时出现参数网格错误

2024-04-20 05:20:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在XG Boost模型上编写超参数调优代码。然而,我不断地得到一个错误。代码如下:

#define X,y
y = data.SalePrice
x = data.drop(['SalePrice'], axis=1).select_dtypes(exclude=['object'])

#test,train split
train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.3, random_state=0)

#Imputation transformer for completing missing values.
my_imputer = Imputer()

#Seperate train and test X
train_x = my_imputer.fit_transform(train_x)
test_x = my_imputer.transform(test_x)

然后,这里是数据的超参数:

# Set the parameters by cross-validation
tuned_parameters = [{'n_estimators': [5, 25, 50, 100, 250, 500],'learning_rate': [0.01,0.05]}]
scores = ['precision', 'recall']

for score in scores:
    print("# Tuning hyper-parameters for %s" % score)
    print()

    #XGBRegressor
    clf = GridSearchCV(XGBRegressor(tuned_parameters), cv=5,scoring='%s_macro' % score)
    clf.fit(train_x, train_y)

    print("Best parameters set found on development set:")
    print(clf.best_params_)

我得到的错误是:TypeError:init()缺少1个必需的位置参数:“param_grid”


Tags: 代码testfordata参数my错误train
1条回答
网友
1楼 · 发布于 2024-04-20 05:20:59

看起来你把tuned_parameters:D放错地方了;请尝试将clf定义作为

clf = GridSearchCV(XGBRegressor(), param_grid=tuned_parameters, cv=5,scoring='%s_macro' % score)

让我知道这是否有效

相关问题 更多 >