Super classic | Summarized 12 Numpy advanced functions, and all of them are good!

I didn't want to talk about the Numpy function specifically, but today someone asked about it. This time, classmate Huang took the opportunity to summarize these 12 Numpy advanced functions for everyone, everyone must master it, because it is really easy to use! very useful! very useful!

Before formally telling about the 12 functions, take a look at the outline prepared by classmate Huang, tidy it up, and remember to save it.

1. np.where(condition,x,y)

• Usage 1: If the condition is satisfied, output x, but output y if the condition is not satisfied.
• Usage 2: Filter out the elements that meet the condition.

Example 1: Find a value greater than 5 in the array and return it. For the part less than or equal to 5, directly use 5 instead;

``````import numpy as np
x = np.array([1,3,5,7,9])

z = x > 5
z

np.where(z,y,5)
``````

The results are as follows:

Example 2: Find people who are greater than 18 years old in the array and return their subscripts;

``````y = np.array([19,35,15,25,10])
y

z = y > 18
z

np.where(z)
``````

The results are as follows:

2. np.cumsum() and np.cumprod()

• np.cumsum(): Calculate the cumulative sum of elements according to different axes.
• np.cumprod(): Calculate the cumulative product of the elements according to different axes.
• Note: If the axis is not set, the array will be automatically drawn into a straight line, and then accumulated or accumulated.

If axis is not set:

``````x = np.array([[1,2],[4,5],[7,8]])
x

np.cumsum(x)

np.cumprod(x)
``````

The results are as follows:

axis=0 means [operate in column direction]; axis=1 means [operate in row direction]

``````np.cumsum(x,axis=0)
np.cumsum(x,axis=1)
``````

The results are as follows:

``````np.cumprod(x,axis=0)
np.cumprod(x,axis=1)
``````

The results are as follows:

3. np.argmin() and np.argmax()

• np.argmin(): According to different axes, returns the subscript of the minimum element.
• np.argmax(): According to different axes, returns the subscript of the maximum element.
• Note: If axis is not set, the array will be automatically drawn into a straight line, and the subscripts of the maximum and minimum elements will be returned.

If axis is not set:

``````x = np.array([[2,1,7],[6,0,3],[5,4,8]])
x

np.argmin(x)

np.argmax(x)
``````

The results are as follows:

axis=0 means [operate in column direction]; axis=1 means [operate in row direction]

``````np.argmin(x,axis=0)
np.argmin(x,axis=1)
``````

The results are as follows:

``````np.argmax(x,axis=0)
np.argmax(x,axis=1)
``````

The results are as follows:

4. np.sort()

• np.sort(): Sort elements according to different axes.
• The default is to operate according to the line, which is equivalent to axis=1.
``````x = np.array([[2,1,7],[6,0,3],[5,4,8]])
x

np.sort(x)
np.sort(x,axis=1)
``````

The results are as follows:

``````np.sort(x,axis=0)
``````

The results are as follows: