SportsDataTutorials

36 tutorials

Basketball

The NBA's stats API is powerful but picky. Learn to pull it politely, then build standings, ratings and shot charts.

Begin with the ratings-table tutorial to learn the ORtg/DRtg/Net vocabulary the rest of the track speaks, then the home-court and shot-chart builds. The modeling arc — logistic regression, k-NN, cross-validation, ROC curves — uses NBA games as its running example and is the site's most complete from-scratch machine-learning sequence; take it in listed order.

Level:

Pull Your First NBA Data with nba_api

Pull NBA standings with nba_api, with the browser headers and retry logic stats.nba.com demands. Includes exactly what to do when the endpoint refuses to answer.

BasketballBeginner~9 min

A current-standings DataFrame from nba_api, with the proper headers baked in.

Filtering and Querying a DataFrame

Use boolean masks and .query() to select the rows matching multiple conditions, then highlight that filtered set on a chart of every NBA team.

BasketballBeginner~5 min

A two-condition filter that finds the contenders, highlighted on a chart.

Build a Team Net-Rating Dashboard Table

Combine offensive and defensive ratings into a ranked net-rating table, then style it into a dashboard-quality figure you can drop into a report.

BasketballIntermediate~8 min

A ranked net-rating table styled like a real dashboard, exported as an image.

Draw an NBA Shot Chart with matplotlib

Draw a regulation half-court from scratch in matplotlib, then plot a player's makes and misses in court coordinates for a real, shareable shot chart.

BasketballIntermediate~10 min

A half-court drawn in matplotlib with a player's makes and misses plotted on it.

Draw a League-Wide NBA Shot Heatmap

Plot thousands of NBA shots as a hexbin density map on a court you draw yourself, revealing the modern fingerprint: the rim and the three-point line.

BasketballIntermediate~8 min

A hexbin heatmap of where the entire league takes its shots.

Build an Elo Rating System from Scratch

Code Elo in about 20 lines: run a real 2023-24 NBA season in date order and watch the ratings discover the standings on their own, plus a home-court edge.

BasketballIntermediate~6 min

An Elo rating system built in ~20 lines over a real 2023-24 NBA season, with a rating-progression chart and the final-standings ranking it discovers on its own.

Make a Shot-Density Heatmap with hexbin

Turn 25,000 real NBA shots into two hexbin maps — where shots come from, and the make rate by location — exposing the cold mid-range the league abandoned.

BasketballIntermediate~5 min

Two hexbin maps of 25,000 real NBA shots - a density map of where shots come from and an efficiency map of the make rate by location.

Is the Difference Real? A Permutation Test

Home teams scored 2.16 more points a game in 2023-24. Shuffle home/away thousands of times to build the null distribution — the edge is real (p < 0.0001).

BasketballIntermediate~5 min

A permutation test on the NBA home-scoring edge: shuffle home/away thousands of times, build the null distribution, and read a p-value off it.

K-Fold Cross-Validation: A Score You Can Trust

Replace one train/test split with 5-fold cross-validation on the NBA home-win classifier: folds range from 61.8% to 72.4% and average 67.8%. One split can lie.

BasketballAdvanced~5 min

Replace tutorial 72's single train/test split with 5-fold cross-validation - train five times, test on every game exactly once - to get a stable accuracy estimate (67.8%) and, just as important, its spread across folds.

ROC Curves and AUC From Scratch

Sweep the threshold on the NBA home-win classifier to trace a ROC curve, then compute AUC two independent ways — trapezoid area and rank identity — both 0.755.

BasketballAdvanced~6 min

Build a ROC curve and its area (AUC) from scratch in pure numpy for the tutorial-71 home-win classifier: sweep the threshold to trace true-positive vs false-positive rate, then compute AUC two independent ways (trapezoid area and the rank identity) and watch them agree at 0.755.

k-Nearest Neighbors From Scratch

Build k-NN in pure numpy on both teams' net ratings to predict NBA home wins, then sweep k from 1 to 101: k=1 overfits at 55.8%, big k smooths to 66.2%.

BasketballAdvanced~6 min

Build k-nearest neighbors from scratch in pure numpy - no training, just store the games and let the k closest ones vote - on a 2-D feature space (home and away net rating) to predict the NBA home win, then sweep k and compare to the tutorial-71 logistic regression.

A Decision Tree From Scratch

Grow a depth-3 decision tree in pure numpy — Gini impurity, brute-force splits, recursion — on NBA home wins. It scores 64.6%, and every decision is readable.

BasketballAdvanced~5 min

Grow a decision tree from scratch in pure numpy on the NBA home-win problem: measure purity with Gini impurity, brute-force the best (feature, threshold) split, recurse to depth 3, then read the flowchart it learned and compare its accuracy to logistic regression and k-NN.