简体   繁体   English

是否可以在 sklearn 中计算线性和多项式回归模型的置信区间?

[英]Is it possible to calculate confidence intervals for linear and polynomial regression models in sklearn?

I am doing some basic forecasting using the sklearn linear and polynomial regression models.我正在使用 sklearn 线性和多项式回归模型进行一些基本预测。 I am able to get my model up and running pretty quickly to make predictions, but I also would like to calculate the 80% confidence intervals to get the upper and lower bounds along with the predictions.我能够让我的模型快速启动并运行以进行预测,但我也想计算 80% 的置信区间以获得预测的上限和下限。

Is there an easy way to calculate those numbers using inbuilt sklearn features?有没有一种简单的方法可以使用内置的 sklearn 功能来计算这些数字?

here is what my data df looks like:这是我的数据df的样子:

+------------+------+
|    date    | cost |
+------------+------+
| 01/01/2022 |  100 |
| 02/01/2022 |  104 |
| 03/01/2022 |  107 |
| 04/01/2022 |  108 |
| 05/01/2022 |  111 |
| 06/01/2022 |  117 |
| 07/01/2022 |  120 |
| 08/01/2022 |  122 |
| 09/01/2022 |  128 |
| 10/01/2022 |  133 |
+------------+------+

For both models and for the next 10 days for example, I want to calculate yhat , yhat_lower , and yhat_upper , but I am not sure on the best way to do this.例如,对于这两个模型和接下来的 10 天,我想计算yhatyhat_loweryhat_upper ,但我不确定最好的方法。 Any help would be appreciated!任何帮助,将不胜感激!

import numpy as np
import matplotlib.pyplot as plt

# confidence level
confidence = .2

# your predictions
y_pred = np.array([100,104,107,108,111,117,120,122,128,133])

y_l, y_h = y_pred * (1 - confidence), y_pred * (1 + confidence)

fig, ax = plt.subplots()
ax.plot(range(len(y_pred)), y_pred, color="green")
ax.fill_between(range(len(y_pred)), y_l, y_h, color='b', alpha=.1)
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM