![](/img/trans.png)
[英]ValueError: x and y must have same first dimension, but have shapes
[英]ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,)
我想从列表中找到最大值和最小值,但在运行我的程序后,终端显示错误,如“ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,)”。 如何解决这个问题?
代码:
from scipy.signal import argrelextrema
data = np.array([-1, 0, 1, 2, 1, 0, -1, -2, -1, 0, 1, 2, 1 ])
plt.plot(data)
maximums = argrelextrema(data, np.greater)
plt.plot(maximums, data[maximums], "x")
minimums = np.array(argrelextrema(data, np.less))
plt.plot(minimums, data[minimums], ".")
plt.show()
正如您在文档中看到的, maxima
和minima
不是一维 arrays 而是 ndarrays 元组
import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
data = np.array([-1, 0, 1, 2, 1, 0, -1, -2, -1, 0, 1, 2, 1 ])
maxima = argrelextrema(data, np.greater)
minima = argrelextrema(data, np.less)
让我们检查
print(maxima)
给
(array([ 3, 11]),)
和
print(minima)
给
(array([7]),)
由于您只需要元组的第一个元素,您可以这样做
plt.plot(data)
plt.plot(maxima[0], data[maxima[0]], "x", ms=10)
plt.plot(minima[0], data[minima[0]], ".", ms=10)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.