Boosting turu sayısını ayarlama
Parametre ayarına, boosting turu sayısının (oluşturduğun ağaç sayısı) XGBoost modelinin örnek dışı performansını nasıl etkilediğini inceleyerek başlayalım. xgb.cv() fonksiyonunu bir for döngüsü içinde kullanacak ve her num_boost_round parametresi için bir model kuracaksın.
Burada Ames konut veri kümesiyle çalışmaya devam edeceksin. Özellikler X dizisinde, hedef vektörü ise y içinde yer alıyor.
Bu egzersiz
XGBoost ile Aşırı Gradyan Artırma
kursunun bir parçasıdırEgzersiz talimatları
Xvey'den birDMatrixoluştur ve adınahousing_dmatrixde.- Uygun
"objective"("reg:squarederror") ve"max_depth"(bunu3yap) değerlerini içerenparamsadlı bir parametre sözlüğü oluştur. num_roundsüzerinde birfordöngüsüyle yineleme yap ve 3 katlı çapraz doğrulama uygula. Döngünün her yinelemesinde, geçerli boosting turu sayısını (curr_num_rounds)xgb.cv()fonksiyonunanum_boost_roundargümanı olarak geçir.- Her çapraz doğrulanmış XGBoost modeli için son boosting turundaki RMSE değerini
final_rmse_per_roundlistesine ekle. num_roundsvefinal_rmse_per_roundbirlikte zip'lenip bir DataFrame'e dönüştürüldü; böylece modelin her boosting turuyla nasıl performans gösterdiğini kolayca görebileceksin. Sonuçları görmek için 'Yanıtı Gönder'e bas!
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# 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"]))