Program to Calculate the Standard Deviation

Python Program to Calculate the Standard Deviation

Standard Deviation:

In statistics, the standard deviation is a measure of spread. It’s a metric for quantifying the spread or variance of a group of data values. It is quite similar to variance in that it delivers the deviation measure, whereas variance offers the squared value.

A low Standard Deviation value implies that the data are more evenly distributed, whereas a high number suggests that the data in a set are dispersed from their mean average values. The standard deviation has the advantage of being expressed in the same units as the data, unlike the variance.

Given a list of numbers , the task is to calculate the standard deviation of the given list in python.

Examples:

Example1:

Input:

given list = [34, 14, 7, 13, 26, 22, 12, 19,29, 33, 31, 30, 20, 10, 9, 27, 31, 24]

Output:

The given list of numbers : 
34 14 7 13 26 22 12 19 29 33 31 30 20 10 9 27 31 24 
Standard deviation of the given list = 9.008529001520827

Example2:

Input:

given list = [4, 7, 1, 2, 17, 19, 11, 19, 18, 13, 5, 3, 17]

Output:

The given list of numbers : 
4 7 1 2 17 19 11 19 18 13 5 3 17 
Standard deviation of the given list = 7.042908781360447

Example3:

Input:

given list = [12, 1, 4, 5, 19, 11, 13, 11, 12, 5, 5, 6, 7, 10, 15, 14]

Output:

The given list of numbers : 
12 1 4 5 19 11 13 11 12 5 5 6 7 10 15 14 
Standard deviation of the given list = 4.8425200051213

Python Program to Calculate standard deviation in Python

There are several ways to calculate the standard deviation in python some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using stdev() function in statistics package

In Python, the statistics package has a function called stdev() that can be used to determine the standard deviation. The stdev() function estimates standard deviation from a sample of data instead of the complete population.
stdev ( [data-set], xbar )

Parameters:

[data] : An iterable with real-valued numbers as parameters.
xbar (Optional): Uses the data set's actual mean as a value.

Return type:

Returns the real standard deviation of the values supplied as parameters.

Exceptions :

  • For data-sets with fewer than two values supplied as parameters, StatisticsError is thrown.
  • When the value provided as xbar does not match the actual mean of the data-set, the value is impossible/precision-less.

We’ve built a list and applied the standard deviation operation to the data values in the following example:

Below is the implementation:

# importing statistics
import statistics
# given list
given_list = [34, 14, 7, 13, 26, 22, 12, 19,
              29, 33, 31, 30, 20, 10, 9, 27, 31, 24]
# using stdev() fun to calculate standard devaition
standarddevList = statistics.stdev(given_list)
# print the given list and standard deviation
print("The given list of numbers : ")
for i in given_list:
    print(i, end=" ")
# printing new empty line
print()
# printing the standard deviation of the given list of numbers
print("Standard deviation of the given list =", standarddevList)

Output:

The given list of numbers : 
34 14 7 13 26 22 12 19 29 33 31 30 20 10 9 27 31 24 
Standard deviation of the given list = 9.008529001520827

Method #2:Using std() function in NumPy module

The NumPy module provides us with a number of functions for dealing with and manipulating numeric data items.

Python’s numpy package includes a function named numpy.std() that computes the standard deviation along the provided axis. This function returns the array items’ standard deviation. The standard deviation is defined as the square root of the average square deviation (calculated from the mean). The standard deviation for the flattened array is calculated by default. The average square deviation is generally calculated using x.sum()/N, where N=len (x).

The standard deviation for a range of values can be calculated using the numpy.std() function, as demonstrated below.

Below is the implementation:

# importing numpy
import numpy as np
# given list
given_list = [34, 14, 7, 13, 26, 22, 12, 19,
              29, 33, 31, 30, 20, 10, 9, 27, 31, 24]
# using std() fun to calculate standard devaition
standarddevList = np.std(given_list)
# print the given list and standard deviation
print("The given list of numbers : ")
for i in given_list:
    print(i, end=" ")
# printing new empty line
print()
# printing the standard deviation of the given list of numbers
print("Standard deviation of the given list =", standarddevList)

Output:

The given list of numbers : 
34 14 7 13 26 22 12 19 29 33 31 30 20 10 9 27 31 24 
Standard deviation of the given list = 8.754716541864452

Method #3:Using std() in pandas Module

The Pandas module allows us to work with a bigger number of datasets and gives us with a variety of functions to apply to them.

We may conduct different statistics operations on the data values using the Pandas module, one of which is standard deviation, as shown below

Below is the implementation:

# importing pandas
import pandas as pd
# given list
given_list = [34, 14, 7, 13, 26, 22, 12, 19,
              29, 33, 31, 30, 20, 10, 9, 27, 31, 24]
# converting the given_list to DataFrame
dataList = pd.DataFrame(given_list)
# calculating the  standard devaition using std() function
standarddevList = dataList.std()

# print the given list and standard deviation
print("The given list of numbers : ")
for i in given_list:
    print(i, end=" ")
# printing new empty line
print()
# printing the standard deviation of the given list of numbers
print("Standard deviation of the given list =", standarddevList)

Output:

The given list of numbers : 
34 14 7 13 26 22 12 19 29 33 31 30 20 10 9 27 31 24 
Standard deviation of the given list = 0 9.008529
dtype: float64

Explanation:

In this example, we built a list and then used the pandas.dataframe() function to convert the list into a data frame. Using the std() function, we calculated the standard deviation of the values in the data frame.
Related Programs: