Skip to content
Related Articles

Related Articles

Calculate standard deviation of a Matrix in Python
  • Last Updated : 26 Nov, 2020

In this article we will learn how to calculate standard deviation of a Matrix using Python.

Standard deviation is used to measure the spread of values within the dataset. It indicates variations or dispersion of values in the dataset and also helps to determine the confidence in a model’s statistical conclusions. It is represented by the sigma (σ) and calculates by taking the square root of the variance. If the standard deviation is low it means most of the values are closer to the mean and if high, that means closer to the mean. In this article, we will learn what are the different ways to calculate SD in Python.

We can calculate the Standard Deviation using the following method : 

  1. std() method in NumPy package
  2. stdev() method in Statistics package

Method 1:std() method in NumPy package.

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

# import required packages
import numpy as np
  
# Create matrix
matrix = np.array([[33, 55, 66, 74], [23, 45, 65, 27],
                  [87, 96, 34, 54]])
  
print("Your matrix:\n", matrix)
  
# use std() method
sd = np.std(matrix)
print("Standard Deviation :\n", sd)

chevron_right


Output :

Your matrix:
[[33 55 66 74]
[23 45 65 27]
[87 96 34 54]]
Standard Deviation :
22.584870796373593

Method 2: stdev() method in Statistics package.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import statistics
  
  
statistics.stdev([11, 43, 56, 77, 87, 45, 67, 33])

chevron_right


Output :

24.67466890789592

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :