python - seaborn 是否计算预测(不是置信度)带?

标签 python seaborn

据我了解,seaborn 回归总是伴随着置信带(下图中的虚线带)。我希望看到一个在seaborn 中计算预测带的函数(图中的虚线带)。有这样的选择吗?

confidence and prediction bands

最佳答案

下面是一个示例代码来解释它:

import numpy as np 
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline 

x = np.arange(100)
y = np.arange(100)*3.2 + np.random.rand(100)*100 

rp = sns.regression._RegressionPlotter(x,y)
grid, yhat, err_bands = rp.fit_regression(x_range=(-10,110))
plt.plot(grid,yhat)
plt.scatter(x,y)
plt.fill_between(grid,err_bands[0,:],err_bands[1,:],alpha=.5)

enter image description here

此处 fit_regression 方法计算误差和预测。如果您使用默认关键字参数(取自源代码),这里是计算预测的函数。

def fit_fast(self, grid):
    """Low-level regression and prediction using linear algebra."""
    def reg_func(_x, _y):
        return np.linalg.pinv(_x).dot(_y)

    X, y = np.c_[np.ones(len(self.x)), self.x], self.y
    grid = np.c_[np.ones(len(grid)), grid]
    yhat = grid.dot(reg_func(X, y))
    if self.ci is None:
        return yhat, None

    beta_boots = algo.bootstrap(X, y, func=reg_func,
                                n_boot=self.n_boot, units=self.units).T
    yhat_boots = grid.dot(beta_boots).T
    return yhat, yhat_boots

这是获取用于计算置信区间的置信度的引导函数(也来自源代码):

def bootstrap(*args, **kwargs):

    # Ensure list of arrays are same length
    if len(np.unique(list(map(len, args)))) > 1:
        raise ValueError("All input arrays must have the same length")
    n = len(args[0])

    # Default keyword arguments
    n_boot = kwargs.get("n_boot", 10000)
    func = kwargs.get("func", np.mean)
    axis = kwargs.get("axis", None)
    units = kwargs.get("units", None)
    smooth = kwargs.get("smooth", False)
    random_seed = kwargs.get("random_seed", None)
    if axis is None:
        func_kwargs = dict()
    else:
        func_kwargs = dict(axis=axis)

    # Initialize the resampler
    rs = np.random.RandomState(random_seed)

    # Coerce to arrays
    args = list(map(np.asarray, args))
    if units is not None:
        units = np.asarray(units)

    # Do the bootstrap
    if smooth:
        return _smooth_bootstrap(args, n_boot, func, func_kwargs)

    if units is not None:
        return _structured_bootstrap(args, n_boot, units, func,
                                     func_kwargs, rs)

    boot_dist = []
    for i in range(int(n_boot)):
        resampler = rs.randint(0, n, n)
        sample = [a.take(resampler, axis=0) for a in args]
        boot_dist.append(func(*sample, **func_kwargs))
    return np.array(boot_dist)

关于python - seaborn 是否计算预测(不是置信度)带?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48764205/

相关文章:

python-3.x - 根据另一个数据帧的条件对 sns 条形图进行着色

python - 在matplotlib中使用数据坐标时如何使y轴文本出现在图中?

python - 如何从 Pandas MultiIndex 制作 Seaborn 线图?

python - 如何在 Seaborn lmplot 中使用 pandas DataFrame 作为数据参数

javascript - 使用 python 重新创建 CryptoJS Hmac

python - mysql 迁移遇到问题(不能将序列乘以 'tuple' 类型的非整数)

Python的sqlite3的executemany不接受元组列表

python - 在 Python 中模拟修补 from/import 语句

python - Django:{{ request.user }} 在模板中没有显示任何内容

python - 使用 pandas/matplotlib 使用 for 循环创建条形图