0
0
NumPydata~5 mins

np.std() and np.var() for spread in NumPy

Choose your learning style9 modes available
Introduction

We use np.std() and np.var() to understand how data points spread out from the average. This helps us see if data is close together or very different.

Checking how consistent students' test scores are in a class.
Measuring how much daily temperatures change in a week.
Understanding the variation in sales numbers for a product.
Comparing the spread of heights in two different groups of people.
Syntax
NumPy
np.std(array, axis=None, ddof=0)
np.var(array, axis=None, ddof=0)

array is your data, like a list or NumPy array.

axis decides if you calculate spread for the whole data or along rows/columns.

Examples
Calculates the standard deviation of all numbers in the list.
NumPy
np.std([1, 2, 3, 4, 5])
Calculates the variance of all numbers in the list.
NumPy
np.var([1, 2, 3, 4, 5])
Calculates standard deviation for each column.
NumPy
np.std([[1, 2], [3, 4]], axis=0)
Calculates variance for each row.
NumPy
np.var([[1, 2], [3, 4]], axis=1)
Sample Program

This program calculates how much daily sales numbers spread out from the average sales. Standard deviation and variance give us this spread in different ways.

NumPy
import numpy as np

# Sample data: daily sales for a week
sales = np.array([100, 120, 90, 110, 105, 115, 95])

# Calculate standard deviation
std_dev = np.std(sales)

# Calculate variance
variance = np.var(sales)

print(f"Standard Deviation: {std_dev:.2f}")
print(f"Variance: {variance:.2f}")
OutputSuccess
Important Notes

Standard deviation is the square root of variance.

Use ddof=1 if you want sample standard deviation or variance.

Lower spread means data points are close to the average; higher means more spread out.

Summary

np.std() finds how much data varies around the average in original units.

np.var() finds the average squared difference from the mean.

Both help understand data consistency and variation.