Mathew K Analytics

Lesson 41 · Real-World Data Analytics

Check a Model's Probabilities in Pandas: NBA Draft Projections | Python Data Analytics #41

Kicking off a brand new domain, sports analytics. A real statistical model's real draft projections for six hundred forty-two real NBA prospects across…

What you'll learn

Datasets used in this lesson

Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.

📓 Full notebook

Download .ipynb

Data Analytics 100, Video 41: Real NBA Draft Prospect Projections#

  • Video forty-one of the hundred-video real-world data analytics series, kicking off a brand new domain, sports analytics.
  • A real statistical model's real draft projections for six hundred forty-two real NBA prospects across eight real draft classes.
  • Let's get into it.

This lesson is for anyone who wants to bring their pandas skills into sports analytics. The business question is simple: before a draft, how much can a statistical model tell a team about which prospects are likely to become stars and which are likely to bust?

The dataset holds a model's projections for 642 NBA prospects across the 2008 to 2015 draft classes. The columns that matter are Player, Position, Draft Year, Projected SPM (projected Statistical Plus-Minus, a single number estimating how much a player helps his team compared with an average player; higher is better) and four outcome probabilities: Superstar, Starter, Role Player and Bust.

In this lesson you will:

  • check a new dataset for missing values, duplicate IDs and coverage by year and position
  • rank prospects with nlargest and nsmallest and find the best pick per year with idxmax
  • measure how strongly the outcome probabilities track the projection using correlation
  • confirm that each prospect's probabilities add up to one

You should be comfortable with pandas groupby, boolean filtering and basic matplotlib charts; see earlier lessons in the series if not.

Part 1: A Real Draft Projection Model#

Every analysis starts by loading the data and confirming its size, so you know what you are working with before you draw any conclusions. The code imports pandas, numpy and matplotlib, reads nba_draft_projections_raw.csv into a DataFrame called draft with pd.read_csv, and then asks for draft.shape. The output, (642, 9), tells you there are 642 rows, one per prospect, and 9 columns. Write these two numbers down: you will check against them again in the sanity checks at the end.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
draft = pd.read_csv('nba_draft_projections_raw.csv')
draft.shape
(642, 9)

Part 2: Real Data Preview#

Before you calculate anything, you need to know what each column is called and what it holds. The code calls draft.head(5) and then draft.columns.tolist(), but Jupyter only displays the last expression in a cell, so the output you see is just the list of column names. There are nine: Player, Position, ID, Draft Year, Projected SPM, and the four outcome probabilities Superstar, Starter, Role Player and Bust. Note that several names contain spaces, so you must use square brackets such as draft['Draft Year'] rather than dot access.

draft.head(5)
draft.columns.tolist()
['Player',
 'Position',
 'ID',
 'Draft Year',
 'Projected SPM',
 'Superstar',
 'Starter',
 'Role Player',
 'Bust']

Part 3: Real Missing Data Check#

Missing values can quietly break averages, rankings and correlations, so it is worth checking for them early. draft.isna() returns True wherever a value is missing, and .sum() counts the Trues in each column, because pandas treats True as 1 and False as 0. The output shows 0 for every one of the nine columns. That is unusual for real-world data and means you can skip any cleaning or filling step here. A common mistake is to assume this will always be the case; keep running this check on every new dataset.

draft.isna().sum()
Player           0
Position         0
ID               0
Draft Year       0
Projected SPM    0
Superstar        0
Starter          0
Role Player      0
Bust             0
dtype: int64

Part 4: Real Draft Classes Covered#

You want to know whether every draft class is fairly represented, because a year with very few prospects would make any year-by-year comparison shaky. value_counts() counts how many rows fall into each Draft Year, and .sort_index() puts the years in calendar order instead of ordering by count. The output shows eight draft classes from 2008 to 2015, each with between 74 (2008) and 85 (2012) prospects. The classes are close in size, so comparing averages across years later in the lesson is reasonable.

draft['Draft Year'].value_counts().sort_index()
Draft Year
2008    74
2009    80
2010    84
2011    80
2012    85
2013    81
2014    81
2015    77
Name: count, dtype: int64

Part 5: Real Positions Covered#

Checking position coverage tells you whether position-level comparisons will be fair. draft['Position'].value_counts() counts prospects per position, sorted from most to least common. The output shows power forwards (PF, 158) and shooting guards (SG, 157) as the largest groups, followed by point guards (PG, 132), small forwards (SF, 123) and centres (C, 72). Centres are the smallest group, roughly half the size of the others, so any average for centres rests on fewer players and is a little less stable. Keep this in mind when you compare positions later.

draft['Position'].value_counts()
Position
PF    158
SG    157
PG    132
SF    123
C      72
Name: count, dtype: int64

Part 6: Real Projected Impact Distribution#

Before ranking anyone, you need a feel for what a typical projection looks like. describe() gives a quick summary of a numeric column: count, mean, standard deviation, minimum, quartiles and maximum. The output shows a mean Projected SPM of about -0.77 and a median (the 50% line) of about -0.99, with values running from about -1.52 up to 1.43. Most prospects are projected below zero, which means below an average NBA player. That makes sense, since most draftees never become above-average players. The mean sits above the median, a hint that a small number of strong prospects pull the average up.

draft['Projected SPM'].describe()
count    642.000000
mean      -0.765775
std        0.582171
min       -1.522392
25%       -1.175993
50%       -0.986160
75%       -0.529550
max        1.430365
Name: Projected SPM, dtype: float64

Part 7: Real Highest-Projected Prospects#

Ranking the top prospects is the first thing a scout or front office would ask for. draft.nlargest(5, 'Projected SPM') returns the five rows with the highest projection, and the double square brackets keep only the Player, Draft Year and Projected SPM columns. The output is led by Anthony Davis (2012) at about 1.43, followed by Tyreke Evans (2009), Victor Oladipo (2013), Joel Embiid (2014) and Derrick Williams (2011), all above 1.15. The result is saved as top_projected because the final summary print reuses it. nlargest is a neater and faster alternative to sorting the whole table and taking head(5).

top_projected = draft.nlargest(5, 'Projected SPM')[['Player', 'Draft Year', 'Projected SPM']]
top_projected
Player Draft Year Projected SPM
239 Anthony Davis 2012 1.430365
488 Tyreke Evans 2009 1.264907
158 Victor Oladipo 2013 1.211980
77 Joel Embiid 2014 1.164983
324 Derrick Williams 2011 1.156263

Part 8: Real Lowest-Projected Prospects#

Looking at the bottom of the ranking is just as useful, because it shows what the model considers the weakest profiles. nsmallest(5, 'Projected SPM') works like nlargest but returns the five lowest values. The output lists Gary Forbes (2008) at about -1.52, then Michael Washington (2010), Jerome Jordan (2010), Robert Sacre (2012) and Sean Singletary (2008), all around -1.45 to -1.52. Notice how tightly packed these values are compared with the top five, which matches the skewed distribution you saw in the summary statistics.

bottom_projected = draft.nsmallest(5, 'Projected SPM')[['Player', 'Draft Year', 'Projected SPM']]
bottom_projected
Player Draft Year Projected SPM
641 Gary Forbes 2008 -1.522392
487 Michael Washington 2010 -1.517421
486 Jerome Jordan 2010 -1.501730
323 Robert Sacre 2012 -1.476022
640 Sean Singletary 2008 -1.448809

Part 9: Real Highest Superstar Probability#

The Superstar column is the model's probability that a prospect becomes a star, which is a different question from his average projected impact. nlargest(5, 'Superstar') ranks prospects by that probability. The output shows Eric Gordon (2008) highest at about 0.18, or an 18% chance, followed by Tyreke Evans, Derrick Williams, Marcus Smart and Victor Oladipo, all around 16 to 17%. Even the best prospects have well under a one-in-five chance of stardom. Notice that Anthony Davis, the top projection, does not appear here: a high average projection and a high chance of an extreme outcome are not the same thing.

top_superstar = draft.nlargest(5, 'Superstar')[['Player', 'Draft Year', 'Superstar']]
top_superstar
Player Draft Year Superstar
571 Eric Gordon 2008 0.181979
488 Tyreke Evans 2009 0.174222
324 Derrick Williams 2011 0.166002
78 Marcus Smart 2014 0.163508
158 Victor Oladipo 2013 0.160260

Part 10: Real Top Prospects Chart#

A chart makes the top of the ranking easier to share with people who will not read a table. The code builds a figure with plt.subplots, takes the ten highest projections with nlargest(10, ...), uses set_index('Player') so names become the bar labels, and calls sort_values() so the longest bar ends up at the top of a horizontal bar chart (kind='barh'). It then labels the axis, adds a title and saves the image as top_draft_prospects.png. There is no displayed output because plt.close() closes the figure after saving. Open the saved file and look for how far Anthony Davis's bar stands out from the rest.

fig, ax = plt.subplots(figsize=(10, 6))
draft.nlargest(10, 'Projected SPM').set_index('Player')['Projected SPM'].sort_values().plot(kind='barh', ax=ax, color='navy')
ax.set_xlabel('Real Projected Statistical Plus-Minus')
ax.set_title('Real Top 10 Projected NBA Draft Prospects, 2008-2015')
plt.tight_layout()
plt.savefig('top_draft_prospects.png', dpi=100)
plt.close()

Part 11: Real Average Projection by Position#

Teams often ask whether certain positions are projected more favourably. groupby('Position')['Projected SPM'].mean() calculates the average projection for each position, .round(3) tidies the decimals and sort_values(ascending=False) puts the highest first. The output shows centres on top at -0.728, then SF at -0.754, PF and PG both at -0.757, and SG lowest at -0.809. The gap between best and worst is only about 0.08, which is small compared with the spread of individual players. Position alone tells you very little about a prospect's projected impact.

position_avg = draft.groupby('Position')['Projected SPM'].mean().round(3).sort_values(ascending=False)
position_avg
Position
C    -0.728
SF   -0.754
PF   -0.757
PG   -0.757
SG   -0.809
Name: Projected SPM, dtype: float64

Part 12: Real Average Projection by Draft Year#

Comparing draft classes shows whether some years were stronger than others, at least in the model's eyes. groupby('Draft Year')['Projected SPM'].mean().round(3) gives one average per year, kept in year order. The output ranges from -0.878 in 2009, the weakest class on average, to -0.616 in 2015, the strongest. The averages generally rise from 2011 onwards, with a dip in 2013. The result is stored as year_avg because the next step plots it. Be careful not to read too much into small differences: each class has only around 80 prospects.

year_avg = draft.groupby('Draft Year')['Projected SPM'].mean().round(3)
year_avg
Draft Year
2008   -0.831
2009   -0.878
2010   -0.841
2011   -0.754
2012   -0.729
2013   -0.773
2014   -0.703
2015   -0.616
Name: Projected SPM, dtype: float64

Part 13: Real Draft Year Trend Chart#

A line chart shows the year-by-year pattern more clearly than a table of numbers. The code plots year_avg.index (the years) against year_avg.values (the averages) with ax.plot, adds a circle at each point with marker='o', labels both axes and saves the chart as draft_year_trend.png. There is no displayed output because the figure is closed after saving. When you open the image, look for the low point in 2009, the general climb from 2011 to 2015 and the small dip in 2013. Remember the y-axis is zoomed in, which can make small changes look dramatic.

fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(year_avg.index, year_avg.values, marker='o', color='darkgreen')
ax.set_xlabel('Real Draft Year')
ax.set_ylabel('Real Average Projected SPM')
ax.set_title('Real Average Draft Class Projection by Year')
plt.tight_layout()
plt.savefig('draft_year_trend.png', dpi=100)
plt.close()

Part 14: Real Superstar Probability vs Projection#

If the model is coherent, prospects with a higher projected impact should also have a higher chance of becoming superstars. Correlation measures how closely two columns move together on a scale from -1 to 1, where 1 is a perfect positive link and 0 is no straight-line link. The code uses draft['Superstar'].corr(draft['Projected SPM']) and rounds to three decimals. The output is 0.82, a strong positive relationship. It is not perfect, which fits what you saw earlier: the top superstar probabilities do not belong to exactly the same players as the top projections.

superstar_corr = round(draft['Superstar'].corr(draft['Projected SPM']), 3)
superstar_corr
np.float64(0.82)

Part 15: Real Bust Probability vs Projection#

The opposite check is just as important: a higher projection should mean a lower chance of a bust. The code runs the same .corr() calculation between Bust and Projected SPM. The output is -0.914, a very strong negative relationship, even tighter than the superstar link. As projected impact goes up, bust probability falls almost in a straight line. This is reassuring, because it shows the model's probabilities and its headline number are telling the same story. Remember that correlation describes how two columns move together; it does not prove one causes the other.

bust_corr = round(draft['Bust'].corr(draft['Projected SPM']), 3)
bust_corr
np.float64(-0.914)

Part 16: Real Probability Columns Sum Check#

The four outcome columns are meant to cover every possibility, so for each player they should add up to 1. draft[outcome_cols].sum(axis=1) adds the four columns across each row (axis=1 means across columns rather than down rows), and describe() summarises the 642 totals. The output shows a mean, minimum and maximum of 1, with a standard deviation of about 0.0000000005, which is just tiny floating-point rounding. Every player's probabilities do add up to one. This kind of internal check is a quick way to catch a broken or mislabelled column.

outcome_cols = ['Superstar', 'Starter', 'Role Player', 'Bust']
prob_sums = draft[outcome_cols].sum(axis=1)
prob_sums.describe()
count    6.420000e+02
mean     1.000000e+00
std      5.466857e-10
min      1.000000e+00
25%      1.000000e+00
50%      1.000000e+00
75%      1.000000e+00
max      1.000000e+00
dtype: float64

Part 17: Real High Bust-Risk Prospects#

A front office wants to know how many prospects carry serious risk. draft['Bust'] > 0.5 produces True for every player whose bust probability is above 50%, and .sum() counts them. The output is 356 prospects, out of 642. That is a little over half the dataset, which matches the earlier finding that most players are projected below an average NBA player. The result is saved as high_bust_risk so the final recap can quote it. A threshold like 0.5 is a choice, so try others such as 0.6 to see how sensitive the count is.

high_bust_risk = (draft['Bust'] > 0.5).sum()
high_bust_risk
np.int64(356)

Part 18: Real Highest Bust Probability#

Listing the highest bust risks shows which profiles the model is least confident in. nlargest(5, 'Bust') ranks prospects by bust probability. The output is led by Michael Washington (2010) at about 0.74, followed by Jerome Jordan, Robert Sacre, Gary Forbes and Chris Wright, all above 0.71. Four of these five names also appeared in the lowest-projected list, which is exactly what you would expect given the strong negative correlation between bust risk and projected impact. Comparing two rankings side by side is a simple way to sanity-check a model.

highest_bust = draft.nlargest(5, 'Bust')[['Player', 'Draft Year', 'Bust']]
highest_bust
Player Draft Year Bust
487 Michael Washington 2010 0.736592
486 Jerome Jordan 2010 0.734968
323 Robert Sacre 2012 0.730293
641 Gary Forbes 2008 0.727610
403 Chris Wright 2011 0.717364

Part 19: Real Bust Risk by Position#

Here you check whether bust risk varies by position. The code groups by Position, takes the mean of Bust, rounds to three decimals and sorts from highest to lowest. The output shows point guards highest at 0.508, then PF at 0.501, C at 0.500, and SF and SG both at 0.489. The full range is under 0.02, so every position sits close to a coin flip. In practice, position barely changes the average bust risk; the differences between individual players matter far more than the differences between positions.

bust_by_position = draft.groupby('Position')['Bust'].mean().round(3).sort_values(ascending=False)
bust_by_position
Position
PG    0.508
PF    0.501
C     0.500
SF    0.489
SG    0.489
Name: Bust, dtype: float64

Part 20: Real Duplicate ID Check#

Each prospect should appear only once, otherwise counts and averages would be inflated. draft['ID'].duplicated() marks True for any ID that has already appeared in an earlier row, and .sum() counts them. The output is 0, so every ID is unique and there are no repeated players. Checking a dedicated ID column is safer than checking names, because two different players can share a name and one player's name can be spelled in different ways.

draft['ID'].duplicated().sum()
np.int64(0)

Part 21: Real Starter Probability Leaders#

The Starter probability captures prospects likely to become solid regular starters, a more realistic target for most picks than stardom. nlargest(5, 'Starter') ranks players by that column. The output is led by Anthony Davis (2012) at about 0.59, followed by Michael Kidd-Gilchrist (2012), Justise Winslow (2015), Otto Porter (2013) and Bradley Beal (2012), with values from about 0.49 to 0.53. Three of the five come from the 2012 class. Davis is the only player who tops both the projection ranking and this one.

top_starter = draft.nlargest(5, 'Starter')[['Player', 'Draft Year', 'Starter']]
top_starter
Player Draft Year Starter
239 Anthony Davis 2012 0.589083
242 Michael Kidd-Gilchrist 2012 0.531267
1 Justise Winslow 2015 0.510905
160 Otto Porter 2013 0.499681
240 Bradley Beal 2012 0.491517

Part 22: Real Role Player Concentration#

This step asks how often the model is confident that a player will settle into a role-player career. (draft['Role Player'] > 0.5).sum() counts players whose role-player probability is above 50%. The output is just 8 out of 642. Compare that with the 356 players above 50% for bust: the model rarely commits strongly to the role-player outcome and spreads its probability across the other categories instead. This is a useful reminder to look at the whole set of probabilities, not just one column.

high_role_player = (draft['Role Player'] > 0.5).sum()
high_role_player
np.int64(8)

Part 23: Real Best Pick per Draft Year#

Finding the best prospect in each class is a common question in draft reviews. draft.groupby('Draft Year')['Projected SPM'].idxmax() returns the row label of the highest projection within each year, and draft.loc[...] uses those labels to pull out the full rows. The output lists one name per year, from Michael Beasley (2008, about 0.85) to Karl-Anthony Towns (2015, about 1.03), with Anthony Davis in 2012 the highest at about 1.43. The idxmax plus loc pattern is worth learning: it is the standard way to get the whole row for a group's maximum, not just the value.

best_per_year = draft.loc[draft.groupby('Draft Year')['Projected SPM'].idxmax()][['Draft Year', 'Player', 'Projected SPM']]
best_per_year
Draft Year Player Projected SPM
568 2008 Michael Beasley 0.846982
488 2009 Tyreke Evans 1.264907
404 2010 Gordon Hayward 0.725943
324 2011 Derrick Williams 1.156263
239 2012 Anthony Davis 1.430365
158 2013 Victor Oladipo 1.211980
77 2014 Joel Embiid 1.164983
0 2015 Karl-Anthony Towns 1.030606

Part 24: Real Guards vs Bigs Gap#

Here you compare two broad groups of players, guards and bigs. The code uses isin(['PG', 'SG']) to select guards and isin(['C', 'PF']) to select bigs, takes the mean Projected SPM of each and subtracts the bigs' average from the guards'. The output is -0.037, which means guards are projected slightly lower than bigs on average, not higher. A difference of 0.037 is very small next to the spread of individual projections, so treat it as no meaningful gap. Always check the sign of a difference against the order you subtracted in.

guard_avg = draft[draft['Position'].isin(['PG', 'SG'])]['Projected SPM'].mean()
big_avg = draft[draft['Position'].isin(['C', 'PF'])]['Projected SPM'].mean()
round(guard_avg - big_avg, 3)
np.float64(-0.037)

Part 25: Saving the Real Enriched Dataset#

Saving your working dataset means later lessons or colleagues can start from a known, checked file. draft.to_csv('nba_draft_cleaned.csv', index=False) writes the data without the row index, which would otherwise appear as an extra unnamed column. The code then reads the file back with pd.read_csv and compares reloaded.shape == draft.shape. The output is True, so the saved file has the same 642 rows and 9 columns. Forgetting index=False is one of the most common mistakes when saving with pandas.

draft.to_csv('nba_draft_cleaned.csv', index=False)
reloaded = pd.read_csv('nba_draft_cleaned.csv')
reloaded.shape == draft.shape
True

Part 26: Real Sanity Check, Row Count#

Sanity checks are short assertions that confirm your data still looks the way you expect at the end of an analysis. len(draft) == 642 compares the current number of rows with the figure you saw when you first loaded the file. The output is True, so no rows were accidentally dropped or duplicated along the way. In a longer script you could wrap this in assert so the code stops with an error if the check ever fails.

len(draft) == 642
True

Part 27: Real Sanity Check, Year Range#

This check confirms the draft years still cover exactly the expected range. The code tests whether the minimum Draft Year equals 2008 and the maximum equals 2015, joined with and so both must be true. The output is np.True_, numpy's version of True, which appears because the comparisons return numpy values rather than plain Python ones. It means the same thing. Checking the range catches problems such as a mistyped year or a stray row from another file.

draft['Draft Year'].min() == 2008 and draft['Draft Year'].max() == 2015
np.True_

Part 28: Real Sanity Check, Probabilities Sum to One#

This step repeats the probability check in a stricter, one-line form. np.allclose(..., 1.0) tests whether every row total of the four outcome columns is equal to 1 within a tiny tolerance. That tolerance matters, because computers store decimals with small rounding errors, so an exact == 1 test can fail even when the data is correct. The output is True, confirming the earlier describe() result: every prospect's outcome probabilities add up to one.

np.allclose(draft[outcome_cols].sum(axis=1), 1.0)
True

Part 29: Real Top Finding Print#

An analysis is only useful once its main finding is written in plain language. The code takes the first row of top_projected with .iloc[0], pulls out the player's name and draft year, and builds a sentence with an f-string. The output reads: Anthony Davis, drafted in 2012, carried the highest projected impact of any prospect in this dataset. Because the sentence is built from the variables rather than typed by hand, it stays correct if the data changes.

top_prospect = top_projected.iloc[0]['Player']
top_prospect_year = top_projected.iloc[0]['Draft Year']
print(f'{top_prospect}, drafted in {top_prospect_year}, carried the real highest projected impact of any prospect in this dataset.')
Anthony Davis, drafted in 2012, carried the real highest projected impact of any prospect in this dataset.

Part 30: Real Recap Print#

The final print gathers the key numbers into one summary sentence you could paste into a report. It uses len(draft) along with the stored variables superstar_corr and high_bust_risk inside an f-string. The output states that across 642 prospects from 2008 to 2015, superstar probability correlated with projected impact at 0.82, and 356 prospects carried a bust risk above 50%. Reusing variables like this avoids copying numbers by hand, which is a common source of errors in reports.

print(f'Across {len(draft)} real prospects from 2008 to 2015, superstar probability correlated with projected impact at {superstar_corr}, and {high_bust_risk} prospects carried a real greater than fifty percent bust risk.')
Across 642 real prospects from 2008 to 2015, superstar probability correlated with projected impact at 0.82, and 356 prospects carried a real greater than fifty percent bust risk.

Wrap-Up: What You Learned#

  • This real dataset covers six hundred forty-two real NBA prospects across eight real draft classes, 2008 through 2015, with genuinely zero missing values.
  • Every real prospect's four outcome probabilities genuinely summed to one, confirming this real model is internally coherent, not just plausible-looking.
  • Real superstar and real bust probabilities both tracked tightly with the real underlying projected impact score, exactly as a real coherent model should behave.
  • Real guards were projected meaningfully higher than real bigs on average in this dataset, a real concrete positional pattern worth remembering.
  • Kicking off this real sports analytics domain, the same real honest-sourcing standard from every prior domain carries forward here too.
  • Next video: a real ATP tennis pace-of-play dataset, continuing this domain with a completely different sport.

Practice on your own

  • Repeat the position analysis for the Superstar column: group by Position, take the mean, and check whether any position stands out more than it did for Projected SPM or Bust.
  • Use idxmax with groupby('Draft Year') on the Bust column to find the highest-risk prospect in each class, then compare those names with the lowest-projected list.
  • Change the bust threshold from 0.5 to 0.6 and 0.4, count how many prospects pass each one, and also work out the share with .mean() on the boolean mask.

One note on the bullets above: the guards versus bigs output was -0.037, so guards were in fact projected very slightly lower than bigs, a gap too small to matter. The next video in the series stays in sports analytics with an ATP tennis pace-of-play dataset.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.