使用Xgboost进行调整时出现param_grid错误

时间:2019-06-10 00:09:50

标签: python machine-learning hyperparameters

我试图在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'

1 个答案:

答案 0 :(得分:0)

好像您放错了tuned_parameters:D;尝试将clf的定义重做为

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

让我知道是否可行。