How does the axis parameter affect the calculation of the mean in numpy.mean()?

0305

The axis parameter in the numpy.mean() function determines the direction along which the mean is calculated in a multi-dimensional array.

  • axis=0: This calculates the mean along the rows, resulting in a mean for each column. It collapses the array along the first dimension (rows).

    mean_axis_0 = np.mean(array, axis=0)
  • axis=1: This calculates the mean along the columns, resulting in a mean for each row. It collapses the array along the second dimension (columns).

    mean_axis_1 = np.mean(array, axis=1)

If no axis is specified, the mean is calculated over the entire flattened array.

0 Comments

no data
Be the first to share your comment!