Why statistics in NumPy?
Quick descriptive stats help you:
- Understand distributions
- Detect outliers
- Summarize data before modeling/visualization
These aggregation methods (also called reductions) are available both as array methods (arr.mean()) and as top-level NumPy functions (np.mean(arr)) โ they do the same thing.
Sample data
import numpy as np
data = np.array([12, 15, 14, 10, 25, 19, 18])Mean, median
import numpy as np
data = np.array([12, 15, 14, 10, 25, 19, 18])
print(np.mean(data))
print(np.median(data))Min, max, range
import numpy as np
data = np.array([12, 15, 14, 10, 25, 19, 18])
print(np.min(data))
print(np.max(data))
print(np.ptp(data)) # peak-to-peak = max - minVariance and standard deviation
import numpy as np
data = np.array([12, 15, 14, 10, 25, 19, 18])
print(np.var(data))
print(np.std(data))Percentiles / quantiles
import numpy as np
data = np.array([12, 15, 14, 10, 25, 19, 18])
print(np.percentile(data, 25))
print(np.percentile(data, 50))
print(np.percentile(data, 75))flowchart LR A["Raw array"] --> B["Center: mean, median"] A --> C["Spread: var, std, ptp"] A --> D["Position: percentile, quantile"] A --> E["Relationship (2 arrays):
cov, corrcoef"]
Working across axes (2D)
import numpy as np
mat = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(np.mean(mat, axis=0)) # per column
print(np.mean(mat, axis=1)) # per rowCorrelation and covariance
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])
cov = np.cov(x, y)
print("cov:\n", cov)
corr = np.corrcoef(x, y)
print("corr:\n", corr)Check your understanding
For data = np.array([12, 15, 14, 10, 25, 19, 18]), what is np.median(data)?
Sorted, the values are 10, 12, 14, 15, 18, 19, 25, so the middle one is 15. The mean is about 16.14.
Show answer
B โ 15.0 โ Sorted, the values are 10, 12, 14, 15, 18, 19, 25, so the middle one is 15. The mean is about 16.14.
What does np.ptp(data) return for the same data?
ptp is peak to peak, max minus min, which is 25 - 10 = 15.
Show answer
A โ 15 โ ptp is peak to peak, max minus min, which is 25 - 10 = 15.
For mat = np.array([[1, 2, 3], [4, 5, 6]]), what does np.mean(mat, axis=1) return?
axis=1 averages across each row: (1+2+3)/3 = 2 and (4+5+6)/3 = 5. axis=0 gives the per-column means [2.5 3.5 4.5].
Show answer
B โ [2. 5.] โ axis=1 averages across each row: (1+2+3)/3 = 2 and (4+5+6)/3 = 5. axis=0 gives the per-column means [2.5 3.5 4.5].
For x = [1, 2, 3, 4, 5] and y = [2, 4, 6, 8, 10], what is the correlation np.corrcoef(x, y) between them?
y is exactly 2 times x, so the off-diagonal correlation is 1.0. Correlation is always between -1 and 1.
Show answer
B โ 1.0, a perfect positive linear relationship โ y is exactly 2 times x, so the off-diagonal correlation is 1.0. Correlation is always between -1 and 1.
Next
Continue to: Saving and Loading NumPy Data to persist arrays efficiently.
๐งช Try It Yourself
Exercise 1 โ Mean and Median
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Standard Deviation
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ Median via the 50th Percentile
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.