gpt4 book ai didi

python - 如何根据索引在多维数组中查找值

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:29 24 4
gpt4 key购买 nike

我有两个相同大小的多维数组。为简单起见,我现在使用随机值来制作它们,但它们是相关的。我需要从 X 数组中找到每行的最大值。从 Y 数组中,我需要具有相应索引的值作为 X 数组中的最大值。

import numpy as np

X_splitted = np.random.random_sample([517,56])
Y_splitted = np.random.random_sample([517,56])

rows = len(Y_splitted[0])
colums = len(Y_splitted)

X_max = np.zeros(colums)
index = np.zeros(colums)
Y_corr = np.zeros(colums)

for i in range(colums):
X_max[i] = max(X_splitted[i])
index[i] = (np.asarray(X_splitted[i].argmax()))
index = index.astype(int)

我已经成功找到了 X 数组的最大值及其相应的索引。但是,我无法将 Y 数组值与这些索引进行匹配。

最佳答案

您的代码已接近!只是需要一些调整。

获取X数组最大值的索引

index[i] = X_splitted[i].argmax()

从 Y 数组中获取相同的索引

y_val[i] = Y_splitted[i][index[i]]

关于python - 如何根据索引在多维数组中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55512041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com