Python Standard Deviation Without Numpy

Spread the love

Today we will discuss python standard deviation without numpy 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.

Python Standard Deviation Without Numpy

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.

Most Important Applications Of Standard Deviation

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.

test_list = [3, 6, 9, 11, 13]
print("The original list : " + str(test_list))
mean = sum(test_list) / len(test_list)
variance = sum([((x - mean) ** 2) for x in test_list]) / len(test_list)
res = variance ** 0.5
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.

<code class="comments">
# Using pstdev()</code><code class="keyword">
import</code> <code class="plain">statistics</code><code class="comments"></code><code class="plain">
test_list </code><code class="keyword">=</code> <code class="plain">[</code><code class="value">4</code><code class="plain">, </code><code class="value">5</code><code class="plain">, </code><code class="value">8</code><code class="plain">, </code><code class="value">9</code><code class="plain">, </code><code class="value">10</code><code class="plain">]</code><code class="keyword">
print</code><code class="plain">(</code><code class="string">"The original list : "</code> <code class="keyword">+</code> <code class="functions">str</code><code class="plain">(test_list))</code>
<code class="plain">res </code><code class="keyword">=</code> <code class="plain">statistics.pstdev(test_list)
</code><code class="functions">print</code><code class="plain">(</code><code class="string">"Standard deviation of sample is : "</code> <code class="keyword">+</code> <code class="functions">str</code><code class="plain">(res))</code>

Read More: 10 best Ways to Use Python For Loop

Leave a Comment