用Python实现不同位移的自相关函数
你好,我想得到不同班次的自相关列表。我的代码如下:用Python实现不同位移的自相关函数,python,python-3.x,numpy,Python,Python 3.x,Numpy,你好,我想得到不同班次的自相关列表。我的代码如下: thetas = np.array([0.24233997, 0.33467202, 0.35020237, ...]) autocorrelation = [] for shift in range(1,21): correlation = np.corrcoef(thetas[:-shift], thetas[shift:])[0,1] autocorrelation.append(correlation) 这给了我一个
thetas = np.array([0.24233997, 0.33467202, 0.35020237, ...])
autocorrelation = []
for shift in range(1,21):
correlation = np.corrcoef(thetas[:-shift], thetas[shift:])[0,1]
autocorrelation.append(correlation)
这给了我一个错误:
C:\Users\PC\Anaconda3\lib\site-packages\numpy\lib\function_base.py:3175: RuntimeWarning: Degrees of freedom <= 0 for slice
c = cov(x, y, rowvar)
C:\Users\PC\Anaconda3\lib\site-packages\numpy\lib\function_base.py:3109: RuntimeWarning: divide by zero encountered in double_scalars
c *= 1. / np.float64(fact)
C:\Users\PC\Anaconda3\lib\site-packages\numpy\lib\function_base.py:3109: RuntimeWarning: invalid value encountered in multiply
c *= 1. / np.float64(fact)
C:\Users\PC\Anaconda3\lib\site-packages\numpy\lib\function_base.py:1128: RuntimeWarning: Mean of empty slice.
avg = a.mean(axis)
C:\Users\PC\Anaconda3\lib\site-packages\numpy\core\_methods.py:73: RuntimeWarning: invalid value encountered in true_divide
ret, rcount, out=ret, casting='unsafe', subok=False)
是否有人知道如何修复此代码或以其他方式创建多个不同班次的自相关列表?发生此错误的原因是python在超出数组范围的索引处获取了一个切片,因此返回了一个空切片,而np.corrcoef会在该切片上返回错误 i、 θ的元素少于22个 这将修复错误:
...
for shift in range(1,thetas.size-1):
...