Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用布尔数组在numpy中为二维数组建立索引_Python_Arrays_Numpy - Fatal编程技术网

Python 使用布尔数组在numpy中为二维数组建立索引

Python 使用布尔数组在numpy中为二维数组建立索引,python,arrays,numpy,Python,Arrays,Numpy,我使用布尔索引从numpy数组中选择元素作为 x = y[t<tmax] 这里有一个例子 x1D = np.array([1,2,3], np.int32) x2D = np.array([[1,2,3],[1,2,3],[1,2,3]], np.int32) print(x1D[x1D<3]) --> [1 2] print(x2D[x1D<3][x1D<3]) --> error 但是我想要 [[1 2] [1 2]] 1d遮罩: In [29]:

我使用布尔索引从numpy数组中选择元素作为

x = y[t<tmax]
这里有一个例子

x1D = np.array([1,2,3], np.int32)
x2D = np.array([[1,2,3],[1,2,3],[1,2,3]], np.int32)
print(x1D[x1D<3]) --> [1 2]
print(x2D[x1D<3][x1D<3]) --> error
但是我想要

[[1 2]
 [1 2]]
1d遮罩:

In [29]: x1D<3                                                                                               
Out[29]: array([ True,  True, False])
应用于二维图形时,将选择两行:

In [31]: x2D[_29]                                                                                            
Out[31]: 
array([[1, 2, 3],
       [1, 2, 3]], dtype=int32)
它可以再次用于选择列-但请注意:行索引的占位符:

In [32]: _[:, _29]                                                                                           
Out[32]: 
array([[1, 2],
       [1, 2]], dtype=int32)
如果我们从该掩码生成索引数组,我们可以通过一个步骤进行索引:

In [37]: idx = np.nonzero(x1D<3)                                                                             
In [38]: idx                                                                                                 
Out[38]: (array([0, 1]),)
In [39]: x2D[idx[0][:,None], idx[0]]                                                                         
Out[39]: 
array([[1, 2],
       [1, 2]], dtype=int32)
ix_u是调整索引尺寸的方便工具:

In [42]: x2D[np.ix_(idx[0], idx[0])]                                                                         
Out[42]: 
array([[1, 2],
       [1, 2]], dtype=int32)
或将布尔掩码传递给ix_3;:

写在[32]中,以便接近您的尝试:

In [46]: x2D[x1D<3][:, x1D<3]                                                                                
Out[46]: 
array([[1, 2],
       [1, 2]], dtype=int32)

y[t这里是什么?还有一个2D数组?Python独立地处理每个索引操作[]。temp[t
In [30]: x1D[_]                                                                                              
Out[30]: array([1, 2], dtype=int32)
In [31]: x2D[_29]                                                                                            
Out[31]: 
array([[1, 2, 3],
       [1, 2, 3]], dtype=int32)
In [32]: _[:, _29]                                                                                           
Out[32]: 
array([[1, 2],
       [1, 2]], dtype=int32)
In [37]: idx = np.nonzero(x1D<3)                                                                             
In [38]: idx                                                                                                 
Out[38]: (array([0, 1]),)
In [39]: x2D[idx[0][:,None], idx[0]]                                                                         
Out[39]: 
array([[1, 2],
       [1, 2]], dtype=int32)
In [41]: x2D[ [[0],[1]], [[0,1]] ]                                                                           
Out[41]: 
array([[1, 2],
       [1, 2]], dtype=int32)
In [42]: x2D[np.ix_(idx[0], idx[0])]                                                                         
Out[42]: 
array([[1, 2],
       [1, 2]], dtype=int32)
In [44]: np.ix_(_29, _29)                                                                                    
Out[44]: 
(array([[0],
        [1]]), array([[0, 1]]))
In [45]: x2D[np.ix_(_29, _29)]                                                                               
Out[45]: 
array([[1, 2],
       [1, 2]], dtype=int32)
In [46]: x2D[x1D<3][:, x1D<3]                                                                                
Out[46]: 
array([[1, 2],
       [1, 2]], dtype=int32)