使用NeuralProphet绘制置信区间时出现ValueError错误
解决NeuralProphet分位数回归绘图报错问题
问题场景
使用NeuralProphet 0.6.2版本进行分位数回归预测,运行绘图代码时触发如下错误:
ValueError: Please specify step_number using the highlight_nth_step_ahead_of_each_forecast function for quantiles plotting when auto-regression enabled.
代码中设置了n_lags=30(开启自回归)、quantiles=quantile_list(多分位数),调用plot时仅指定了forecast_in_focus=30,未满足分位数绘图的参数要求。
错误原因
当模型开启自回归(n_lags>0)且配置多个分位数时,NeuralProphet需要明确知道要绘制哪一步预测的分位数区间,否则无法确定展示的分位数对应哪个预测时间点。
解决方法
在绘图前调用highlight_nth_step_ahead_of_each_forecast函数,指定要聚焦的预测步长(和forecast_in_focus保持一致即可)。
修改后的完整代码
confidence_lv = 0.9 quantile_list = [round(((1 - confidence_lv) / 2), 2), round((confidence_lv + (1 - confidence_lv) / 2), 2)] m = NeuralProphet( yearly_seasonality=True, weekly_seasonality=True, daily_seasonality=False, quantiles=quantile_list, n_lags=30, epochs=10, n_forecasts=30, ) m.set_plotting_backend('plotly') metrics = m.fit(df) # 添加该行,指定要高亮的预测步长 m.highlight_nth_step_ahead_of_each_forecast(step_number=30) df_future = m.make_future_dataframe( df, n_historic_predictions=True, periods=30, ) forecast = m.predict(df_future) m.plot(forecast, forecast_in_focus=30)
内容的提问来源于stack exchange,提问作者zzzbbx
相关产品推荐
相关产品推荐

