2016-07-21 5 views
6

Der CodeSet Anzahl von Verzögerungen in Python Pandas autocorrelation_plot

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() 

Plots die Autokorrelationen von xx, aber es zeichnet alle 10000 Lags. Wie zeichne ich nur die ersten 10?

Die Funktion autocorrelation_plot beginnt wie folgt:

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 

Gibt es eine Möglichkeit die Anzahl der Lags zu setzen aufgetragen das Argument ** kwds mit?

Antwort

0

Die Funktion autocorrelation_plot ist eine Funktion auf hoher Ebene. Anzeigen des Codes aus der Pandas-Bibliothek:

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 

Es gibt eine Registerkarte in der gesamten Zeile in der Funktion fehlt.

zum Hinzufügen von Kopf:

from pandas.compat import lmap 

in der 4. Zeile vor dem Ende Änderung ax.plot (x, y, ** kwds) ax.plot (x [10], y [: 10], ** kwds)

ich habe hinzugefügt, eine N_SAMPLES Variablen:

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