Skip to Content

Get the Mean of NumPy Array – (With Examples)

The Numpy library in Python comes with a number of useful built-in functions for computing common descriptive statistics like mean, median, standard deviation, etc. In this tutorial, we will look at how to get the mean of a Numpy array with the help of some examples.

How do you get the mean of an array in Numpy?

You can use the Numpy mean() function to get the mean of a Numpy array. Pass the array as an argument.

The following is the syntax –

# mean of all values in array
numpy.mean(ar)

It returns the average of the values in the array. For multi-dimensional arrays, you can specify the axis along which you want to compute the mean (see the examples below).

Examples

Let’s now look at some examples of using the above syntax on single and multi-dimensional arrays.

Example 1 – Mean of a one-dimensional Numpy array

get the mean of a numpy array

Let’s first create a one-dimensional Numpy array.

import numpy as np

# create numpy array
ar = np.array([1, 2, 3, 4])
# display the array
print(ar)

Output:

[1 2 3 4]

Here, we used the numpy.array() function to create a one-dimensional array containing some numeric values.

Let’s now get the average of all the values in the above array.

# mean of array
print(np.mean(ar))

Output:

2.5

We get the mean as 2.5 which is the correct answer as (1+2+3+4)/4 = 2.5

Example 2 – Mean of multi-dimensional Numpy array

First, let’s create a 2-D Numpy array.

# create 2-D numpy array
ar = np.array([[1, 2, 3],
               [4, 5, 6]])
# display the array
print(ar)

Output:

[[1 2 3]
 [4 5 6]]

Here, we used the numpy.array() function to create a Numpy array with two rows and three columns.

If you use the Numpy mean() function on an array without specifying the axis, it will return the mean of all the values inside the array.

# mean of array
print(np.mean(ar))

Output:

3.5

We get the mean of all the values inside the 2-D array.

Use the numpy.mean() function with axis=1 to get the mean value for each row in the array.

# mean of each row in array
print(np.mean(ar, axis=1))

Output:

[2. 5.]

We get the mean of each row in the above 2-D array. The mean of values in the first row is (1+2+3)/3 = 2 and the mean of values in the second row is (4+5+6)/3 = 3.

Use the numpy.mean() function with axis=0 to get the mean of each column in the array.

# mean of each column in array
print(np.mean(ar, axis=0))

Output:

[2.5 3.5 4.5]

We get the mean of each column in the above 2-D array. The mean of values – in the first column is (1+4)/2 = 2.5, in the second column is (2+5)/2 = 3.5, and in the third column is (3+6)/2 = 4.5.

Summary

In this tutorial, we looked at how to use the numpy.mean() function to get the average of values in an array. The following are the key takeaways from this tutorial.

  • Use the numpy.mean() function without any arguments to get the average of all the values inside the array.
  • For multi-dimensional arrays, use the axis parameter to specify the axis along which to compute the mean. For example, for a 2-D array –
    • Pass axis=1 to get the mean of each row.
    • Pass axis=0 to get the mean of each column.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.