Application to multidimensional arrays
function usage
Returns the argument of the lookup, indexed in the array.
Code
Examples:
- The input or output of a general convolutional neural network is a four-dimensional array/Tensor.
- Generally[batch_size, channel, height, width]
The goal of the following code is to output the index of all numbers that have a value of 0.
output = [[ [[1, 0, 2], [2, 1, 0], [1, 0, 0]] ]] arr = (output) print() res = (arr==0) print(res)
Output
# print() (1, 1, 3, 3) # print(res) (array([0, 0, 0, 0], dtype=int64), array([0, 0, 0, 0], dtype=int64), array([0, 1, 2, 2], dtype=int64), array([1, 2, 1, 2], dtype=int64))
The output of is a list containing four ndarrays, each representing four dimensions.
[0, 0, 0, 0] # axis=0 [0, 0, 0, 0] # axis=1 [0, 1, 2, 2] # axis=2 [1, 2, 1, 2] # axis=3
To read the values correctly, looking at the columns, the indexes of each of the four 0 values are
print(arr[0][0][0][1]) # output:0 print(arr[0][0][1][2]) # output:0 print(arr[0][0][2][1]) # output:0 print(arr[0][0][2][2]) # output:0
() Usage Analysis
grammatical description
(condition,x,y)
- When there are three parameters within where, the first parameter represents the condition, and the where method returns x when the condition holds, and y when the condition does not hold.
(condition)
- When there is only one parameter within the where, that parameter represents the condition, when the condition is established, where returns the coordinates of each element that meets the condition, the return is in the form of a tuple, the coordinates are given in the form of a tuple, usually how many dimensions of the original array, the output of the tuple contains a few arrays, which correspond to the conditions of the elements of the coordinates of the various dimensions.
Multi-condition
- -& means with, | means or.
- For example, a = ((a>0)&(a<5), x, y) returns the value of x when a>0 is satisfied with a<5, and the value of y when a>0 is not satisfied with a<5.
- Note: x, y must be of the same dimension as a for the values of the array to correspond to each other.
typical example
(1) A parameter
import numpy as np a = (0, 100, 10) b = (a < 50) c = (a >= 50)[0] print(a) print(b) print(c)
The results are as follows:
[ 0 10 20 30 40 50 60 70 80 90]
(array([0, 1, 2, 3, 4]),)
[5 6 7 8 9]
Description:
- b is the position of the element that meets the condition of being less than 50, and the data type of b is tuple
- c is the position of the element that meets the condition greater than or equal to 50, and the data type of c is
(2) Three parameters
a = (10) b = (0,100,10) print((a > 5, 1, -1)) print(b) print(((a>3) & (a<8),a,b)) c=((a<3) | (a>8),a,b) print(c)
The results are as follows:
[-1 -1 -1 -1 -1 -1 1 1 1 1]
[ 0 10 20 30 40 50 60 70 80 90]
[ 0 10 20 30 4 5 6 7 80 90]
[ 0 1 2 30 40 50 60 70 80 9]
Description:
- (a > 5, 1, -1) which satisfies the condition 1 and does not satisfy it -1
- ((a>3) & (a<8),a,b), which satisfies the condition a , and which does not satisfy it is b , and a and b have the same dimensions
Attention:
& | with and or, each condition must be enclosed in parentheses, otherwise an error is reported.
c=((a<3 | a>8),a,b)
ValueError: The truth value of an array with more than one element is ambiguous. Use () or ()
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.