我創建了一個函式來對我的 xgboost 分類器的最佳引數執行網格搜索。因為我的訓練集很大,我想將網格搜索限制為大約 5000 個觀察值的樣本。
這是功能:
def xgboost_search(X, y, search_verbose=1):
params = {
"gamma":[0.5, 1, 1.5, 2, 5],
"max_depth":[3,4,5,6],
"min_child_weight": [100],
"subsample": [0.6, 0.8, 1.0],
"colsample_bytree": [0.6, 0.8, 1.0],
"learning_rate": [0.1, 0.01, 0.001]
}
xgb = XGBClassifier(objective="binary:logistic", eval_metric="auc", use_label_encoder=False)
skf = StratifiedKFold(n_splits=3, shuffle=True, random_state=1234)
grid_search = GridSearchCV(estimator=xgb, param_grid=params, scoring="roc_auc", n_jobs=1, cv=skf.split(X,y), verbose=search_verbose)
grid_search.fit(X, y)
print("Best estimator: ")
print(grid_search.best_estimator_)
print("Parameters: ", grid_search.best_params_)
print("Highest AUC: %.2f" % grid_search.best_score_)
return grid_search.best_params_
這就是我試圖獲得 5000 個觀察值的結果:
rows = random.sample(list(X_res), 5000)
model_params = xgboost_search(X_res[rows], Y_res[rows])
我收到了這個錯誤:
IndexError Traceback (most recent call last)
/var/folders/cf/yh2vvpdn0klby68k9zrttfv00000gp/T/ipykernel_80963/3533706692.py in <module>
1 rows = random.sample(list(X_res), 5000)
----> 2 model_params = xgboost_search(X_res[rows], Y_res[rows])
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
我認為這是因為我的 'X_res' 和 'Y_res' 是陣列,而 'rows' 是一個串列。
有人可以幫忙嗎?
uj5u.com熱心網友回復:
陣列可以由 a 索引list,這里的問題是串列中不是整數的索引型別:
只有整數 ... 是有效的索引
在錯誤可能是什么錯了。
Xres這是因為您對with的 5000 個元素進行了采樣,而不是像您可能想要的那樣在andrandom.sample(list(X_res), 5000)之間采樣了 5000 個索引。0len(Xres)
嘗試:
rows = random.sample(range(len(Xres)), 5000)
model_params = xgboost_search(X_res[rows], Y_res[rows])
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516945.html
標籤:机器学习
