【问题标题】:How to get prediction interval for column of predictions如何获取预测列的预测区间
【发布时间】:2020-05-10 05:48:50
【问题描述】:

如何为我的预测同时获得较低和较高的 95% 置信度或预测区间列?

df1 = pd.DataFrame({
        'cumsum_days': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
        'prediction': [800, 900, 1200, 700, 600, 
                 550, 500, 650, 625, 600,
                550, 525, 500, 400, 350]})

所需的数据框如下所示:

prediction  lower_ci   high_ci
800         some_num   some num
900         some_num   some num
1200        some_num   some num
700         some_num   some num

这些函数只给我个位数,但我正在寻找 df.prediction 的 95% 置信区间(一个 15 个数据点)。

mean = df.prediction.mean()
std = df.prediction.std()

我也试过这个(如下),但是它只给了我三个值,而不是我的预测值的 2 个额外的置信带/区间数组:

import numpy as np
import scipy.stats


def mean_confidence_interval(data, confidence=0.95):
    a = 1.0 * np.array(data)
    n = len(a)
    m, se = np.mean(a), scipy.stats.sem(a)
    h = se * scipy.stats.t.ppf((1 + confidence) / 2., n-1)
    return m, m-h, m+h

【问题讨论】:

  • 这对您有帮助吗:stackoverflow.com/questions/15033511/…?
  • 这只给了我三个值,而不是我预测值的 2 个置信区间数组。
  • Pandas 的 plot 有一个 yerr 参数。试试:df1.set_index('cumsum_days').pipe(lambda d: d.plot(yerr={**d.std()}))
  • 然后你需要回到预测结果并从那里得到标准误差和置信区间。因为在这一点上这是不可能的。
  • @Starbucks 从训练集中获取残差并计算标准差,然后使用标准正态分位数得到 CI (1.96 * std for 95%)

标签: python pandas scipy confidence-interval


【解决方案1】:

这样的事情怎么样?

bins = [0, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, 5, 5.25, 5.5, 5.75, 6, 6.25, 6.5, 6.75, 7, 7.25, 7.5, 7.75, 8, 8.25, 8.5, 8.75, 9, 9.25, 9.5, 9.75, 10, np.inf]
labels = ['0', '1', '1.25', '1.5', '1.75', '2', '2.25', '2.5', '2.75', '3', '3.25', '3.5', '3.75', '4', '4.25', '4.5', '4.75', '5', '5.25', '5.5', '5.75', '6', '6.25', '6.5', '6.75', '7', '7.25', '7.5', '7.75', '8', '8.25', '8.5', '8.75', '9', '9.25', '9.5', '9.75', '10']

dataset['RatingScore'] = pd.cut(dataset['Rating'], bins=bins, labels=labels, right=True)

您可以创建基本设置,然后将最终对象转换为数据框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2015-04-30
    相关资源
    最近更新 更多