小编Joe*_*ese的帖子

为什么 2D NumPy 数组的布尔索引会生成 1d 数组?

我正在 NumPy 中尝试布尔索引,并发现了这一点,这让我感到困惑:

import numpy as np

np.random.seed(0)
Run Code Online (Sandbox Code Playgroud)

创建了一个 7 x 4 数组:

data = np.random.rand(7, 4) 

[[ 0.5488  0.7152  0.6028  0.5449]

 [ 0.4237  0.6459  0.4376  0.8918]

 [ 0.9637  0.3834  0.7917  0.5289]

 [ 0.568   0.9256  0.071   0.0871]

 [ 0.0202  0.8326  0.7782  0.87  ]

 [ 0.9786  0.7992  0.4615  0.7805]

 [ 0.1183  0.6399  0.1434  0.9447]]
Run Code Online (Sandbox Code Playgroud)

同样创建了一个 7 x 4 的布尔数组:

bool_array = 

         ([[True,False,False,True],
          [True,False,False,True],
          [True,False,False,True],
          [True,False,False,True],
          [True,False,False,True],
          [True,False,False,True],
          [True,False,False,True]])


bool_array = np.array(bool_array)

data[bool_array]
Run Code Online (Sandbox Code Playgroud)

输出:

[ 0.5488  0.5449  0.4237  0.8918  0.9637  0.5289  0.568 …
Run Code Online (Sandbox Code Playgroud)

python boolean numpy

5
推荐指数
1
解决办法
3491
查看次数

标签 统计

boolean ×1

numpy ×1

python ×1