np.mean() Function of NumPy Library in Python (3 Examples)

In this Python programming tutorial you’ll learn how to use the mean function of the NumPy library.

Setting up the Examples

import numpy            # Load numpy
x = np.array([[8, 2, 1, 7, 7, 5],    # Constructing a NumPy array in Python
              [4, 10, 5, 9, 1, 4]])
print(x)
# [[ 8  2  1  7  7  5]
#  [ 4 10  5  9  1  4]]

Example 1: Calculate Mean of All Array Values in Python

print(np.mean(x))                    # Computing the mean of all values
# 5.25

Example 2: Calculate Mean by Array Rows in Python

print(np.mean(x, axis = 1))          # Computing the mean by rows
# [5.  5.5]

Example 3: Calculate Mean by Array Columns in Python

print(np.mean(x, axis = 0))          # Computing the mean by columns
# [6.  6.  3.  8.  4.  4.5]

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top