liveBook · Manning

Discretization

As explained in Grokking Statistics

Binning is the process of grouping continuous or discrete observations into intervals and summarizing each interval.

Binning groups observations into intervals, then summarizes each interval. For example, bird-strike records can be grouped into 100-foot height ranges and counted. section 7.12

Bird strikes by altitude

A dataset records each bird strike, but not a ready-made count of strikes at each altitude. Creating 100-foot HEIGHT bins produces one count per interval, which can be plotted and analyzed. The resulting Pearson correlation is weak before transformation, while extreme near-ground outliers challenge the assumptions of linear analysis. section 7.12

A practical binning workflow uses:

  • Remove records with missing height values. section 7.12
  • Create 100-foot intervals with pandas.cut() and count records in each interval. section 7.12
  • Plot the counts before applying correlation or regression, and inspect skew and outliers first. section 7.12
import pandas as pd
import numpy as np
from scipy.stats import linregress
import matplotlib.pyplot as plt

df = pd.read_csv('birdstrike.csv')

df = df[df['HEIGHT'].isna() == False]  #1

max_height = int(df['HEIGHT'].max() \
    // 100 + 1) * 100  #2

bins = list(range(0, max_height + 100, 100))  #2
df['HEIGHT_BIN'] = pd.cut(df['HEIGHT'], 
    bins=bins, right=False)  #2

incidents_by_bin = df['HEIGHT_BIN']
    .value_counts().sort_index()  #2

x = np.arange(start=0, stop=max_height, step=100)  #3
y = incidents_by_bin.to_numpy()  #3

slope, intercept, r_value, p_value, std_err \
    = linregress(x, y)  #4

x_line = np.linspace(min(x), max(x), 100)  #5
y_line = slope * x_line + intercept  #5

plt.scatter(x, y, label='Data points')  #6

plt.plot(x_line,  #7
         y_line,  #7
         color='red',  #7
         label=f'Fit: y = {slope:.2f}x '
     f'{'-' if intercept < 0 else '+'}'
     f' {abs(intercept):.2f}')#7

plt.title("Bird Strikes by Height")  #8
plt.xlabel("Bird Strike Height (feet)")  #8
plt.ylabel("Bird Strike Count")  #8
plt.legend()  #8

plt.show()  #9

print(f"Slope: {slope:.8f}")  #10
print(f"Intercept: {intercept:.8f}")  #10
print(f"Pearson Correlation: {r_value:.4f}")  #10
print(f"P-value (for slope): {p_value:.10f}")  #10
print(f"Standard error of slope: {std_err:.4f}")  #10

"""
Slope: -0.03924224
Intercept: 851.05028927
Pearson Correlation: -0.1672
P-value (for slope): 0.0026550029
Standard error of slope: 0.0130
"""

Python code demonstrates interval construction, counting, plotting, and linear regression on binned observations. section 7.12

How Grokking Statistics covers it

How deep it goes
Demonstrates interval construction and counting, then stops short of endorsing linear analysis when outliers violate assumptions.
Examples
Uses airplane bird-strike records grouped into 100-foot altitude intervals and plotted against incident counts.
What it assumes
Assumes basic tabular data handling and introduces the missing dependent-variable problem through a concrete dataset.
What you can do after
Apply it via pandas to create intervals, count observations, visualize the result, and question subsequent model choices.
How it connects
Connects binning with Pearson correlation, linear regression, outliers, and logarithmic transformation.

Also explained in

Compare how 5 books explain discretization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Statistics. The section links point to the parts of the book it draws on.