Home » Python standard deviation without numpy

Python standard deviation without numpy

Last Updated on November 1, 2021 by admin

As explained in this article, you can use standard deviation to control whether your data is close to the mean or widely distributed. For example, if an employer wants to determine if one salary in his department is fair to all employees, or if there is a large inequality, the standard deviation can be used.

To do this, find the average salary for that department and calculate the standard deviation. The scenario looks like this: We found that the standard deviation was slightly higher than expected.

Further examination of the data reveals that the majority of employees are within similar salary ranges, but far more are four loyal employees who have been in the department for more than 15 years.

Others earn far more for their long life in the company. In general, a small standard deviation means that the data is closely related to the mean, which makes it more reliable.

A large standard deviation means that there is a large variance between the data and the statistical mean, so this is not the case.

I can’t trust it. … One of the most important applications of standard deviation is to compare two datasets. If the averages of two datasets are the same, that doesn’t necessarily mean they are the same, right?

For example, the averages for datasets 199, 200, 201, and 0, 200, 400 are the same (200).

However, the standard deviation of the first dataset is very small (s = 1) compared to the second dataset (s = 200).

When using math, you may have problems trying to calculate the standard deviation of a sample. It has many uses not only for competitive programming but also for school-wide projects. Let’s look at some ways you can perform this task.

Method #1 : Using sum() + list comprehension

Performing this particular task is an abbreviation for brute force. You can address this issue in the section by taking the mean, variance, and standard deviation as the square roots of the variance.

The sum () is the key to calculating the mean and variance. The concept of lists is used to extend functionality that is common to each item in the list.


# Python3 code to demonstrate the working of
# Standard deviation of list

Using sum() + list comprehension

initializing list

test_list = [4, 5, 8, 9, 10]

# printing list

print("The original list : " + str(test_list))
# Standard deviation of list

# Using sum() + list comprehension

mean = sum(test_list) / len(test_list)

variance = sum([((x - mean) ** 2) for x in test_list]) / len(test_list)

res = variance ** 0.5

# Printing result

print("Standard deviation of sample is : " + str(res))

Method #2 : Using pstdev()

This task can also be performed using the built-in pstdev() feature. This function internally calculates the standard deviation of the sample.


# Python3 code to demonstrate the working of

# Standard deviation of list

# Using pstdev()

import statistics

# initializing list

test_list = [4, 5, 8, 9, 10]

# printing list

print("The original list : " + str(test_list))

# Standard deviation of list

# Using pstdev()

res = statistics.pstdev(test_list)

# Printing result

print("Standard deviation of sample is : " + str(res))

Tags:

Leave a Reply

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