Set the number of delays in Python pandas autocorrelation_plot

The code

import numpy as np from pandas.tools.plotting import autocorrelation_plot import matplotlib.pyplot as plt nobs = 10000 xx = np.random.normal(size=nobs) autocorrelation_plot(xx) plt.show() 

displays xx autocorrelation, but it displays all 10,000 lags. How to create only the first 10?

The autocorrelation_plot function runs as follows:

 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 

Is there a way to set the number of delays built using the ** kwds argument?

+10
source share
2 answers

The autocorrelation_plot function is a high-level function. View code from pandas library:

 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 

This tab is missing a tab.

Adding to the header:

 from pandas.compat import lmap 

In the 4th line to the end, change ax.plot (x, y, ** kwds) to ax.plot (x [: 10], y [: 10], ** kwds)

I added n_samples variables:

 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 
+2
source

Just like a backup solution if you don't need to use pandas methods. There is a statsmodels function plot_acf in which you can set the lags argument.

 from statsmodels.graphics.tsaplots import plot_acf import pandas as pd d = dict() d['value'] = [11, 22, 34, 22, 43, 23, 45, 32, 56, 40, 44, 33, 22, 56, 44] df = pd.DataFrame.from_dict(d) plot_acf(df, lags = 5) 
0
source

All Articles