使用Python CausalImpact包报错:'CausalImpact'对象无'inferences'属性
报错原因
AttributeError: 'CausalImpact' object has no attribute 'inferences' 出现的核心原因是CausalImpact建模流程中断,没有成功生成内部结果属性就返回了对象,你的场景下主要触发原因是数据集索引和时间区间参数不匹配:
你当前使用的DataFrame是默认整数索引,但是传入的pre_period、post_period是日期字符串,CausalImpact无法匹配到对应的时间切片,建模直接中断。
修复步骤
- 先将日期列转为datetime格式并设置为数据集索引,保证时间区间参数能匹配到数据切片:
df['Order_Date'] = pd.to_datetime(df['Order_Date']) df = df.set_index('Order_Date')
- 确认
pre_period和post_period的边界日期都存在于数据集索引中,不要传入数据中没有的日期 - 重新运行CausalImpact分析,如果你只有单门店的销售数据(无其他未受干预的对照门店数据作为协变量),可以根据数据规律添加季节性参数保证模型拟合效果:
pre_period = ['2020-12-01', '2021-01-17'] post_period = ['2021-01-18', '2021-03-31'] ci = CausalImpact(df['Sales'], pre_period, post_period, model_args={'nseasons': 7}) ci.summary()
- 如果按上述操作仍然报错,可卸载当前版本的causalimpact包后重装官方稳定版:
pip uninstall causalimpact -y && pip install causalimpact
内容的提问来源于stack exchange,提问作者June Smith
相关产品推荐
相关产品推荐

