During the training of the company, I was forced to work on knocking 100 Numpy, but there were many problems that I could not beat without knowing the function, and I was keenly aware of my lack of knowledge. In this article, I will introduce some of the functions that I found to be "convenient" as a memorandum.
Programming history: Approximately 1 year (Python only) Numpy studied at this book.
When you come across a function for the first time, it's overwhelmingly quicker to look at help before google. help is
np.info([Function name])
ex) np.info(np.add)
If it is a jupyter notebook
[Function name]?
ex) np.add?
indicated by. Not only the explanation of the function but also the types of arguments and usage examples are described, and the amount of information is very large. Since it is in English, it may be painful if you are not used to it, but I would like you to refer to it.
np.flip Invert the array.
a = np.arange(10)
np.flip(a)
-> array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
Slices ([:: -1]) are sufficient for vectors, but they seem to come in handy for matrices. For matrices, you can specify axis to flip them in any direction.
a = np.arange(9).reshape(3,3)
np.flip(a)
-> array([[8, 7, 6],
[5, 4, 3],
[2, 1, 0]])
np.flip(a, axis=0)
->array([[6, 7, 8],
[3, 4, 5],
[0, 1, 2]])
np.flip(a, axis=1)
-> array([[2, 1, 0],
[5, 4, 3],
[8, 7, 6]])
np.eye Generate an identity matrix.
np.eye(3)
-> array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
np.diag Extract the diagonal components.
a = np.random.randint(0,10, (3,3))
print(a)
-> [[5 2 8]
[2 7 5]
[5 1 0]]
p.diag(a)
-> array([5, 7, 0])
np.tile Spread the array.
a = np.array([[0, 1], [1, 0]])
np.tile(a, (2,2))
-> array([[0, 1, 0, 1],
[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 0, 1, 0]])
np.bincount Counts the numbers (non-negative, int type) in the array and stores them in the index of that value.
a = np.random.randint(0, 10, 10)
print(a)
-> array([8 6 0 8 4 6 2 5 2 1])
np.bincount(a)
-> array([1, 1, 2, 0, 1, 1, 2, 0, 2], dtype=int64)
np.repeat Repeats the element a specified number of times.
np.repeat(3, 4)
-> array([3, 3, 3, 3])
np.roll Shifts the array to the right by the specified number.
a = np.arange(10)
np.roll(a, 2)
-> array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
np.flatten Converts the original array to a one-dimensional array.
a = np.arange(9).reshape(3,3)
print(a)
-> [[0 1 2]
[3 4 5]
[6 7 8]]
b = a.flatten()
print(b)
-> [0 1 2 3 4 5 6 7 8]
np.nonzero It will tell you the index that contains non-zero values.
a = np.array([1,0,0,1])
b = np.array([1,1,1,1])
print(np.nonzero(a))
-> (array([0, 3], dtype=int64),)
print(np.nonzero(b))
-> (array([0, 1, 2, 3], dtype=int64),)
np.copysign Converts the sign of the first argument to the same sign as the second argument.
a = np.arange(10)
b = np.repeat([1, -1], 5)
np.copysign(a, b)
-> array([ 0., 1., 2., 3., 4., -5., -6., -7., -8., -9.])
np.intersect1d Extracts the common elements of the two arrays.
a = np.arange(10)
b = np.arange(5, 15)
np.intersect1d(a, b)
-> array([5, 6, 7, 8, 9])
These functions aren't memorized, but you can't use them without knowing them in the first place. I would be very happy if the functions I introduced could remain in the corner of your head!
Recommended Posts