DEV Community

Cover image for Mastering NumPy Array Mean Calculation
Labby for LabEx

Posted on

Mastering NumPy Array Mean Calculation

Introduction

MindMap

NumPy is a Python package for scientific computing that provides a high-performance array object, which is the fundamental building block for mathematical operations. The mean can easily be calculated by adding all the items of an array and dividing them by the total number of array elements. The numpy.mean() function in the NumPy library is used to compute the arithmetic mean across the specified axis of a numpy array. By default, the average is calculated over the flattened array unless the user specifies an axis.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import the NumPy library

The first step is to import the NumPy library.

import numpy as np
Enter fullscreen mode Exit fullscreen mode

Create a one-dimensional array

Create a one-dimensional array x with values [80, 23, 17, 1, 39].

x = np.array([80, 23, 17, 1, 39])
Enter fullscreen mode Exit fullscreen mode

Calculate the mean of the array

Use the numpy.mean() function to calculate the mean of the one-dimensional x array.

array_mean = np.mean(x)
print("The mean of the input array is: ", array_mean)
Enter fullscreen mode Exit fullscreen mode

Create a two-dimensional array

Create a two-dimensional array p with values [[14, 19, 12, 34, 43], [16, 8, 28, 8, 20], [25, 5, 55, 1, 2]].

p = np.array([[14, 19, 12, 34, 43], [16, 8, 28, 8, 20], [25, 5, 55, 1, 2]])
Enter fullscreen mode Exit fullscreen mode

Calculate the mean of the flattened array

Use the numpy.mean() function to calculate the mean of the flattened p array.

mean_flattened = np.mean(p)
print("The mean of the array when axis = None : ", mean_flattened)
Enter fullscreen mode Exit fullscreen mode

Calculate the mean along axis 0

Use the numpy.mean() function to calculate the mean of the p array along the axis 0.

mean_axis_0 = np.mean(p, axis = 0)
print("The mean of the array when axis = 0 : ", mean_axis_0)
Enter fullscreen mode Exit fullscreen mode

Calculate the mean along axis 1

Use the numpy.mean() function to calculate the mean of the p array along the axis 1.

mean_axis_1 = np.mean(p, axis = 1)
print("The mean of the array when axis = 1 : ", mean_axis_1)
Enter fullscreen mode Exit fullscreen mode

Out parameter

Use the numpy.mean() function with the out parameter to place the result in an alternative array.

out_arr = np.arange(3)
print("out_arr : ", out_arr)
print("Mean of arr, axis = 1: ", np.mean(p, axis = 1, out = out_arr))
Enter fullscreen mode Exit fullscreen mode

Summary

In this tutorial, we covered the numpy.mean() function from the NumPy library. We explained what mean is, the syntax of the mean() function, and its parameters. We also provided step-by-step examples of using this function on both one-dimensional and two-dimensional arrays.


πŸš€ Practice Now: NumPy Array Mean Calculation


Want to Learn More?

Top comments (0)