Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

Statistical Functions in NumPy

Learn descriptive statistics in NumPy including mean, median, variance, standard deviation, percentiles, and correlation.

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

Python
import numpy as np
 
data = np.array([12, 15, 14, 10, 25, 19, 18])

Mean, median

Python
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

Python
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 - min

Variance and standard deviation

Python
import numpy as np
 
data = np.array([12, 15, 14, 10, 25, 19, 18])
 
print(np.var(data))
print(np.std(data))

Percentiles / quantiles

Python
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))
diagram From raw data to a summary mermaid
A handful of NumPy reductions turn a raw array into the descriptive statistics analysts reach for first.

Working across axes (2D)

Python
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 row

Correlation and covariance

Python
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

quizCheck yourself
  1. For data = np.array([12, 15, 14, 10, 25, 19, 18]), what is np.median(data)?

    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.

  2. What does np.ptp(data) return for the same data?

    Show answer

    A โ€” 15 โ€” ptp is peak to peak, max minus min, which is 25 - 10 = 15.

  3. For mat = np.array([[1, 2, 3], [4, 5, 6]]), what does np.mean(mat, axis=1) return?

    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].

  4. For x = [1, 2, 3, 4, 5] and y = [2, 4, 6, 8, 10], what is the correlation np.corrcoef(x, y) between them?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Standard Deviation

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Median via the 50th Percentile

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did