NumPy Array Mean Calculation

PythonPythonBeginner
Practice Now

Introduction

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) numpy(("`NumPy`")) -.-> numpy/MathandStatisticsGroup(["`Math and Statistics`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") numpy/MathandStatisticsGroup -.-> numpy/math_ops("`Math Operations`") numpy/MathandStatisticsGroup -.-> numpy/stats("`Statistical Analysis`") subgraph Lab Skills python/lists -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} python/tuples -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} python/importing_modules -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} python/numerical_computing -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} python/build_in_functions -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} numpy/1d_array -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} numpy/multi_array -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} numpy/data_array -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} numpy/bool_idx -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} numpy/fancy_idx -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} numpy/math_ops -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} numpy/stats -.-> lab-86481{{"`NumPy Array Mean Calculation`"}} end

Import the NumPy library

The first step is to import the NumPy library.

import numpy as np

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])

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)

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]])

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)

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)

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)

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))

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.

Other Python Tutorials you may like