0
0
NumPydata~10 mins

np.std() and np.var() for spread in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.std() and np.var() for spread
Start with data array
Calculate mean
Calculate deviations from mean
Square deviations (for variance)
Average squared deviations = variance
Square root of variance = std deviation
Output variance and std deviation
This flow shows how variance and standard deviation measure data spread by first finding the mean, then deviations, then variance, and finally standard deviation.
Execution Sample
NumPy
import numpy as np
arr = np.array([2, 4, 4, 4, 5, 5, 7, 9])
var = np.var(arr)
std = np.std(arr)
print(var, std)
Calculate variance and standard deviation of a simple data array to measure spread.
Execution Table
StepActionIntermediate ValueResult
1Input array[2, 4, 4, 4, 5, 5, 7, 9]Data ready
2Calculate meanmean = (2+4+4+4+5+5+7+9)/8mean = 5.0
3Calculate deviationsdeviations = arr - mean[-3, -1, -1, -1, 0, 0, 2, 4]
4Square deviationssquared = deviations ** 2[9, 1, 1, 1, 0, 0, 4, 16]
5Calculate variancevariance = mean of squaredvariance = 4.0
6Calculate std deviationstd = sqrt(variance)std = 2.0
7Outputvariance and std deviation4.0 and 2.0
💡 All steps complete, variance and std deviation calculated
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
arr[2,4,4,4,5,5,7,9][2,4,4,4,5,5,7,9][2,4,4,4,5,5,7,9][2,4,4,4,5,5,7,9][2,4,4,4,5,5,7,9][2,4,4,4,5,5,7,9][2,4,4,4,5,5,7,9]
meanN/A5.05.05.05.05.05.0
deviationsN/AN/A[-3, -1, -1, -1, 0, 0, 2, 4][-3, -1, -1, -1, 0, 0, 2, 4][-3, -1, -1, -1, 0, 0, 2, 4][-3, -1, -1, -1, 0, 0, 2, 4][-3, -1, -1, -1, 0, 0, 2, 4]
squaredN/AN/AN/A[9, 1, 1, 1, 0, 0, 4, 16][9, 1, 1, 1, 0, 0, 4, 16][9, 1, 1, 1, 0, 0, 4, 16][9, 1, 1, 1, 0, 0, 4, 16]
varianceN/AN/AN/AN/A4.04.04.0
stdN/AN/AN/AN/AN/A2.02.0
Key Moments - 3 Insights
Why do we square the deviations before averaging for variance?
Squaring makes all deviations positive and emphasizes larger differences, as shown in step 4 of the execution_table.
Why is standard deviation the square root of variance?
Standard deviation is in the same units as the original data, unlike variance which is squared units, as shown in step 6.
What does a higher variance or std deviation mean about the data?
It means data points are more spread out from the mean, which you can see from the larger squared deviations in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the deviations array?
A[-3, -1, -1, -1, 0, 0, 2, 4]
B[3, 1, 1, 1, 0, 0, -2, -4]
C[2, 4, 4, 4, 5, 5, 7, 9]
D[9, 1, 1, 1, 0, 0, 4, 16]
💡 Hint
Check the 'Intermediate Value' column for step 3 in execution_table.
At which step does the variance get calculated?
AStep 4
BStep 6
CStep 5
DStep 3
💡 Hint
Look for 'Calculate variance' in the 'Action' column of execution_table.
If the data array had all identical values, what would np.var(arr) return?
AA positive number
BZero
CNegative number
DCannot be calculated
💡 Hint
Variance measures spread; no spread means zero variance, see variable_tracker for variance values.
Concept Snapshot
np.var(array) calculates variance = average squared deviation from mean
np.std(array) calculates standard deviation = sqrt(variance)
Variance shows spread in squared units
Std deviation shows spread in original units
Higher values mean more spread out data
Use these to understand data variability
Full Transcript
This lesson shows how numpy's np.var() and np.std() functions measure data spread. We start with a data array, calculate its mean, then find how far each value is from the mean (deviations). We square these deviations to avoid negatives and emphasize bigger differences, then average them to get variance. Taking the square root of variance gives standard deviation, which is easier to interpret because it uses the same units as the data. The execution table traces each step with values, and the variable tracker shows how variables change. Key moments clarify why we square deviations and why std deviation is the square root of variance. The quiz tests understanding of these steps and concepts.