What is axis of numpy.mean()
In NumPy, an array can have one or more dimensions, each of which is represented by an axis. The axis
parameter in NumPy functions such as np.mean()
specifies the axis along which an operation should be performed.
For example, consider a two-dimensional array array2
with shape (3, 4) representing three rows and four columns. If we want to calculate the mean of each column, we can use the np.mean()
function with axis=0
. This means that the mean will be calculated along the first axis (which represents the rows), resulting in a one-dimensional array of length 4 representing the means of each column.
On the other hand, if we want to calculate the mean of each row, we can use the np.mean()
function with axis=1
. This means that the mean will be calculated along the second axis (which represents the columns), resulting in a one-dimensional array of length 3 representing the means of each row.
In general, the axis
parameter specifies the axis or axes along which an operation should be performed. If axis=None
(the default), the operation is performed on the entire array. If axis=0
, the operation is performed along the first axis (rows). If axis=1
, the operation is performed along the second axis (columns), and so on for higher-dimensional arrays.