【问题标题】:numpy maximum element axis = 1 gives Deprecation warningnumpy 最大元素轴 = 1 给出弃用警告
【发布时间】:2020-02-26 20:52:10
【问题描述】:

我正在使用 Python 3.7 和 numpy 1.18.1,并且我有一个 3 x 2 numpy 数组,如下所示:

y = np.array([
    [10, 2],
    [13, 15],
    [19, 1]
    ])

现在,要跨列查找最大值-

np.amax(y, axis = 0)
# array([19, 15])

# Find indices of maximum elements across columns-
np.where(y == np.amax(y, axis = 0))

并找到跨行的最大值-

np.amax(y, axis = 1)
# array([10, 15, 19])

但是,当我尝试跨列查找最大值索引时-

np.where(y == np.amax(y, axis = 1))

它给了我以下警告:

:1: DeprecationWarning: elementwise 比较失败;这将在未来引发错误。
np.where(y == np.amax(y, axis = 1))

为什么会这样?

谢谢!

【问题讨论】:

  • 这很有趣,我想由于广播规则,它适用于axis=1 而不适用于axis=0。显然我错了。

标签: python-3.x numpy


【解决方案1】:

我认为这是关于这里的不均匀形状 - np.amax 现在返回 ndarray 形状为 1,x 当您采用 axis=1 时,您有一个大小为 3,2 的数组,您尝试比较它针对大小为1,3 的单列 - 它不适合。

重塑以使其发挥作用:

np.where(y == np.amax(y, axis = 1).reshape(-1,1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多