Summary of commonly used functions of numpy(updating)
numpy.nonzero()
return a tuple, whose two dimensions respectively represent the indexes of nonzero numbers of corresponding dimensions.
Example:
import numpy as np
A = np.mat([[0,1,2,3,4,3,2,1,0],[0,1,2,3,4,5,6,7,0]])
x = A.nonzero() #collect the indexes of nonzero numbers
print(x)
Output:
(array([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], dtype=int64), array([1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7], dtype=int64))
np.sum()
Add the elements in the array up. axis is the mostly used argument of this function.
axis=1 add by column, keep the column number, row number becomes 1.
axis=0 add by row, keep the row number, column number becomes 1.