我需要对一组数字进行自相关,据我所知,它只是集合与自身的相关性.
我已经尝试使用numpy的相关函数,但我不相信结果,因为它几乎总是给出一个向量,其中第一个数字不是最大的,因为它应该是.
所以,这个问题实际上是两个问题:
numpy.correlate做什么?我有一个二维数组,即一个也是数组的序列数组.对于每个序列,我想计算自相关,因此对于(5,4)数组,我将获得5个结果,或维数(5,7).
我知道我可以绕过第一个维度,但这很慢,我的最后一招.还有另外一种方法吗?
谢谢!
编辑:
根据所选答案加上mtrw的评论,我有以下功能:
def xcorr(x):
"""FFT based autocorrelation function, which is faster than numpy.correlate"""
# x is supposed to be an array of sequences, of shape (totalelements, length)
fftx = fft(x, n=(length*2-1), axis=1)
ret = ifft(fftx * np.conjugate(fftx), axis=1)
ret = fftshift(ret, axes=1)
return ret
Run Code Online (Sandbox Code Playgroud)
请注意,length是我的代码中的全局变量,因此请务必声明它.我也没有将结果限制为实数,因为我也需要考虑复数.
我遵循了另一篇文章中定义自相关函数的建议:
def autocorr(x):
result = np.correlate(x, x, mode = 'full')
maxcorr = np.argmax(result)
#print 'maximum = ', result[maxcorr]
result = result / result[maxcorr] # <=== normalization
return result[result.size/2:]
Run Code Online (Sandbox Code Playgroud)
但最大值不是"1.0".因此我引入了标有"<=== normalization"的行
我尝试了使用"时间序列分析"(Box - Jenkins)第2章数据集的函数.我希望得到像图的结果.那本书中的2.7.但是我得到了以下内容:

任何人都有这种奇怪的不期望的自相关行为的解释?
增加(2012-09-07):
我进入Python - 编程并执行以下操作:
from ClimateUtilities import *
import phys
#
# the above imports are from R.T.Pierrehumbert's book "principles of planetary
# climate"
# and the homepage of that book at "cambridge University press" ... they mostly
# define the
# class "Curve()" used in …Run Code Online (Sandbox Code Playgroud) 在 python (+ pandas/numpy/scipy/statsmodels) 中,是否有一个函数可以返回滞后的自相关性?有没有像这样的库函数现成的?
为了避免混淆,我想要以下内容,只是我不想绘制它,但我希望它作为系列返回(pd.Series 或 pd.DataFrame):
import numpy as np
import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf
from matplotlib import pyplot as plt
plt.ion()
s = pd.Series(np.sin(range(1,100))) + pd.Series(np.random.randn(99))
plot_acf(s)
Run Code Online (Sandbox Code Playgroud)
实际上,我想要pd.Series.autocorr()返回什么,但我想要返回一个序列而不是标量,其中该序列包含各种滞后的自相关。
编辑:
实现上述目标的一种方法是:
pd.Series([s.autocorr(i) for i in range(0,s.shape[0]-1)], index=range(0,s.shape[0]-1))
Run Code Online (Sandbox Code Playgroud) 假设我们有一个称为"系列"的时间序列对象.我知道它非常容易使用autocorrelation_plot()方法来绘制系列对象的Lag和Autocorrelation维度.
这是代码:
from matplotlib import pyplot
from pandas.tools.plotting import autocorrelation_plot
autocorrelation_plot(series)
pyplot.show()
Run Code Online (Sandbox Code Playgroud)
这是大熊猫的情节:
有没有办法使用散景服务器获得相同的情节?
python ×5
numpy ×3
pandas ×2
statistics ×2
bokeh ×1
correlation ×1
math ×1
statsmodels ×1
time-series ×1