首页 > 解决方案 > TypeError: (typecode 'l') 根据转换规则 ''same_kind'' 同时使用互相关

问题描述

我正在尝试使用互相关。我正在调查 x 和 y 之间的滞后是 1 个时间间隔。

我有这样的代码:

x= ([1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1])
y= ([0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1]) 
fig, [ax1, ax2] = plt.subplots(2, 1, sharex=True)
ax1.xcorr(x, y, usevlines=True, maxlags=50, normed=True, lw=2)
ax1.grid(True)

ax1.acorr(x, usevlines=True, normed=True, maxlags=50, lw=2)
ax1.grid(True)

ax2.acorr(y, usevlines=True, normed=True, maxlags=50, lw=2)
ax2.grid(True)

plt.show()

但是当我运行代码时,它给了我下图中给出的这个错误。我有点卡在这里。

https://i.stack.imgur.com/uV2F4.png

有任何想法吗?

标签: pythondataframestatisticsdata-sciencecorrelation

解决方案


您应该将输入转换为类型为 np.float 的 numpy 数组,如下所示:

x= np.array([1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1], dtype=np.float) y= np.array([0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1], dtype=np.float)

另外您maxlags的太大(请参阅上述修复后出现的下一个错误)


推荐阅读