python - numpy 数组上的 2D argmax

标签 python arrays numpy

从 numpy 数组开始 v:

v = \
np.array([[0, 0, 0, 0, 0],
          [0, 0, 1, 0, 0],
          [1, 0, 0, 0, 0],
          [0, 1, 0, 0, 0],
          [0, 0, 1, 0, 1],
          [0, 0, 0, 0, 1],
          [0, 0, 0, 1, 1]])

我想找到每列中的第一个非零值,并将这些值乘以 100。

我想要的结果是:

array([[  0,   0,   0,   0,   0],
       [  0,   0, 100,   0,   0],
       [100,   0,   0,   0,   0],
       [  0, 100,   0,   0,   0],
       [  0,   0,   1,   0, 100],
       [  0,   0,   0,   0,   1],
       [  0,   0,   0, 100,   1]])

我想通过沿每个轴获取 argmax 来解决这个问题:

i = v.argmax(0)
j = v.argmax(1)
v[i, j] *= 100  

我知道我没有正确使用 ij,那么如何解决这个问题?

最佳答案

您只想对列进行范围

v[v.argmax(axis=0), np.arange(v.shape[1])] *= 100

关于python - numpy 数组上的 2D argmax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47279261/

相关文章:

python - Python中的顺序模式匹配算法

python - 来自嵌套单词列表的共现矩阵

arrays - 我的3D数组长度为2,但在运行时访问索引2紧急情况

python - scipy PchipInterpolator 错误 : 'array cannot be safely cast to required type'

python - 此消息总是指索引问题?索引错误 : index out of bounds

python - django channels message.reply_channel 没有属性发送

python - 为什么 Python 在函数中增加变量时会提示赋值前的引用?

arrays - Mongodb - 更新 JSON 数组中的多个数组元素

c - 为什么 sizeof 运算符给出不同的输出

python - 数组的连续、重叠子集(NumPy、Python)