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!

Insert picture description here


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.

Insert picture description here

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:

Insert picture description here


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:

Insert picture description here

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:

Insert picture description here

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:

Insert picture description here
np.cumprod(x,axis=0)
np.cumprod(x,axis=1)

The results are as follows:

Insert picture description here

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:

Insert picture description here


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:

Insert picture description here
np.argmax(x,axis=0)
np.argmax(x,axis=1)

The results are as follows:

Insert picture description here

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:

Insert picture description here
np.sort(x,axis=0)

The results are as follows:

Insert picture description here

5. As shown in the picture (six in one)

Insert picture description here
① unique()
Insert picture description here
② np.in1d()
Insert picture description here
③ np.intersect1d()
Insert picture description here
④ np.union1d()
Insert picture description here
⑤ np.setdiff1d()
Insert picture description here
⑥ np.setxor1d()
Insert picture description here