開始使用免費開始

衡量準確率

你現在將透過 XGBoost 內建的交叉驗證功能,練習使用其學習 API。正如 Sergey 在前一支影片中說明的,XGBoost 之所以表現優異且效率高,是因為它使用名為 DMatrix 的、為資料集最佳化的資料結構。

在上一個練習中,輸入的資料集是即時轉換成 DMatrix;但當你使用 xgboostcv 物件時,必須先明確把資料轉成 DMatrix。因此,在對 churn_data 進行交叉驗證之前,你會先完成這個轉換。

本練習屬於課程

使用 XGBoost 的極端梯度提升

檢視課程

練習說明

  • 使用 xgb.DMatrix()churn_data 建立名為 churn_dmatrixDMatrix。特徵在 X,標籤在 y
  • 呼叫 xgb.cv() 進行 3 折交叉驗證。dtrain 為你的 churn_dmatrixparams 為你的參數字典,nfold 是交叉驗證的折數(3),num_boost_round 是要建立的樹數(5),metrics 是你想計算的評估指標(此處為 "error",稍後會轉換成準確率)。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create arrays for the features and the target: X, y
X, y = churn_data.iloc[:,:-1], churn_data.iloc[:,-1]

# Create the DMatrix from X and y: churn_dmatrix
churn_dmatrix = ____(data=____, label=____)

# Create the parameter dictionary: params
params = {"objective":"reg:logistic", "max_depth":3}

# Perform cross-validation: cv_results
cv_results = ____(dtrain=____, params=____, 
                  nfold=____, num_boost_round=____, 
                  metrics="____", as_pandas=____, seed=123)

# Print cv_results
print(cv_results)

# Print the accuracy
print(((1-cv_results["test-error-mean"]).iloc[-1]))
編輯並執行程式碼