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

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ•’

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more