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.

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.

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.

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

Scroll to Top