在訓練與測試資料上評估模型
函式 auc_train_test 會計算一個以 train 集建立並在 test 集上評估之模型的 AUC:
auc_train, auc_test = auc_train_test(variables, target, train, test)
其中 variables 是模型所用變數名稱的清單。
在這個練習中,你將套用此函式,並檢查訓練與測試的 AUC 是否相近。
本練習屬於課程
Python 預測分析入門
練習說明
basetable已載入。請切分 basetable,讓訓練集包含 70% 的資料,並確保訓練與測試集的目標變數發生率相同。- 使用
auc_train_test函式,計算以"age"與"gender_F"為預測因子的模型在訓練與測試上的 AUC。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load the partitioning module
from sklearn.model_selection import train_test_split
# Create DataFrames with variables and target
X = basetable.drop('target', 1)
y = basetable["target"]
# Carry out 70-30 partititioning with stratification
X_train, X_test, y_train, y_test = ____(X, y, test_size = ____, stratify = ____)
# Create the final train and test basetables
train = pd.concat([X_train, y_train], axis=1)
test = pd.concat([X_test, y_test], axis=1)
# Apply the auc_train_test function
auc_train, auc_test = ____([____, ____], ["target"], ____, ____)
print(round(auc_train,2))
print(round(auc_test,2))