NumPy mean (), STD () and other methods are acting on the entire numpy array, if it is a two-dimensional array, but also the entire array, including all the rows and columns, but we often need it only for rows or columns, rather than the entire two-dimensional array, this time, you can define axis axis:
axis=0 represents the action on the column
axis=1 indicates the action on the line
Take the sum () sum method as an example:
Import= Np.array ([ [1, 2, 3], [4, 5, 6], [7, 8, 9]]) Print a.sum ()# print a.sum (axis=0)
# Indicates the sum of each column # [] print a.sum (Axis=1)
# Indicates the sum of each row # [6]
A comprehensive chestnut:
# Suppose there are 5 subway stations for 10 days of passenger traffic data ridership = Np.array ([ [ 0, 0 ,2, 5, 0], [ 1478, 3877, 3674, 2328, 2539], [1613, 4088, 3991, 6461, 2691], [1560, 3392, 3826, 47 2613], [1608, 4802, 3932, 4477, 2705], [1576, 3933, 3909, 4979, 2685], [ c21/>, 229, 255, 496, 201], [ 2, 0, 1, , 0], [1438, 3785, 3589, 4174, 2215], [1342, 4043, 4009, 4665, 3033]]
The average value of the daily passenger flow is calculated from each station to find the maximum and minimum values:
def Min_and_max_riders_per_day (ridership): = Ridership.mean (axis=0) = Mean_ridership_per_station.max () = mean_ridership_per_ Station.min () return (max_daily_ridership, min_daily_ridership) Print (Min_and_max_riders_per_day (ridership)) # results (3239.9, 1071.2)
NumPy Array (5)-axis of two-dimensional array