Use of where ()
First of all, the Where () function returns only different for different inputs.
1 when an array is a one-dimensional array, the returned value is a one-dimensional index, so there is only one set of indexed arrays
2 When an array is a two-dimensional array, the array value that satisfies the condition returns the position index of the value, so there will be two sets of indexed arrays to represent the position of the value
For example
1>>>b=np.arange (10)2>>>b3Array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])4>>>np.where (b>5)5(Array ([6, 7, 8, 9], dtype=Int64),)6 7>>>a=np.reshape (Np.arange (20), (4,5))8>>>a9Array ([[[0, 1, 2, 3, 4],Ten[5, 6, 7, 8, 9], One[10, 11, 12, 13, 14], A[15, 16, 17, 18, 19]]) ->>>np.where (a>10) -(Array ([2, 2, 2, 2, 3, 3, 3, 3, 3], dtype=Int64), theArray ([1, 2, 3, 4, 0, 1, 2, 3, 4], Dtype=int64))
An introduction to the explanations in the NumPy standard library:
NumPy. where (condition[, x, y])
Based on conditional condition, the return value is from X or Y.
If.
| Parameters: |
condition : array, bool value
When True, yield x, otherwise yield y.
x, y : array_like, optional
X is the same shape as Y, and when the value in condition is true returns the value of the x corresponding position, False is the return y
|
| return value: |
out: Ndarray or tuple of ndarrays
① If the parameters have Condition,x and Y, the shape of their three parameters is the same. Then, when the value in condition is true, the value of the x corresponding position is returned, false is the return Y.
② If the argument is only condition, the return value is the positional index where the element value is true in condition, the tangent is returned as a tuple, the element of the tuple is the Ndarray array, and the index of the position is represented
|
>>>Np.where (["True, False], [true, true]],... [[1, 2], [3, 4]],... [[9, 8], [7, 6]]) array ([[[1, 8], [3, 4]])>>>>>> Np.where ([[0, 1], [1, 0]]) (Array ([0,1]), Array ([1, 0]))>>>>>> x = Np.arange (9.). Reshape (3, 3)>>> np.where (x > 5) (Array ([2, 2, 2]), array ([0, 1, 2]))>>> x[np.where (x > 3.0)]#Note:result is 1D.Array ([4., 5., 6., 7., 8.])>>> Np.where (x < 5, x,-1)#note:broadcasting.Array ([[0., 1., 2.], [ 3., 4.,-1.], [-1.,-1.,-1.]]) Find the indices of elements of x that isinchgoodvalues.>>>>>> goodvalues = [3, 4, 7]>>> IX =np.in1d (X.ravel (), goodvalues). Reshape (X.shape)>>>Ixarray ([[False, False, false], [true, True, false], [False, True, false]], Dtype=bool)>>>Np.where (ix) (Array ([1, 1, 2]), array ([0, 1, 1])
How to use the Where () function in Python