Standard Deviation of NumPy Array in Python – np.std() Function (3 Examples)

In this tutorial, I’ll demonstrate how to find the standard deviation of a NumPy array using the np.std function in the Python programming language.

Creation of Example Data

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 Standard Deviation of All Array Values in Python

print(np.std(x))                     # Computing the standard deviation of all values
# 2.890357532670771

Example 2: Calculate Standard Deviation by Array Rows in Python

print(np.std(x, axis = 1))           # Computing the standard deviation by rows
# [2.64575131 3.09569594]

Example 3: Calculate Standard Deviation by Array Columns in Python

print(np.std(x, axis = 0))           # Computing the standard deviation by columns
# [2.  4.  2.  1.  3.  0.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