numpy.mean

Full name
numpy.mean
Library
NumPy
Syntax

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=False)

Description

The numpy.mean function returns the average of the elements in array a. By default the average of the array is calculated once it is flattened. If an axis is specified, only that axis will be considered in the calculation.

Parameters
  • a: array_tipe. Array containing the values whose average is to be calculated. If a is not an array, the function tries to convert it.
  • axis: (Optional) None, integer or tuple of integers. Axis or axes on which to perform the calculation.
  • dtype: (Optional) data-type. Type to use for the calculation. If the input values are integers, the default is float64; for input values of type real numbers, the default value matches the one they have.
  • out: (Optional) ndarray. Alternative output array into which to dump the result. The default is None. If this argument is included, it must be the same size as the expected result, although the type will be modified if necessary.
  • keepdims: (Optional) Boolean. If it takes the value True, the axes that are reduced are left in the result with dimensions with size 1.
Result

The numpy.mean function returns a ndarray.

Examples

If we start from the following array a:

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print(a.shape)

Np.mean function. Example of use

...we can calculate the mean value of all the values included in the array with the following instructions:

r = np.mean(a)
print(r)
print(type(r))

Np.mean function. Example of use

If we specify that the calculation is to be performed along axis 0 (axis corresponding to the rows), the result is the following:

r = np.mean(a, 0)
print(r)
print(type(r))

Np.mean function. Example of use

Note the type of the result: a numpy array of size (3,).

We obtain a similar result if the calculation is carried out along axis 1 (columns):

r = np.mean(a, 1)
print(r)
print(type(r))

Np.mean function. Example of use

In this case, the result is also a numpy array, but its size is (2,).

If we set the keepdims argument to True, the dimensions are kept:

r = np.mean(a, 1, keepdims = True)
print(r)
print(r.shape)

Np.mean function. Example of use

We can force the type of the returned values in the result with the dtype argument:

r = np.mean(a, 1, dtype = np.int8)
print(r)
print(type(r))

Np.mean function. Example of use

In this example we start from a three-dimensional numpy array:

import numpy as np

a = np.array([
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])

print(a)
print(a.shape)

Np.mean function. Example of use

We can calculate the average over the different axes as shown below. The 0 axis (the one corresponding to the rows):

r = np.mean(a, 0)
print(r)
print(type(r))

Np.mean function. Example of use

Axis 1 (corresponding to the columns):

r = np.mean(a, 1)
print(r)
print(type(r))

Np.mean function. Example of use

And, finally, axis 2 (the one corresponding to depth):

r = np.mean(a, 2)
print(r)
print(type(r))

Np.mean function. Example of use

It is also possible to give a tuple as the axis value, indicating the axes on which the calculation will be carried out:

r = np.mean(a, (0, 2))
print(r)
print(type(r))

Np.mean function. Example of use
Submitted by admin on Mon, 01/21/2019 - 15:44