我有一个时间序列数据,我想显示自相关函数。(我们知道正弦函数的自相关是余弦函数)
我应用了几种方法来做到这一点如下
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
x = np.arange(0,30,0.1) #interval=0.1, 300 samples
y = np.sin(x)
y_cor = np.correlate(y,y,'full')
lags = np.arange(-x[-1],x[-1]+0.1,0.1)
#sin(x)
plt.figure()
plt.plot(x,y)
#autocorrelation(numpy)
plt.figure()
plt.plot(lags,y_cor)
plt.xlabel('Lag')
plt.ylabel('autocorrelation')
#matplotlib
plt.figure()
plt.acorr(y,maxlags=y.size-1)
#statsmodels
plt.figure()
plot_acf(y,lags=y.size-1)
plt.show()
Run Code Online (Sandbox Code Playgroud)
然而,结果是衰减余弦函数,而不是纯 cos(x)。我看到一些答案说这是因为计算自相关时包在 x 区域之外填充了零,但是如何解决这个问题以获得纯 cos(x) ?

我有两个数组,我希望第一个数组的所有元素除以第二个数组。例如,
In [24]: a = np.array([1,2,3])
In [25]: b = np.array([1,2,3])
In [26]: a/b
Out[26]: array([1., 1., 1.])
In [27]: 1/b
Out[27]: array([1. , 0.5 , 0.33333333])
Run Code Online (Sandbox Code Playgroud)
这不是我想要的答案,我想要的输出是这样的(我们可以看到 a 的所有元素都除以 b)
In [28]: c = []
In [29]: for i in a:
...: c.append(i/b)
...:
In [30]: c
Out[30]:
[array([1. , 0.5 , 0.33333333]),
array([2. , 1. , 0.66666667]),
In [34]: np.array(c)
Out[34]:
array([[1. , 0.5 , 0.33333333],
[2. , 1. , 0.66666667],
[3. , 1.5 , 1. ]])
Run Code Online (Sandbox Code Playgroud)
但我不喜欢 …