Get Standard deviation of a List in Python

In this article, we will discuss different ways to get the Standard Deviation of numbers in a List in Python.

Table Of Contents

Method 1: Using Custom Function

In this solution, we will create a custom function to calculate the Standard Deviation of a List of Numbers in Python. In side this function, fist we will compute the mean of numbers. Then using the mean, we will calculate the variance of numbers. Once we have the variance, then its square root will give us the standard deviation of numbers. Let’s see an example,

# Get Standard Deviation Of Numbers in a List
def GetStandardDeviation(listOfNumbers):
    # get mean of numbers
    mean = sum(listOfNumbers) / len(listOfNumbers)
    # Get variance
    variance = sum([((num - mean) ** 2) for num in listOfNumbers]) / (len(listOfNumbers)-1)
    return variance ** 0.5

numbers = [940, 898, 635, 208, 804, 873, 553, 825, 346, 189]

# Get Standard Deviation Of Numbers in a List
stdDeviation = GetStandardDeviation(numbers)

print(stdDeviation)

Output:

289.5139413261859

It returned the standard deviation of numbers.

Advertisements

We can modify the above function a little to get the population standard deviation. Let’s see an example,

# Get Population Standard Deviation Of Numbers in a List
def GetPopulationStandardDeviation(listOfNumbers):
    # get mean of numbers
    mean = sum(listOfNumbers) / len(listOfNumbers)
    # Get variance
    variance = sum([((num - mean) ** 2) for num in listOfNumbers]) / len(listOfNumbers)
    return variance ** 0.5

numbers = [940, 898, 635, 208, 804, 873, 553, 825, 346, 189]

# Get Standard Pouplation Deviation Of Numbers in a List
stdDeviation = GetPopulationStandardDeviation(numbers)

print(stdDeviation)

Output:

274.657040688929

It returned the population standard deviation of numbers.

Method 2: Using statistics module

We can also use the stdev() method of statistics module, to get the Standard Deviation Of Numbers in a List. The stdev() function accepts a list of numbers as argument, and returns the square root of the sample variance. Let’s see an example,

import statistics

numbers = [940, 898, 635, 208, 804, 873, 553, 825, 346, 189]

# Get Standard Deviation Of Numbers in a List
stdDeviation = statistics.stdev(numbers)

print(stdDeviation)

Output:

289.51394132618594

It returned the standard deviation of numbers.

We can also use the pstdev() method of the statistics module to get the population standard deviation of numbers in a list. Let’s see an example,

import statistics

numbers = [940, 898, 635, 208, 804, 873, 553, 825, 346, 189]

# Get Population Standard Deviation Of Numbers in a List
stdDeviation = statistics.pstdev(numbers)

print(stdDeviation)

Output:

274.657040688929

It returned the population standard deviation of numbers.

Method 3: Using NumPy

We can also use the std() function of NumPy module in Python, to compute the standard deviation along the specified axis of the array. We can pass our list, to the numpy.std() function to get the standard deviation of numbers in list. Let’s see an example,

import numpy as np

numbers = [940, 898, 635, 208, 804, 873, 553, 825, 346, 189]

# Get Standard Deviation Of Numbers in a List
stdDeviation = np.std(numbers, ddof=1)

print(stdDeviation)

Output:

289.51394132618594

It returned the standard deviation of numbers.

We can use the numpy.std() method with default value of ddof parameter, to get the population standard deviation of numbers in a list. Let’s see an example,

import numpy as np

numbers = [940, 898, 635, 208, 804, 873, 553, 825, 346, 189]

# Get Population Standard Deviation Of Numbers in a List
stdDeviation = np.std(numbers)

print(stdDeviation)

Output:

274.657040688929

It returned the population standard deviation of numbers.

Summary

We learned about three different ways to calculate the standard deviation of numbers in a list in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top