Menu

Measures of Central Tendency and Variability

Measures of Central Tendency and Variability

Measures of central tendency describe a typical value in a dataset. Measures of variability describe how widely the observations are spread.

Mean, Median, and Mode

The mean is calculated by adding all values and dividing by the number of observations.

The median is the middle value after sorting the data. If there is an even number of observations, it is the average of the two middle values.

The mode is the most frequently occurring value.

from scipy import stats

salaries = [30000, 32000, 35000, 35000, 38000, 120000]

print("Mean:", sum(salaries) / len(salaries))

print("Median:", stats.median(salaries))

print("Mode:", stats.mode(salaries, keepdims=False).mode)

The unusually high salary increases the mean, while the median remains closer to the center of most observations. This illustrates why the median can be more representative when a dataset contains extreme values.

Range, Variance, and Standard Deviation

The range is the difference between the maximum and minimum values. Variance measures the average squared deviation from the mean, while standard deviation is the square root of variance.

import numpy as np

scores = np.array([60, 65, 70, 75, 80])

print("Range:", np.ptp(scores))

print("Population variance:", np.var(scores))

print("Population standard deviation:", np.std(scores))

NumPy calculates population variance and standard deviation by default. When working with a sample, use np.var(scores, ddof=1) and np.std(scores, ddof=1) to calculate their sample versions.

Quartiles and the Interquartile Range

Quartiles divide ordered data into four sections. The interquartile range (IQR) is the difference between the third and first quartiles.

scores = np.array([45, 50, 55, 60, 65, 70, 75, 90])

q1 = np.percentile(scores, 25)

q3 = np.percentile(scores, 75)

iqr = q3 - q1

print("First quartile:", q1)

print("Third quartile:", q3)

print("Interquartile range:", iqr)

The IQR describes the spread of the middle 50% of observations and is less sensitive to extreme values than the full range.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details