How to Calculate Mean Using NumPy in Python
To calculate the mean of an array in NumPy, use the
numpy.mean() function. Pass your data array as the first argument, and optionally specify the axis to calculate the mean along rows or columns.Syntax
The basic syntax of the numpy.mean() function is:
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=False)
Where:
a: Input array containing numbers.axis: Axis or axes along which to compute the mean. Default isNone, which computes mean of the flattened array.dtype: Data type for the computation, optional.out: Alternative output array to place the result, optional.keepdims: IfTrue, retains reduced dimensions with size 1.
python
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=False)
Example
This example shows how to calculate the mean of a 1D and 2D NumPy array. It demonstrates the default behavior and how to use the axis parameter.
python
import numpy as np # 1D array arr1 = np.array([10, 20, 30, 40, 50]) mean1 = np.mean(arr1) # 2D array arr2 = np.array([[1, 2, 3], [4, 5, 6]]) mean_all = np.mean(arr2) # mean of all elements mean_axis0 = np.mean(arr2, axis=0) # mean of each column mean_axis1 = np.mean(arr2, axis=1) # mean of each row print(f"Mean of 1D array: {mean1}") print(f"Mean of all elements in 2D array: {mean_all}") print(f"Mean along axis 0 (columns): {mean_axis0}") print(f"Mean along axis 1 (rows): {mean_axis1}")
Output
Mean of 1D array: 30.0
Mean of all elements in 2D array: 3.5
Mean along axis 0 (columns): [2.5 3.5 4.5]
Mean along axis 1 (rows): [2. 5.]
Common Pitfalls
Common mistakes when calculating mean with NumPy include:
- Forgetting to specify
axiswhen working with multi-dimensional arrays, which results in the mean of the entire array instead of along a specific axis. - Passing a list instead of a NumPy array, which still works but is less efficient.
- Not handling empty arrays, which raises a warning and returns
nan.
python
import numpy as np # Wrong: mean of entire array instead of axis arr = np.array([[1, 2], [3, 4]]) mean_wrong = np.mean(arr) # mean of all elements # Right: mean along axis 0 (columns) mean_right = np.mean(arr, axis=0) print(f"Wrong mean (all elements): {mean_wrong}") print(f"Right mean (axis=0): {mean_right}")
Output
Wrong mean (all elements): 2.5
Right mean (axis=0): [2. 3.]
Quick Reference
Here is a quick summary of how to use numpy.mean():
| Parameter | Description | Default |
|---|---|---|
| a | Input array | Required |
| axis | Axis to compute mean along; None for all elements | None |
| dtype | Data type for computation | None |
| out | Alternative output array | None |
| keepdims | Keep reduced dimensions as size 1 | False |
Key Takeaways
Use numpy.mean() to calculate the average value of array elements easily.
Specify the axis parameter to calculate mean along rows or columns in multi-dimensional arrays.
Passing a list works but converting to a NumPy array is more efficient.
Empty arrays cause warnings and return nan; always check your data before computing mean.
Keepdims=True helps maintain array dimensions after mean calculation when needed.