NumPy mean() Function



The NumPy mean() function computes the arithmetic mean (average) of the elements in an array along a specified axis. The mean is the sum of the data values divided by the number of data points. The average is taken over the flattened array by default, otherwise over the specified axis. The float64 data type is intermediated and return values are used for integer inputs.

In statistics, the mean (also known as the average) is the sum of the data values divided by the number of data points. The formula is mean = (sum of all elements) / (number of elements).

For a one-dimensional array, the mean is computed over all elements. For multi-dimensional arrays, the mean is computed along the specified axis.

Syntax

Following is the syntax of the NumPy mean() function −

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>, where=<no value>)

Parameters

Following are the parameters of the NumPy mean() function −

  • a: Input array, which can be a NumPy array, list, or a scalar value.
  • axis (optional): Axis along which to compute the mean. Default is None, which means the mean is computed over the entire array.
  • dtype (optional): The data type used to compute the mean. Default is None, meaning the data type of the input array is used.
  • out (optional): A location into which the result is stored. If provided, it must have the same shape as the expected output.
  • keepdims (optional): If True, the reduced dimensions are retained as dimensions of size one in the output. Default is False.
  • where: This provides elements to be included in the mean

Return Values

This function returns the mean of the input array. If the axis parameter is specified, the mean is computed along that axis. The result is a scalar for one-dimensional input and an array for multi-dimensional input.

Example

Following is a basic example to compute the mean of an array using the NumPy mean() function −

import numpy as np
# input array
x = np.array([1, 2, 3, 4, 5])
# applying mean
result = np.mean(x)
print("Mean Result:", result)

Output

Following is the output of the above code −

Mean Result: 3.0

Example: Specifying an Axis

The mean() function can compute the mean along a specific axis of a multi-dimensional array. In the following example, we have computed the mean along axis 0 (columns) and axis 1 (rows) of a 2D array −

import numpy as np
# 2D array
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# applying mean along axis 0 (columns)
result_axis0 = np.mean(x, axis=0)
# applying mean along axis 1 (rows)
result_axis1 = np.mean(x, axis=1)
print("Mean along axis 0:", result_axis0)
print("Mean along axis 1:", result_axis1)

Output

Following is the output of the above code −

Mean along axis 0: [4. 5. 6.]
Mean along axis 1: [2. 5. 8.]

Example: Usage of 'keepdims' Parameter

The keepdims parameter allows the result to retain the reduced dimensions as size one. This is useful for broadcasting the result back to the original shape. In the following example, we have computed the mean along axis 0 and retained the reduced dimensions −

import numpy as np
# 2D array
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
result = np.mean(x, axis=0, keepdims=True)
print("Mean with keepdims=True:", result)

Output

Following is the output of the above code −

Mean with keepdims=True: [[4. 5. 6.]]

Example: Plotting 'mean()' Function

In the following example, we have plotted the behavior of the mean() function. We have calculated and plotted the mean for different sizes of input arrays by importing Numpy and matplotlib.pyplot module −

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.full_like(x, np.mean(x))
plt.plot(x, y, label="Mean")
plt.title("Mean Function")
plt.xlabel("Input")
plt.ylabel("Mean Value")
plt.legend()
plt.grid()
plt.show()

Output

The plot demonstrates the constant nature of the mean value across the input range −

Mean Visualization
numpy_statistical_functions.htm
Advertisements