NumPy has fast functions for summarising data: mean, median, standard deviation, percentiles, correlation and more. The axis argument lets you calculate them per column or per row.
What You Will Learn
- mean, median, std and var
- The axis argument
- min, max, argmax and ptp
- Percentiles and histograms
- Correlation and covariance
- Handling NaN values
Prerequisites
Introduction to NumPy and Descriptive Statistics.
Data
import numpy as np
marks = np.array([[78, 85, 62],
[90, 72, 88],
[55, 95, 70],
[82, 64, 91]]) # 4 students x 3 subjects
Averages and Spread
print(marks.mean(), marks.mean(axis=0), marks.mean(axis=1))
print(np.median(marks), marks.std().round(2), marks.var(axis=0).round(2))
77.66666666666667 [76.25 79. 77.75] [75. 83.33333333 73.33333333 79. ] 80.0 12.41 [169.19 141.5 147.19]
Explanation: No axis = whole array. axis=0 goes down the rows and gives one value per column (subject averages); axis=1 goes across and gives one value per row (student averages). np.std uses the population formula by default; pass ddof=1 for the sample version that pandas uses.
Extremes
print(marks.min(), marks.max(axis=1), marks.argmax(axis=0))
print(np.ptp(marks, axis=0), marks.sum(axis=0), marks.cumsum(axis=1)[0])
55 [85 90 95 91] [1 2 3] [35 31 29] [305 316 311] [ 78 163 225]
Explanation: argmax(axis=0) gives the row index of the top scorer in each subject (students 1, 2 and 3). ptp (“peak to peak”) is max − min. cumsum gives running totals.
Percentiles and Histogram
print(np.percentile(marks, [25, 50, 75]))
print(np.histogram(marks, bins=[50, 60, 70, 80, 90, 100]))
[68.5 80. 88.5] (array([1, 2, 3, 3, 3]), array([ 50, 60, 70, 80, 90, 100]))
Explanation: The histogram counts how many marks fall in each bin: 1 in 50–60, 2 in 60–70, and so on.
Correlation and Covariance
x = np.array([2, 4, 6, 8, 10])
y = np.array([3, 5, 7, 11, 13])
print(np.corrcoef(x, y)[0, 1].round(4), np.cov(x, y)[0, 1])
0.9912 13.0
Explanation: A correlation of 0.99 means a very strong positive linear relationship. corrcoef and cov return 2×2 matrices; [0, 1] is the value between x and y.
Missing Values and Counts
d = np.array([1.0, np.nan, 3.0])
print(np.mean(d), np.nanmean(d))
vals, counts = np.unique(np.array([3, 1, 3, 2, 3, 1]), return_counts=True)
print(vals, counts)
nan 2.0 [1 2 3] [2 1 3]
Explanation: One NaN makes the ordinary mean NaN; the nan* versions (nanmean, nanstd, nanmax…) ignore missing values.
| Function | Returns |
|---|---|
| mean, median | Average, middle value |
| std, var | Spread (ddof=1 for sample) |
| min, max, argmin, argmax | Extremes and their positions |
| percentile, quantile | Value below which a % of data lies |
| corrcoef, cov | Relationship between variables |
| nanmean, nanstd… | Same, ignoring NaN |
Series
NumPy series: Introduction · Creating Arrays · Data Types · Slicing & Indexing · Array Manipulation · Broadcasting · Math Functions · Statistical Functions · Random · Linear Algebra · NumPy vs Pandas
Interview Questions
Q: What does axis=0 mean?
Compute along the rows, giving one result per column.
Q: np.std vs pandas std?
NumPy uses ddof=0 (population) by default, pandas uses ddof=1 (sample).
Practice Exercises
- Find the student with the highest total marks.
- Compute the 90th percentile of each subject.
Summary
NumPy’s statistical functions summarise arrays quickly; use axis for per-column or per-row results and nan-functions when data has missing values.
Next: NumPy vs Pandas
Machine Learning Tutorials, Courses and Certifications