手动计算预测值
与简单线性回归一样,您也可以用模型系数手动计算预测值。并行斜率回归唯一的变化是:截距会随分类解释变量的类别而不同。这意味着您需要分别考虑每个类别出现时的情况。
taiwan_real_estate、mdl_price_vs_both 和 explanatory_data 可用;上一练习中的 ic_0_15、ic_15_30、ic_30_45 和 slope 也已加载。
本练习是课程的一部分
Python 中级回归:使用 statsmodels
练习说明
- 定义一个列表
conditions,包含 3 个条件语句:house_age_years等于"0 to 15",house_age_years等于"15 to 30",以及house_age_years等于"30 to 45"。 - 定义一个列表
choices,包含从mdl_price_vs_both提取的截距。它们与上述各条件一一对应。 - 使用
np.select()创建一个截距数组。 - 创建
prediction_data:以explanatory_data为起点,赋值intercept为该截距数组,并将price_twd_msq设为手动计算的预测值。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define conditions
conditions = [____]
# Define choices
choices = [____]
# Create array of intercepts for each house_age_year category
intercept = ____(____, ____)
# Create prediction_data with columns intercept and price_twd_msq
prediction_data = ____.____(
____ = ____,
____ = ____ + ____ * ____[____])
print(prediction_data)