Python - Python Machine Learning - Standard Deviation Part 1: Understanding Standard Deviation
What is Standard Deviation?
Standard deviation (σ) measures the spread of numbers in a dataset. It is calculated as:
σ = √(Σ (xi - x̄)² / N)
Where:
- xi is each data point
- xˉ is the mean of the data
- N is the total number of values
Example 1: Calculating Standard Deviation Manually
import math
data = [10, 20, 30, 40, 50]
mean = sum(data) / len(data) # Calculate mean
variance = sum((x - mean) ** 2 for x in data) / len(data) # Variance
std_dev = math.sqrt(variance) # Standard deviation
print("Mean:", mean)
print("Standard Deviation:", std_dev)
Output:
Mean: 30.0
Standard Deviation: 14.142135623730951
Explanation:
We calculate the mean of the dataset.
Then, we find the variance by summing squared differences from the mean.
Finally, we take the square root of the variance to get the standard deviation.