In NumPy, how to explain the return of ndarray [True]?

In NumPy, I know that we can index ndarray by logical values ​​as follows in the Python interpreter:

 >>> import numpy as np >>> b = np.arange(1, 6) >>> print(b) [1 2 3 4 5] >>> bi = np.array([True, False, True, False, True]) >>> print(b[bi]) [1 3 5] 

Then I tried b[True] , I could not understand what it means to return. Can anyone explain this to me? Thanks.

 >>> print(b) [1 2 3 4 5] >>> b[True] array([[1, 2, 3, 4, 5]]) >>> b[True] array([[1, 2, 3, 4, 5]]) 

EDIT

With this function, can I use it to get a vector from an array of rank one? Similar:

 >>> b = np.arange(1, 6) >>> print(b) [1 2 3 4 5] >>> row_vec = b[True] >>> print(row_vec) [[1 2 3 4 5]] >>> col_vec = b[True].T >>> print(col_vec) [[1] [2] [3] [4] [5]] 

I have to say that this is hard to understand, but is it better to do this than the reshape method?

+5
source share

Source: https://habr.com/ru/post/1272414/


All Articles