DEV Community

Cover image for Implementation of Mean, Variance, and Standard Deviation
Shlok Kumar
Shlok Kumar

Posted on

1

Implementation of Mean, Variance, and Standard Deviation

In this section, we will implement the calculations of mean, variance, and standard deviation using Python's NumPy library. This practical example will help you understand how to apply these statistical concepts to actual datasets.

Mean

The mean is a measure that represents the central or typical value in a dataset. It is calculated by summing all the values and dividing by the count of those values.

Mean in Python Using NumPy

You can calculate the mean using the numpy.average() function:

import numpy as np

# Dataset
data = [2, 4, 4, 4, 5, 5, 7, 9]

# Calculating mean
mean = np.average(data)
print("Mean:", mean)  # Output: 5.0
Enter fullscreen mode Exit fullscreen mode

Variance

Variance measures the dispersion of data points from the mean, indicating how much the values in a dataset differ from the mean.

Variance in Python Using NumPy

To calculate variance in Python, use the numpy.var() function:

# Calculating variance
variance = np.var(data)
print("Variance:", variance)  # Output: 4.0
Enter fullscreen mode Exit fullscreen mode

Standard Deviation

Standard deviation is the square root of the variance and indicates the extent to which data varies from the mean.

Standard Deviation in Python Using NumPy

You can calculate the standard deviation using the numpy.std() function:

# Calculating standard deviation
std_deviation = np.std(data)
print("Standard Deviation:", std_deviation)  # Output: 2.0
Enter fullscreen mode Exit fullscreen mode

Full Implementation Example

Here’s how the complete implementation looks:

import numpy as np

# Dataset
data = [2, 4, 4, 4, 5, 5, 7, 9]

# Calculating mean
mean = np.average(data)
print("Mean:", mean)  # Output: 5.0

# Calculating variance
variance = np.var(data)
print("Variance:", variance)  # Output: 4.0

# Calculating standard deviation
std_deviation = np.std(data)
print("Standard Deviation:", std_deviation)  # Output: 2.0
Enter fullscreen mode Exit fullscreen mode

This Python implementation demonstrates how easily you can compute mean, variance, and standard deviation using NumPy, making it a valuable tool for data analysis in machine learning and other scientific applications.

For more content, follow me at —  https://linktr.ee/shlokkumar2303

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If you found this article helpful, please give a ❤️ or share a friendly comment!

Got it