開始使用免費開始

時間軸違規

為了說明時間軸的重要性,來看一個違反時間軸、使用目標期間資訊來建構預測變數的例子。

pandas DataFrame basetable 中有兩個欄位:「amount_2017」是 2017 年的捐款總額;「target」在金額大於 30 時為 1,否則為 0。

建立一個以「amount_2017」作為唯一預測變數、用來預測目標的邏輯斯迴歸模型,並計算 AUC。

本練習屬於課程

Python 中級預測分析

檢視課程

練習說明

  • 建立一個包含預測變數的 DataFrame X,以及一個包含目標的 DataFrame y
  • 擬合邏輯斯迴歸模型,使 yX 進行預測。 建立一個以 amount_2017 作為唯一預測變數並預測 target 的邏輯斯迴歸模型。
  • X 中的物件進行預測。
  • 使用 roc_auc_score 函式計算並印出此模型的 AUC。

動手互動練習

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

# Select the relevant predictors and the target
X = basetable[["____"]]
y = basetable[["____"]]

# Build the logistic regression model
logreg = linear_model.LogisticRegression()
logreg.____(____, ____)

# Make predictions for X
predictions = logreg.____(____)[:,1]

# Calculate and print the AUC value
auc = ____(____, ____)
print(round(auc, 2))
編輯並執行程式碼