将numpy分类数据映射到numpy向量

时间:2018-08-17 14:46:53

标签: python numpy

我有一个类似的numpy数组:

my_arr = array([[0., 0., 0., 0., 1., 0.],
   [0., 1., 0., 0., 0., 0.],
   [0., 0., 0., 1., 0., 0.],
   [0., 0., 0., 0., 1., 0.],
   [1., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 1., 0.],
   [0., 1., 0., 0., 0., 0.],
   [0., 0., 0., 0., 1., 0.],
   ...
   ...]

我想返回一个向量,该向量将为my_arr的每个向量包含值为1的条目索引。我该怎么办?

3 个答案:

答案 0 :(得分:4)

为此您使用np.argmax()

 $post_title = get_the_title();
 $post_title_output = explode( " ", $post_title );
 array_splice( $post_title_output, -2 );
 echo implode( " ", $post_title_output );

答案 1 :(得分:1)

np.where(my_arr)[1]

查看文档:{​​{3}}

答案 2 :(得分:1)

您可以使用np.argwhere返回坐标数组:

arr = np.random.randint(0, 2, (5, 5))

print(arr)

[[0 0 1 1 1]
 [0 1 0 1 1]
 [1 1 0 0 1]
 [1 1 1 0 0]
 [1 1 1 1 0]]

res = np.argwhere(arr)

print(res)

array([[0, 2], [0, 3], ..., [4, 2], [4, 3]], dtype=int64)