Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python实现不同位移的自相关函数_Python_Python 3.x_Numpy - Fatal编程技术网

用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):
...