BaşlayınÜcretsiz Başlayın

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ır
Kursu Görüntüle

Egzersiz talimatları

  • X ve y'den bir DMatrix oluştur ve adına housing_dmatrix de.
  • Uygun "objective" ("reg:squarederror") ve "max_depth" (bunu 3 yap) değerlerini içeren params adlı bir parametre sözlüğü oluştur.
  • num_rounds üzerinde bir for dö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() fonksiyonuna num_boost_round argümanı olarak geçir.
  • Her çapraz doğrulanmış XGBoost modeli için son boosting turundaki RMSE değerini final_rmse_per_round listesine ekle.
  • num_rounds ve final_rmse_per_round birlikte 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"]))
Kodu Düzenle ve Çalıştır