A list of functions that I came across with 100 Numpy knocks and thought "This is convenient!"

Overview

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.

Author profile

Programming history: Approximately 1 year (Python only) Numpy studied at this book.

at first

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.

"It's convenient" function

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])

Finally

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

A list of functions that I came across with 100 Numpy knocks and thought "This is convenient!"
Add a list of numpy library functions little by little --a
I thought about why Python self is necessary with the feeling of a Python interpreter
Add a list of numpy library functions little by little --c
This and that for using Step Functions with CDK + Python
[Python3] I made a decorator that declares undefined functions and methods.
I tried to create a list of prime numbers with python
I made a chatbot with Tensor2Tensor and this time it worked
i! i! ← This is a mathematical formula
Numpy functions I learned this year
This and that of python properties
Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type
I measured the speed of list comprehension, for and while with python2.7.
I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value
This and that of the inclusion notation.
Summary of numpy functions I didn't know
Graph trigonometric functions with numpy and matplotlib
I made a life game with Numpy
Python practice data analysis Summary of learning that I hit about 10 with 100 knocks
I want to write an element to a file with numpy and check it.
[Linux] A list of unique command selections that are convenient but unexpectedly unknown