It may be normal for a professional pythonist, It takes time for me to understand If I could understand it, I was impressed by its strength. .. .. Because. .. ..
Determine whether each element meets or does not meet the condition in the matrix, The resulting True / False is output as a matrix with the same shape.
arr = np.array([[0,1,2,3],[0,2,4,6]])
print(arr<3)
# [[ True True True False]
# [ True True False False]]
numpy.nonzero It gets the index of the non-zero element of the matrix and outputs it as an x, y separate ndarray array.
#Example definition
arr_int = np.array([[3,5,0],[0,4,0]])
arr_bool = np.array([[True,True,False],[False,True, False]])
# np.Use of nonzero
nonzero_int_row, nonzero_int_column = np.nonzero(arr_int)
nonzero_bool_row, nonzero_bool_column = np.nonzero(arr_bool)
#The value of each variable
# row: array([0, 0, 1])
# column: array([0, 1, 1]))
In this example, arr [0,0], arr [0,1], arr [1,1] are non-zero, so this is the result.
arr = np.array([[0,1,2,3],[0,2,4,6]])
arr_bool = arr<3
nonzero_row, nonzero_column = np.nonzero(arr_bool)
# row: [0 0 0 1 1]
# column: [0 1 2 0 1]
I got a comment! It means that np.where has the same function and is convenient! Why didn't the site I was looking at use this ...
Recommended Posts