python用pandas投票_python - 设置Python pandas autocorrelation_plot中的滞后数 - 堆栈内存溢出...

该博客讨论了如何修改Python pandas库的autocorrelation_plot函数,以限制显示的滞后数。通过添加n_samples参数,允许用户自定义显示的样本数量,从而更好地控制图表的详细程度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

autocorrelation_plot函数是一个高级函数。 查看pandas库中的代码:

def autocorrelation_plot(series, ax=None, **kwds):

"""Autocorrelation plot for time series.

Parameters:

-----------

series: Time series

ax: Matplotlib axis object, optional

kwds : keywords

Options to pass to matplotlib plotting method

Returns:

-----------

ax: Matplotlib axis object

"""

import matplotlib.pyplot as plt

n = len(series)

data = np.asarray(series)

if ax is None:

ax = plt.gca(xlim=(1, n), ylim=(-1.0, 1.0))

mean = np.mean(data)

c0 = np.sum((data - mean) ** 2) / float(n)

def r(h):

return ((data[:n - h] - mean) *

(data[h:] - mean)).sum() / float(n) / c0

x = np.arange(n) + 1

y = lmap(r, x)

z95 = 1.959963984540054

z99 = 2.5758293035489004

ax.axhline(y=z99 / np.sqrt(n), linestyle='--', color='grey')

ax.axhline(y=z95 / np.sqrt(n), color='grey')

ax.axhline(y=0.0, color='black')

ax.axhline(y=-z95 / np.sqrt(n), color='grey')

ax.axhline(y=-z99 / np.sqrt(n), linestyle='--', color='grey')

ax.set_xlabel("Lag")

ax.set_ylabel("Autocorrelation")

ax.plot(x, y, **kwds)

if 'label' in kwds:

ax.legend()

ax.grid()

return ax

函数中的所有行都缺少一个选项卡。

添加到标题:

from pandas.compat import lmap

在结束前的第4行将ax.plot(x,y,** kwds)更改为ax.plot(x [:10],y [:10],** kwds)

我添加了一个n_samples变量:

from pandas.compat import lmap

def autocorrelation_plot(series, n_samples=None, ax=None, **kwds):

"""Autocorrelation plot for time series.

Parameters:

-----------

series: Time series

ax: Matplotlib axis object, optional

kwds : keywords

Options to pass to matplotlib plotting method

Returns:

-----------

ax: Matplotlib axis object

"""

import matplotlib.pyplot as plt

n = len(series)

data = np.asarray(series)

if ax is None:

ax = plt.gca(xlim=(1, n_samples), ylim=(-1.0, 1.0))

mean = np.mean(data)

c0 = np.sum((data - mean) ** 2) / float(n)

def r(h):

return ((data[:n - h] - mean) *

(data[h:] - mean)).sum() / float(n) / c0

x = (np.arange(n) + 1).astype(int)

y = lmap(r, x)

z95 = 1.959963984540054

z99 = 2.5758293035489004

ax.axhline(y=z99 / np.sqrt(n), linestyle='--', color='grey')

ax.axhline(y=z95 / np.sqrt(n), color='grey')

ax.axhline(y=0.0, color='black')

ax.axhline(y=-z95 / np.sqrt(n), color='grey')

ax.axhline(y=-z99 / np.sqrt(n), linestyle='--', color='grey')

ax.set_xlabel("Lag")

ax.set_ylabel("Autocorrelation")

if n_samples:

ax.plot(x[:n_samples], y[:n_samples], **kwds)

else:

ax.plot(x, y, **kwds)

if 'label' in kwds:

ax.legend()

ax.grid()

return ax

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值