Boosting rounds की संख्या ट्यून करना
आइए पैरामीटर ट्यूनिंग की शुरुआत इस तरह करें कि boosting rounds (आप जितने पेड़ बनाते हैं) की संख्या आपके XGBoost मॉडल के out-of-sample प्रदर्शन को कैसे प्रभावित करती है. आप for लूप के अंदर xgb.cv() का उपयोग करेंगे और num_boost_round पैरामीटर के प्रति एक मॉडल बनाएँगे.
यहाँ, आप Ames housing डेटासेट पर काम जारी रखेंगे. फीचर्स X array में उपलब्ध हैं, और टार्गेट वेक्टर y में है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
XGBoost के साथ Extreme Gradient Boosting
अभ्यास निर्देश
Xऔरyसे एकDMatrixबनाइए जिसका नामhousing_dmatrixहो.paramsनाम की एक पैरामीटर डिक्शनरी बनाइए, जिसमें उपयुक्त"objective"("reg:squarederror") और"max_depth"(इसे3सेट करें) पास करें.forलूप के अंदरnum_roundsपर iterate कीजिए और 3-fold cross-validation चलाइए. लूप की हर iteration में, मौजूदा boosting rounds की संख्या (curr_num_rounds) कोxgb.cv()मेंnum_boost_roundआर्ग्यूमेंट के रूप में पास करें.- हर cross-validated XGBoost मॉडल के लिए अंतिम boosting राउंड का RMSE,
final_rmse_per_roundलिस्ट में append कीजिए. num_roundsऔरfinal_rmse_per_roundको zip करके DataFrame में बदला जा चुका है ताकि आप आसानी से देख सकें कि हर boosting राउंड के साथ मॉडल कैसा परफॉर्म करता है. परिणाम देखने के लिए 'उत्तर सबमिट करें' दबाइए!
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Create the DMatrix: housing_dmatrix
housing_dmatrix = ____
# Create the parameter dictionary for each tree: params
params = {"____":"____", "____":____}
# Create list of number of boosting rounds
num_rounds = [5, 10, 15]
# Empty list to store final round rmse per XGBoost model
final_rmse_per_round = []
# Iterate over num_rounds and build one model per num_boost_round parameter
for curr_num_rounds in num_rounds:
# Perform cross-validation: cv_results
cv_results = ____(dtrain=____, params=____, nfold=3, num_boost_round=____, metrics="rmse", as_pandas=True, seed=123)
# Append final round RMSE
____.____(cv_results["test-rmse-mean"].tail().values[-1])
# Print the resultant DataFrame
num_rounds_rmses = list(zip(num_rounds, final_rmse_per_round))
print(pd.DataFrame(num_rounds_rmses,columns=["num_boosting_rounds","rmse"]))