在测试集和训练集上评估模型
函数 auc_train_test 会计算在 train 集上训练、在 test 集上评估的模型的 AUC:
auc_train, auc_test = auc_train_test(variables, target, train, test)
其中,variables 是模型中使用的变量名列表。
在本练习中,您将调用此函数,并检查训练集和测试集的 AUC 是否相近。
本练习是课程的一部分
Python 预测分析入门
练习说明
- 已加载
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))