NumPy ndarray.mean()

The numpy.ndarray.mean() method calculates the arithmetic mean of elements in a NumPy array. It can compute the mean over the entire array or along a specified axis.

Syntax

</>
Copy
ndarray.mean(axis=None, dtype=None, out=None, keepdims=False, *, where=True)

Parameters

ParameterTypeDescription
axisNone, int, or tuple of ints, optionalAxis or axes along which the mean is computed. If None, computes the mean of the entire array.
dtypedata-type, optionalType of the returned array and accumulator for the calculation. Defaults to the input array type.
outndarray, optionalAlternative output array for storing the result. Must have the same shape as expected output.
keepdimsbool, optionalIf True, retains reduced dimensions with size one, allowing proper broadcasting.
wherearray_like of bool, optionalSpecifies elements to include in the mean calculation.

Return Value

Returns a scalar value if axis=None, or an array of mean values if an axis is specified. The mean is computed by summing all selected elements and dividing by their count.


Examples

1. Computing Mean of an Entire ndarray

In this example, we compute the mean of all elements in a NumPy array.

</>
Copy
import numpy as np  

# Creating a 2D NumPy array  
arr = np.array([[1, 2, 3],  
                [4, 5, 6]])  

# Computing the mean of all elements in the array  
result = arr.mean()  

# Printing the result  
print("Mean of entire array:", result)  

Output:

Mean of entire array: 3.5

The mean is calculated as \( \frac{1+2+3+4+5+6}{6} = 3.5 \).

2. Computing Mean Along a Specific Axis

Here, we compute the mean along different axes of a 2D array.

</>
Copy
import numpy as np  

# Creating a 2D NumPy array  
arr = np.array([[10, 20, 30],  
                [40, 50, 60]])  

# Computing mean along axis 0 (column-wise mean)  
result_axis0 = arr.mean(axis=0)  

# Computing mean along axis 1 (row-wise mean)  
result_axis1 = arr.mean(axis=1)  

# Printing results  
print("Mean along axis 0 (column-wise):", result_axis0)  
print("Mean along axis 1 (row-wise):", result_axis1)  

Output:

Mean along axis 0 (column-wise): [25. 35. 45.]
Mean along axis 1 (row-wise): [20. 50.]

Each column mean is computed independently, e.g., \( \frac{10+40}{2} = 25 \). Each row mean is computed similarly, e.g., \( \frac{10+20+30}{3} = 20 \).

3. Using dtype to Control Precision

The dtype parameter allows specifying a higher precision for the mean computation.

</>
Copy
import numpy as np  

# Creating an integer array  
arr = np.array([1, 2, 3, 4], dtype=np.int8)  

# Computing mean with default precision  
result_default = arr.mean()  

# Computing mean with higher precision using dtype=np.float64  
result_high_precision = arr.mean(dtype=np.float64)  

# Printing results  
print("Mean with default precision:", result_default)  
print("Mean with higher precision (float64):", result_high_precision)  

Output:

Mean with default precision: 2.5
Mean with higher precision (float64): 2.5

Using dtype=np.float64 ensures higher numerical stability for calculations.

4. Using where Parameter in ndarray.mean()

The where parameter allows computing the mean only for selected elements.

</>
Copy
import numpy as np  

# Creating a NumPy array  
arr = np.array([10, 20, 30, 40, 50])  

# Creating a boolean mask to include only elements greater than 20  
mask = arr > 20  

# Computing mean only for selected elements  
result = arr.mean(where=mask)  

# Printing result  
print("Mean considering only elements > 20:", result)  

Output:

Mean considering only elements > 20: 40.0

Only elements 30, 40, and 50 are included in the calculation, leading to a mean of (30+40+50)/3 = 40.0.