python - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (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
不是一维数组,而是 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()
推荐阅读
- java - 根据情况使用 CascadeType.MERGE 和 CascadeType.PERSIST
- node.js - 使用 Nightwatch.js 上传动作
- python - Bitshift Int Color 回到 RGB
- csv - Spark:写入时避免任务重启
- python - 在python中设计一个猜数字列表的游戏时遇到麻烦
- visual-studio - Visual Studio 选项卡作为单独的窗口自行分离
- android - 如何从可序列化的对象中获取值
- mysql - sql server 中的@@error ,Mysql server 中的替代品
- python-3.x - 如何组装使用附加指令解析 reStructuredText 的工具
- sql - SQL查询:如何将整数更改为布尔值