Table of Contents
ToggleIn mathematics, the mean is the average value of a set of numbers. The most common type is the arithmetic mean, which is the sum of the numbers divided by the count of the numbers.
Other types include the geometric mean (nth root of the product of the numbers) and the harmonic mean (number of values divided by the sum of reciprocals).
These different means are used based on the nature of the data and specific needs of the analysis.
The mean() function in NumPy calculates the arithmetic mean (average) of the elements in an array. By default, it computes the mean of all elements, but you can specify an axis to compute the mean along rows or columns.
It can also handle different data types and allow you to define the output type. For example, np.mean([1, 2, 3, 4]) returns 2.5.
Following is the basic syntax of the mean() function in NumPy −
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=False)
Where,
If you have a one-dimensional array, you can use the numpy.mean() function to calculate the mean of its elements. Here is an example −
# Open Compiler
import numpy as np
# Define a 1D array
arr = np.array([1, 2, 3, 4, 5])
# Calculate the mean of all elements
mean_value = np.mean(arr)
print("Mean of the array:", mean_value)
Output:
Following is the output obtained −
Mean of the array: 3.0
In a two-dimensional array, you can compute the mean along a specific axis. For example, calculating the mean along rows or columns −
# Open Compiler
import numpy as np
# Define a 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Mean along rows (axis=1)
mean_rows = np.mean(arr_2d, axis=1)
# Mean along columns (axis=0)
mean_columns = np.mean(arr_2d, axis=0)
print("Mean along rows:", mean_rows)
print("Mean along columns:", mean_columns)
Output:
Following is the output obtained −
Mean along rows: [2. 5. 8.]
Mean along columns: [4. 5. 6.]
You can also specify the data type in which you want the mean to be computed. This is especially useful when dealing with large numbers or when you need the result in a specific precision (such as float64). Here is an example −
# Open Compiler
import numpy as np
# Define an array of integers
arr_int = np.array([10, 20, 30])
# Calculate the mean with a specified data type (float64)
mean_float = np.mean(arr_int, dtype=np.float64)
print("Mean with dtype float64:", mean_float)
Output:
Following is the output obtained −
Mean with dtype float64: 20.0
The keepdims parameter helps preserve the dimensionality of the original array after the mean operation. If set to True, the result will have the same number of dimensions as the input array, but the size of the reduced axes will be one.
# Open Compiler
import numpy as np
# Define a 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Mean along columns while keeping dimensions
mean_keepdims = np.mean(arr_2d, axis=0, keepdims=True)
print("Mean with keepdims=True:", mean_keepdims)
Output:
Following is the output obtained −
Mean with keepdims=True: [[4. 5. 6.]]
The numpy.mean() function has a wide range of applications in scientific computing, data analysis, and machine learning. Some common use cases are −
NumPy is optimized for fast array operations, and the numpy.mean() function is highly efficient. However, there are a few ways to further optimize your mean calculations −
out parameter, which avoids creating a new array and helps save memory.Key Takeaway: Master the NumPy mean function at Vista Academy!
