NumPy mean()

The numpy.mean() is a built-in method in the NumPy library that is used to calculate the mean of an array of elements along with an axis.

In NumPy array library or Mathematics, the arithmetic mean is the sum of the given elements along with an axis divided by the total number of elements. It is the average of the given numbers.

For Example – Imagine we have a NumPy array of five values as shown.

Image 11
NumPy Array

We can use the NumPy np.mean() function to compute the mean of the Array as shown below.

Image 12
Compute the mean using np.mean() method

NumPy mean() Syntax

The Syntax of numpy.mean() method is:

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

NumPy mean() Parameters

  • array_name: the name of the input array containing input numbers for which the mean needs to be computed.
  • axis: None or int or tuple of ints (optional) – the alignment or axis of the mean to be computed. If None, the default is to compute the mean of the flattened array. If int or tuple of ints, a mean is performed over multiple axes, instead of a single axis or all the axes as before.
  • dtype: [int, float64] (optional) – Data Type to use in computing the mean, use ‘int’ for integer and ‘float64’ for floating point value.
  • out: nd_array (optional) – Alternate output array in which the results are to be placed. The default is None
  • keepdims: bool (optional) – If the value of keepdims is set to True, then the axes that are reduced will be left in the result as dimensions with size one.
  • where: array_like of bool (optional) – Elements to be included in the mean.

NumPy mean() Return Value

The numpy.mean() method will return a new array with mean values as the elements if out = None, else a reference to the output numpy array is returned.

Example 1: Using the numpy.mean() method on a flattened array

In the below example the numpy.mean() function will compute the weighted mean of a numpy n-dimensional array array_in input array.

# import the numpy module
import numpy as np

# initialize the input array
array_in = [[15, 25, 30], [35, 55, 70]]

print("The input numpy array is:\n", array_in)

# the mean of the array
print("The mean of the array in flattened is:\n", np.mean(array_in, axis=None))

Output

The input numpy array is:

 [[15, 25, 30], [35, 55, 70]]

The mean of the array in flattened is:
 38.333333333333336

Example 2: Compute the mean of the array column-wise using numpy.mean()

When the axis parameter is specified as axis = 0, then the mean is computed on the column-major elements as shown in the below example.

# import the numpy module
import numpy as np

# initialize the input array
array_in = [[15, 25, 30], [35, 55, 70]]

print("The input numpy array is:\n", array_in)

# the mean of the array as
print("The mean of the array in column-major is:\n", np.mean(array_in, axis=0))

Output

The input numpy array is:
 [[15, 25, 30], [35, 55, 70]]

The mean of the array in column-major is:
 [25. 40. 50.]

Example 3: Compute the mean of the array row-wise using numpy.mean()

When the axis parameter is specified as axis = 1, then the mean is computed on the row-major elements as shown in the below example.

# import the numpy module
import numpy as np

# initialize the input array
array_in = [[15, 25, 30], [35, 55, 70]]

print("The input numpy array is:\n", array_in)

# the mean of the array
print("The mean of the array in row-major is:\n", np.mean(array_in, axis=1))

Output

The input numpy array is:
 [[15, 25, 30], [35, 55, 70]]

The mean of the array in row-major is:
 [23.33333333 53.33333333]

Example 4: Compute the mean of the array as flattened

When the axis parameter is specified as axis = None, then the array will be operated as flattened for calculating the mean of the elements.

import numpy as np

# initialize input array
array_in = [[1.0, 0.1], [1.5, 0.5]]

print("The input numpy array is:\n", array_in)

# the mean of the array as default float64 data_type
print("The array of means of the input array is:\n", np.mean(array_in, axis=1))

# using output data_type as float64
print(
    "The array of means of the input array in float64 is:\n",
    np.mean(array_in, axis=1, dtype=np.float64),
)

# using output data_type as float32
print(
    "The array of means of the input array in float32 is:\n",
    np.mean(array_in, axis=1, dtype=np.float32),
)

# using output data_type as int
print(
    "The array of means of the input array in integer is:\n",
    np.mean(array_in, axis=1, dtype=int),
)

Output

The input numpy array is:
 [[1.0, 0.1], [1.5, 0.5]]

The array of means of the input array is:
 [0.55 1.  ]

The array of means of the input array in float64 is:
 [0.55 1.  ]

The array of means of the input array in float32 is:
 [0.55 1.  ]

The array of means of the input array in integer is:
 [0 0]

Example 5: Compute the mean of the array with column and row majors together

When the axis parameter is specified as axis=(0,1), then the mean is computed along both the column and row axis as shown in the example.

import numpy as np

array_in = [[15, 25, 30], [35, 55, 70]]

print("The input numpy array is:\n", array_in)

# the mean of the array both row-major and column-major
array_out = np.mean(array_in, axis=(0, 1))
print(
    "The mean of the array in both row-major and column-major and type of the output respectively are:\n",
    array_out,
)
print(type(array_out))

Output

The input numpy array is:
 [[15, 25, 30], [35, 55, 70]]

The mean of the array in both row-major and column-major and type of the output respectively are:
 38.333333333333336

<class 'numpy.float64'>

Example 6: Computing the mean of the array with where() parameter

If we provide the parameter where = [[True],[False],[False]] then the numpy.mean() function will check the boolean array [[True],[False],[False]], if the value of the element in the boolean array is True, then that row is selected. That is only the first row will get selected for the mean.

import numpy as np

# initialize the input array
array_in = np.array([[5, 9, 13], [14, 10, 12], [11, 15, 19]])

print("The input nd_array is:\n\n", array_in)

# flattened average of the input array
print("\nmean with every row is selected :\n\n", np.mean(array_in, axis=None))

# every dimension is selected
print(
    "\nmean with every row is selected :\n\n",
    np.mean(array_in, axis=None, where=[[True], [True], [True]]),
)

# only the first row is selected for average
print(
    "\nmean with only the first row is selected :\n\n",
    np.mean(array_in, axis=None, where=[[True], [False], [False]]),
)

# only the last row is selected for average
print(
    "\nmean with only the last row is selected :\n\n",
    np.mean(array_in, axis=None, where=[[False], [False], [True]]),
)

# only the first and second row is selected for average
print(
    "\naverage with only the first and second row is selected :\n\n",
    np.mean(array_in, axis=None, where=[[True], [True], [False]]),
)

Output

The input nd_array is:
 [[ 5  9 13]
 [14 10 12]
 [11 15 19]]

mean with every row is selected :
 12.0

mean with every row is selected :
 12.0

mean with only the first row is selected :
 9.0

mean with only the last row is selected :
 15.0

average with only the first and second row is selected :
 10.5

Conclusion

The numpy.mean() method is used to compute the arithmetic mean along the specified axis. The mean() method returns the new array with mean values as the elements if out = None, else a reference to the output NumPy array is returned.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Remove Newline From String

Python Remove Newline From String

Table of Contents Hide Python Remove Newline From StringUsing strip() method to remove the newline character from a stringUsing replace() method to remove newlines from a stringUsing regex to remove…
View Post
Python Callable()

Python callable()

Table of Contents Hide callable() Syntax callable() Parameterscallable() Return ValueExample 1: How does callable() works?Example 2: When an Object is callable Example 3: When an Object is NOT callable  The callable() function in…
View Post