numpy中的np.where使用的小细节
import numpy as np
label = np.array([[0,1,0,1,1],[1,1,0,1,1],[0,0,0,1,1]])
pos_index = np.where(label[0] == 1)
运行上面代码,返回的pos_index是一个包含array数组的元组,如下图所示:

要先获得numpy类型数组,必须在pos_index后加索引:
import numpy as np
label = np.array([[0,1,0,1,1],[1,1,0,1,1],[0,0,0,1,1]])
pos_index = np.where(label[0] == 1)[0]
运行后结果如下:

这篇博客探讨了在numpy库中使用np.where函数获取数组元素条件索引的细节。通过示例代码展示了如何从二维数组中提取满足特定条件的元素索引,并解释了返回结果的结构。重点在于如何正确访问返回的元组以获取实际的numpy数组索引。


被折叠的 条评论
为什么被折叠?



