![](/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.