【问题标题】:Calculating the argmax from one array and using to get values from another从一个数组计算 argmax 并用于从另一个数组中获取值
【发布时间】:2022-06-28 23:38:08
【问题描述】:

我正在尝试从一个 ndarray 获取 argmax 并使用它从另一个 ndarray 获取值,但我做错了。

ndvi_array = np.random.randint(0, 255, size=(4, 1, 100, 100))
image_array = np.random.randint(0, 255, size=(4, 12, 100, 100))
ndvi_argmax = ndvi_array.argmax(0)
print(f"NDVI argmax shape: {ndvi_argmax.shape}")
zipped = tuple(zip(range(len(ndvi_argmax)), ndvi_argmax))
result = image_array[zipped]
print(f"Result share: {result.shape}")

我收到以下错误:

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

如何获得具有最大值的形状数组 (1,12,100,100)?

【问题讨论】:

    标签: python numpy argmax


    【解决方案1】:
    >>> result = np.take_along_axis(
    ...     image_array,
    ...     ndvi_array.argmax(axis=0, keepdims=True),
    ...     axis=0,
        )
    
    >>> print(f"{result.shape = }")
    result.shape = (1, 12, 100, 100)
    

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 1970-01-01
      • 2022-07-05
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多