Mean of Numpy Array | Explained With Examples

In Python, different libraries and modules are used to perform the arithmetic and computation operations. Python’s “NumPy” library performs different computation operations such as mean, median and standard deviation, etc. This write-up will provide a complete overview of the “mean of the Numpy array” with detailed examples:  

So let’s begin!

What is the Mean of Numpy Array in Python?

In Python, the “array” data structure is used to store multiple elements of the same data simultaneously. The “Mean” value is generally defined as the average of all input numbers. The function named “numpy.mean()” is used to calculate the mean of any input array, including “1D”,”2D”, and “3D”. The syntax of the “numpy.mean()” function is shown below:

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

In the above syntax:

  • The first parameter named “arr” takes an array variable as an input. This parameter is mandatory to initialize otherwise, the numpy.mean() function does not execute.
  • The second parameter named “axis” is used to define the axis in which the mean is calculated. The default value of the axis parameter in which the mean is computed is  “flattened array”.
  • The “dtype” parameter is used to define the type of calculation. The default type for all the ”integer” values is float.
  • The “out” parameter is used for result placement, and it is an alternative output array.
How to install Kali Linux in Window...

Let’s understand the mean of Numpy using a few examples:

Example 1: Using Numpy to Calculate the Mean of 1-D Array

The “np.array()” function (the second name of the numpy.mean() function) is used to create the “1D” array and the “np.mean()” function is used to calculate the mean of the “1D” Array. Let’s calculate the mean of “1-D” array via the following code:

Code

#Using Numpy to Calculate the Mean of 1-D Array
import numpy as np
array_1D = np.array([12, 17, 52, 83, 49,44])

# Calculating the mean of 1-D array
array = np.mean(array_1D)
print(array)

In the above code:

  • The “NumPy” library is accessed at the beginning of code.
  • The “1D array” is created using the “np.array()” function and stored in a variable named “array_1D
  • The mean of the “1D array” is calculated using the “np.mean()” function by taking the argument value of the variable named “array_1D”.
  • The print function prints the mean value on screen.

Output

Analyzing the smallest and the largest value inside the array, the mean of the array is calculated.

Example 2: Using Numpy to Calculate the Mean of 2-D Array

The “2D” or “Two-dimensional array” is just like a 1D array, but the 2-D array can be arranged in a row-column structure. In this example, the mean of the 2D array can be calculated using the “np.mean()” function. Let’s understand it with an example of code given below:

Code

#Using Numpy to Calculate the Mean of 2-D Array
import numpy as np
array_2D = np.array([[15, 8, 32, 73], [39, 44, 2, 62]])
# calculating the mean of 2-D array
array = np.mean(array_2D)
print(array)

In the above code:

  • The “NumPy” library is accessed at the start of code.
  • The “2D array” is created using the “np.array()” function and stored in a variable named “array_2D”.
  • The mean of the “2D array” is calculated using the “np.mean()” function by taking the argument value of the variable named “array_2D”.
  • The print function prints the mean value on screen.

Output

The “np.mean()” function has returned the mean of the “2-D” array.

Example 3: Using Numpy to Calculate the Mean of 3-D Array With Multiple Axis

The “np.mean()” function of Python can also calculate the mean of the NumPy array according to the specified axis. For the mean calculation of the “3D” array, we must specify more than one axis in the “axis parameter”. Let’s see how the mean of the 3-D array with multiple axis is calculated:

Code

#Using Numpy to Calculate the Mean of 3-D Array with multiples axis
import numpy as np

array_3D = [[[6, 7, 4], [8, 5, 3]],[[4, 8, 6], [3, 5, 4]]]
axis_none = np.mean(array_3D)
axis_0 = np.mean(array_3D, axis=0)
axis_1 = np.mean(array_3D, axis=1)

print("\nMean value of 3D array, when axis = None : ", axis_none)
print("\nMean value of 3D array, when axis = 0 : ", axis_0)
print("\nMean value of 3D array, when axis = 1 : ", axis_1)

In the above code:

  • The “NumPy” library is accessed at the start of code.
  • The “3D array” is created using the “np.array()” function and stored in a variable named “array_3D”.
  • There are three “np.mean()” functions used in the program.
  • The first “np.mean()” function takes a variable named “array_3D” as an input parameter and saves the value of an array with a “flattened axis” (which means 1D-Iterator over the array)
  • The second “np.mean()” function takes a variable named “array_3D” and “axis value=0” as an input parameter.
  • The third “np.mean()” function takes a variable named “array_3D” and “axis value=1” as an input parameter.
  • Three print statements are used to print the mean value of a “3D array” with different axis values such as “flattened”, “axis=0” and “axis=1”.

Output

In the above output, the mean value will be printed on the screen according to the axis value.

Note: The 2-Dimension has two axes which mean one axis is its row (axis 0), and another is its columns (axis 1). Similarly, 3-D arrays have more than two axes.

That’s it from this informative guide!

Conclusion

In Python, the mean of the NumPy array is calculated using the “np.mean()” function. This function can calculate the mean of “1-Dimensional”,”2-Dimensional” and “3-Dimensional” arrays along with the axis. The “np.mean()” function calculates the arithmetic mean of an array along with the single axis and for multiple axes. This article briefly explained the “Mean of Python Numpy Array” along with multiple “axes” with numerous examples.