Python NumPy Array mean() Function

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:January 29, 2023
Spread the love

Python NumPy array mean() function is used to compute the arithmetic mean or average of the array elements along with the specified axis or multiple axis. You get the mean by calculating the sum of all values in a Numpy array divided by the total number of values.

By default, the average is taken from the flattened array (from all array elements), otherwise along with the specified axis. When an input is an integer array, it returns Float 64 intermediate but you can change this behavior by specifying the return data type.

In this article, I will explain numpy.mean() function syntax, usage, and how to calculate the mean for the given single-dimensional or multi-dimensional array.

1. Quick Examples of NumPy Array mean() Function

If you are in a hurry, below are some quick examples of how to calculate the mean of an array by using the Python NumPy array mean() function.


# Below are the quick examples

# Example 1: Get the mean of 1-D array
arr = np.array([2, 7, 5, 8, 9,4])

arr1 = np.mean(arr)

# Example 2: Get the mean of 2-D array
arr = np.array([[5, 8, 3, 7], [9, 4, 2, 6]])
arr1 = np.mean(arr) 

# Example 3: Get the mean values over row 
# for each of 4 columns 
arr1 = np.mean(arr, axis = 0)

# Example 4: Get the mean values over column 
# for each of 2 rows 
arr1 = np.mean(arr, axis = 1)

# Example 5: Create 3-D array
# Get the mean values of an array along multiple axis
arr = np.array([[[5, 8, 3], [9, 4, 2]], [[3, 9, 5], [2, 6, 8]]])
arr1 = np.mean(arr, axis=(0, 1))

# Example 6: Get the mean value of an array 
# with specified datatype
arr1 = np.mean(arr, dtype = np.float32)

# Example 7: Get the mean of arr float64 data
arr2 = np.mean(arr, dtype = np.float64)

2. Syntax of NumPy Array mean()

Following is the syntax of the numpy.mean() function.


# Syntax numpy.mean() 
numpy.mean(arr, axis=None, dtype=None, out=None)

2.1 Parameters of mean()

  • arr – Array containing numbers to calculate mean. If arr is not an array, a conversion is attempted.
  • axis – [ int or tuples of int] This parameter defines the axis along which the means are computed. By default, the mean is computed of the flattened array. If this is a tuple of ints, the mean is performed over multiple axes, instead of a single axis or all the axes as before.
  • dtype – Type to be used during the calculation of the arithmetic mean. For integer inputs, the default is float64.
  • out – Alternate output array in which to place the result.

2.2 Return Value of mean()

It returns the arithmetic mean of the array (a scalar value if the axis is none) or array with mean values along the specified axis.

3. Usage of NumPy mean() Function

Arithmetic mean is a sum of array elements, along with the specified axis divided by the number of elements. The mean() function returns the arithmetic mean along with the specified axis of array elements.

3.1 Get the Arithmetic Mean of 1-D Array

Take a one-dimensional NumPy array and compute the mean of the array using numpy.mean() function, for that let’s create an array using numpy.array(). For example,


import numpy as np
# Create 1D array
arr = np.array([2, 7, 5, 8, 9,4])

# Get the mean of 1-D array
arr1 = np.mean(arr)
print(arr1)

# Output :
# 5.833333333333333

4. Get the Arithmetic Mean of 2-D Array

This function is also used to compute the mean of the 2-D NumPy array, By default it considers all elements to calculate the mean. You can change this behavior by using the axis param.


# Create 2-D array
arr = np.array([[5, 8, 3, 7], [9, 4, 2, 6]])
# Get the mean of 2-D array
arr1 = np.mean(arr) 
print(arr1)

# Output:
# 5.5

5. Get the Mean Values of 2-D NumPy Array along Axis

We can also compute the mean value of a NumPy array along with a specified axis. If we want to compute the mean value of each row, we will pass the axis = 0 parameter through the mean() function. Similarly, to compute the mean value of each column, use axis = 1.


# Get the mean values over row 
# for each of 4 columns 
arr1 = np.mean(arr, axis = 0)
print(arr1)

# Output
# [7.  6.  2.5 6.5]

# Get the mean values over column 
# for each of 2 rows 
arr1 = np.mean(arr, axis = 1)
print(arr1)

# Output
# [5.75 5.25]

6. Get the Mean Values of 3-D Array along Multiple Axis

We can also use this to compute the mean values of the 3-Dimensional NumPy Array, for that we have to specify at least two axes. Let’s take an example,


# Create 3-D array
arr = np.array([[[5, 8, 3], [9, 4, 2]], [[3, 9, 5], [2, 6, 8]]])
# Get the mean values of an array along multiple axis
arr1 = np.mean(arr, axis=(0, 1))
print(arr1)

# Output
# [4.75 6.75 4.5 ]

7. Use Datatype Param

Let’s use the dtype parameter to specify the result mean data type. The result has a lower resolution if you use float32 data type rather than the default float64.


# Get the mean value of an array 
# with specified datatype
arr1 = np.mean(arr, dtype = np.float32)
print(arr1)

# Output
# 5.5

# Get the mean of arr float64 data
arr1 = np.mean(arr, dtype = np.float64)
print(arr1)

# Output
# 5.5

Conclusion

In this article, I have explaithe ned how to calculate the arithmetic mean of NumPy array along with the specified axis and multiple axes. Also explained how to use dtype optional param to change the return data type.

Happy Learning!!

References

Leave a Reply

You are currently viewing Python NumPy Array mean() Function