比较预测值
在上一个练习中,您使用 crab 数据分别拟合了线性回归和 GLM(逻辑回归)模型,用 width 预测 y。也就是说,您希望根据雌蟹的宽度来预测其附近是否有卫星蟹的概率。
本练习中,您将进一步查看两个模型输出的估计概率,并判断线性拟合是否适合当前问题。
常见做法是用新的、未见过的样本来测试模型。这样的数据集称为测试集(test sample)。
已为您创建并加载了 test 样本。请注意,您需要为模型中出现的所有变量提供测试值,在本例中即为 width。
crab 数据集已预加载到工作区。
本练习是课程的一部分
Python 中的广义线性模型
练习说明
- 使用
print()查看test集。 - 基于
test样本,使用已拟合的线性模型model_LM调用.predict()计算估计概率,并保存为pred_lm。同时,使用已拟合的 GLM(逻辑回归)模型model_GLM调用.predict()计算估计概率,并保存为pred_glm。 - 使用
pandas的DataFrame()合并两种模型的预测,并保存为predictions。 - 将
test与predictions连接,保存为all_data。使用print()查看all_data。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# View test set
print(____)
# Compute estimated probabilities for linear model: pred_lm
____ = model_LM.____(____)
# Compute estimated probabilities for GLM model: pred_glm
____ = model_GLM.____(____)
# Create dataframe of predictions for linear and GLM model: predictions
____ = pd.DataFrame({'Pred_LM': ____, 'Pred_GLM': ____})
# Concatenate test sample and predictions and view the results
all_data = pd.concat([____, ____], axis = 1)
print(____)