XGBoost特征重要性以及CV

1 feature importance

gradient boosting 有一個優(yōu)點是可以給出訓(xùn)練好的模型的特征重要性,這樣就可以知道哪些變量需要被保留,哪些可以舍棄。
首先要進入兩個類:

from xgboost import plot_importance
from matplotlib import pyplot

然后在model fit后面添加打印或繪制特征重要性:

model.fit(X, y)
plot_importance(model)
pyplot.show()

2 參數(shù)優(yōu)化

XGBoost中提供GridSearchCV方法來對一些重要的超參進行調(diào)整,例如:
learning_rate
tree_depth
subsample
n_estimators
max_depth

當(dāng)然,大多時候?qū)W習(xí)率的大小一定程度上很影響模型的穩(wěn)定性,學(xué)習(xí)率較小容易過擬合,學(xué)習(xí)率較大又容易欠擬合,所以很多時候都會先考慮對學(xué)習(xí)率進行調(diào)整。
引入如下兩個類:

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold

將要嘗試的學(xué)習(xí)率的值放在一個list中即可:
learning_rate = [0.005, 0.001, 0.01, 0.1, 0.2, 0.3]

model = XGBClassifier()
learning_rate = [0.005, 0.001, 0.01, 0.1, 0.2, 0.3]
param_grid = dict(learning_rate=learning_rate)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)
grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss", n_jobs=-1, cv=kfold)
grid_result = grid_search.fit(X, Y)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容