简体   繁体   中英

TypeError: (typecode 'l') according to the casting rule ''same_kind'' while using cross-correlation

I am trying to use cross-correlation. The lag I am investigating between x and y is 1 time interval.

I have a code like this:

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()

But when i run the code, it gave me this error given in the picture below. And I am kinda stuck here.

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

Any ideas?

You should convert your inputs into numpy array with type np.float like this:

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)

In addition your maxlags is too big (see the next error you'll get after the above fix)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM