
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.
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.
No tutorials match that filter.

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.

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

Nudge two bar series apart with a half-width offset to put each NBA team's offense and defense side by side — the chart for comparisons within each group.

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.

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.

Group a season of NBA shots by court zone to see where points really come from: volume, FG%, and points per shot for the rim, mid-range, and three.

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.

Plot every NBA team's offensive rating against its defensive rating with league-average crosshairs — one scatter separates contenders from rebuilders.

Turn a game-by-game results log into a final standings table — one concat plus one groupby collapses a season of scores into wins, losses, and win%.

Compute a correlation matrix with .corr() and render it as an annotated heatmap, showing at a glance which NBA team stats rise and fall together.

pd.crosstab() counts how two categorical variables co-occur — here, who won against how big the margin was — and normalize='index' turns counts into rates.

NBA home teams won 54.3% of 2023-24 games. Bootstrap the games thousands of times and read a 95% confidence interval that sits entirely above 50%: a real edge.

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.

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.

Build a Lorenz curve and Gini coefficient in pure numpy from 25,000 real NBA shots — the top 20% of scorers hold over half the points (Gini ~0.51).

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

Start from a flat Beta(1,1) prior, feed it 1,231 real NBA games, and watch the posterior sharpen into a tight home-win estimate with a 95% credible interval.

Build a 2x2 table from 25,000 real NBA shots and compute the chi-square test by hand in numpy — expected counts, the statistic, and a clean null verdict.

Turn a single-game win probability into exact best-of-seven odds in pure numpy, verify it by Monte Carlo, and read the 2024 Finals off the amplification curve.

Aggregate NBA shot data across twenty seasons to chart basketball's biggest tactical shift — the three-point revolution — and quantify how it changed.

Across 2004-2024, three-point volume more than doubled while accuracy barely moved — a capstone regression showing an era won on shot value, not better aim.

Build k-means in pure numpy and watch it sort 30 NBA teams into playing-style archetypes from offensive and defensive rating alone — no labels needed.

Build logistic regression in pure numpy to predict NBA home wins from the net-rating gap — reaching 67% accuracy against a 54% always-pick-home baseline.

Hold out 25% of NBA games and grade the home-win classifier honestly (67.9% on unseen games), then build the confusion matrix, precision, and recall.

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.

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.

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

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.