API · SciPy

SciPy stats.mean Function

This tutorial will tell what is stats mean function by using the SciPy library in Python

Arithmetic mean in statistics is defined as the measure of the average value of the collection of data. It is calculated by calculating the sum of all the values present in the data and dividing it by the sum of the number of values.

the scipy.stats.mean Function

The scipy.stats.mean function of the SciPy library helps calculate the arithmetic mean of the elements of a given array. This function is defined as scipy.stats.mean(array, axis).

Following are the parameters of the scipy.stats.mean function.

array It defines the input array with the elements of which the arithmetic mean has to be calculated.
axis It defines the axis along which the arithmetic mean of the input array containing the elements has to be calculated. The default value of this parameter is 0.

The axis parameter is optional. That means it is not necessary to mention it every time while using the scipy.stats.mean function.

Arithmetic Mean of One-Dimensional Array

import scipy

input_array = scipy.mean([2, 12, 9, 37, 20, 10, 4, 27])

print("Arithmetic Mean of the input array :", input_array)

Output:

Arithmetic Mean of the input array : 15.125

Note that the axis parameter is not mentioned in the above example.

Arithmetic Mean of a Multi-Dimensional Array

from scipy import mean

input_array = [[2, 12, 9], [37, 20, 10], [4, 27, 13], [9, 12, 26]]

print("Arithmetic Mean of the input array :", mean(input_array))

Output:

Arithmetic Mean of the input array : 15.083333333333334

Now let us define the axis parameter along with the above multi-dimensional array.

from scipy import mean

input_array = [[2, 12, 9], [37, 20, 10], [4, 27, 13], [9, 12, 26]]

print("Arithmetic Mean with axis = 0 : ", mean(arr1, axis=0))
print("Arithmetic Mean with axis = 1 : ", mean(arr1, axis=1))

Output:

Arithmetic Mean with axis = 0 :  [13.   17.75 14.5 ]
Arithmetic Mean with axis = 1 :  [ 7.66666667 22.33333333 14.66666667 15.66666667]