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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# NBA Evolution Analysis (1990–2024)
|
| 2 |
+
# Course: Introduction to Data Science
|
| 3 |
+
# Author: Tomer Golan
|
| 4 |
+
# Objective: Analyze how the NBA game evolved over time using EDA and visualization
|
| 5 |
+
|
| 6 |
+
# Step 1: Import required libraries
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import seaborn as sns
|
| 10 |
+
from matplotlib.ticker import MaxNLocator
|
| 11 |
+
|
| 12 |
+
# Configure visual style
|
| 13 |
+
sns.set(style="whitegrid", font_scale=1.1, palette="muted")
|
| 14 |
+
|
| 15 |
+
# Load dataset
|
| 16 |
+
df = pd.read_csv("players_stats_by_season_full_details.csv")
|
| 17 |
+
|
| 18 |
+
# Focus only on NBA data
|
| 19 |
+
df = df[df['League'] == 'NBA']
|
| 20 |
+
|
| 21 |
+
# Select relevant columns
|
| 22 |
+
cols = ['Season', 'PTS', 'AST', '3PM', 'height_cm']
|
| 23 |
+
df = df[cols]
|
| 24 |
+
|
| 25 |
+
# Preview
|
| 26 |
+
print("Shape:", df.shape)
|
| 27 |
+
df.head()
|
| 28 |
+
|
| 29 |
+
# Drop missing values and ensure numeric columns
|
| 30 |
+
df = df.dropna()
|
| 31 |
+
df['Season_Year'] = df['Season'].str.extract(r'(\d{4})').astype(int)
|
| 32 |
+
|
| 33 |
+
# Filter for reasonable timeline
|
| 34 |
+
df = df[df['Season_Year'].between(1990, 2024)]
|
| 35 |
+
|
| 36 |
+
# Convert numeric columns properly
|
| 37 |
+
for col in ['PTS', 'AST', '3PM', 'height_cm']:
|
| 38 |
+
df[col] = pd.to_numeric(df[col], errors='coerce')
|
| 39 |
+
|
| 40 |
+
print("After cleaning:", df.shape)
|
| 41 |
+
|
| 42 |
+
# Summary statistics
|
| 43 |
+
df[['PTS', 'AST', '3PM', 'height_cm']].describe()
|
| 44 |
+
|
| 45 |
+
# Group by year and calculate averages
|
| 46 |
+
trend = df.groupby('Season_Year')[['PTS', 'AST', '3PM', 'height_cm']].mean().reset_index()
|
| 47 |
+
|
| 48 |
+
# Rename columns for clarity
|
| 49 |
+
trend.rename(columns={
|
| 50 |
+
'PTS': 'Avg_Points',
|
| 51 |
+
'AST': 'Avg_Assists',
|
| 52 |
+
'3PM': 'Avg_3PM',
|
| 53 |
+
'height_cm': 'Avg_Height'
|
| 54 |
+
}, inplace=True)
|
| 55 |
+
|
| 56 |
+
trend.head()
|
| 57 |
+
|
| 58 |
+
# Helper function for clean, clear barplots
|
| 59 |
+
def make_barplot(data, x, y, color, title, xlabel, ylabel):
|
| 60 |
+
plt.figure(figsize=(9,6))
|
| 61 |
+
ax = sns.barplot(x=x, y=y, data=data, color=color, edgecolor='black')
|
| 62 |
+
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
|
| 63 |
+
plt.title(title, fontsize=15, weight='bold')
|
| 64 |
+
plt.xlabel(xlabel)
|
| 65 |
+
plt.ylabel(ylabel)
|
| 66 |
+
plt.grid(True, linestyle='--', alpha=0.6)
|
| 67 |
+
plt.tight_layout()
|
| 68 |
+
plt.show()
|
| 69 |
+
|
| 70 |
+
# Average Points & Assists
|
| 71 |
+
|
| 72 |
+
plt.figure(figsize=(10,6))
|
| 73 |
+
|
| 74 |
+
bar_data = trend.melt(id_vars='Season_Year', value_vars=['Avg_Points', 'Avg_Assists'],
|
| 75 |
+
var_name='Metric', value_name='Average')
|
| 76 |
+
|
| 77 |
+
ax = sns.barplot(x='Season_Year', y='Average', hue='Metric', data=bar_data,
|
| 78 |
+
palette=['orange', 'mediumseagreen'], edgecolor='black')
|
| 79 |
+
|
| 80 |
+
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
|
| 81 |
+
plt.title("Average Points and Assists per Player (NBA 1990–2024)", fontsize=15, weight='bold')
|
| 82 |
+
plt.xlabel("Season Year")
|
| 83 |
+
plt.ylabel("Average per Player")
|
| 84 |
+
plt.xticks(rotation=45)
|
| 85 |
+
plt.legend(title="Metric", loc='upper left')
|
| 86 |
+
plt.grid(True, linestyle='--', alpha=0.6)
|
| 87 |
+
plt.tight_layout()
|
| 88 |
+
plt.show()
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# The 3 Point Revolution
|
| 92 |
+
|
| 93 |
+
plt.figure(figsize=(10,6))
|
| 94 |
+
ax = sns.barplot(x='Season_Year', y='Avg_3PM', data=trend, color='dodgerblue', edgecolor='black')
|
| 95 |
+
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
|
| 96 |
+
plt.title("Average 3-Point Shots Made per Player (1990–2024)", fontsize=15, weight='bold')
|
| 97 |
+
plt.xlabel("Season Year")
|
| 98 |
+
plt.ylabel("Average 3PM per Player")
|
| 99 |
+
plt.xticks(rotation=45)
|
| 100 |
+
plt.grid(True, linestyle='--', alpha=0.6)
|
| 101 |
+
plt.tight_layout()
|
| 102 |
+
plt.show()
|
| 103 |
+
|
| 104 |
+
# Average Height Over Time
|
| 105 |
+
plt.figure(figsize=(10,6))
|
| 106 |
+
ax = sns.lineplot(x='Season_Year', y='Avg_Height', data=trend, color='purple', linewidth=2.5, marker='o')
|
| 107 |
+
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
|
| 108 |
+
plt.title("Average Player Height (1990–2024)", fontsize=15, weight='bold')
|
| 109 |
+
plt.xlabel("Season Year")
|
| 110 |
+
plt.ylabel("Average Height (cm)")
|
| 111 |
+
plt.grid(True, linestyle='--', alpha=0.6)
|
| 112 |
+
plt.tight_layout()
|
| 113 |
+
plt.show()
|
| 114 |
+
|
| 115 |
+
# Points vs Assists over time
|
| 116 |
+
plt.figure(figsize=(10,6))
|
| 117 |
+
scatter = sns.scatterplot(
|
| 118 |
+
x='Avg_Assists',
|
| 119 |
+
y='Avg_Points',
|
| 120 |
+
data=trend,
|
| 121 |
+
hue='Season_Year',
|
| 122 |
+
palette='coolwarm',
|
| 123 |
+
size='Avg_3PM',
|
| 124 |
+
sizes=(50, 300),
|
| 125 |
+
alpha=0.8,
|
| 126 |
+
edgecolor='black'
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
sns.regplot(
|
| 130 |
+
x='Avg_Assists',
|
| 131 |
+
y='Avg_Points',
|
| 132 |
+
data=trend,
|
| 133 |
+
scatter=False,
|
| 134 |
+
color='black',
|
| 135 |
+
line_kws={'linestyle':'--', 'linewidth':2}
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
plt.title("Points vs Assists Over Time (Color = Year, Size = 3PM)", fontsize=15, weight='bold')
|
| 139 |
+
plt.xlabel("Average Assists per Player")
|
| 140 |
+
plt.ylabel("Average Points per Player")
|
| 141 |
+
plt.grid(True, linestyle='--', alpha=0.6)
|
| 142 |
+
plt.legend(title="Season Year", loc='upper left', bbox_to_anchor=(1.02, 1))
|
| 143 |
+
plt.tight_layout()
|
| 144 |
+
plt.show()
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# Ratio of Points to Assists
|
| 148 |
+
trend['PTS_AST_Ratio'] = trend['Avg_Points'] / trend['Avg_Assists']
|
| 149 |
+
|
| 150 |
+
plt.figure(figsize=(10,6))
|
| 151 |
+
ax = sns.lineplot(x='Season_Year', y='PTS_AST_Ratio', data=trend, color='crimson', linewidth=2.5, marker='o')
|
| 152 |
+
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
|
| 153 |
+
plt.title("Ratio of Points to Assists Over Time (Team Play Indicator)", fontsize=15, weight='bold')
|
| 154 |
+
plt.xlabel("Season Year")
|
| 155 |
+
plt.ylabel("Points / Assists Ratio")
|
| 156 |
+
plt.grid(True, linestyle='--', alpha=0.6)
|
| 157 |
+
plt.tight_layout()
|
| 158 |
+
plt.show()
|
| 159 |
+
|
| 160 |
+
corr = trend[['Avg_Points', 'Avg_Assists', 'Avg_3PM', 'Avg_Height']].corr()
|
| 161 |
+
|
| 162 |
+
plt.figure(figsize=(6,4))
|
| 163 |
+
sns.heatmap(corr, annot=True, cmap='coolwarm', fmt=".2f")
|
| 164 |
+
plt.title("Correlation Between NBA Performance Metrics", fontsize=14, weight='bold')
|
| 165 |
+
plt.tight_layout()
|
| 166 |
+
plt.show()
|
| 167 |
+
|
| 168 |
+
# Summary of Findings
|
| 169 |
+
print("""
|
| 170 |
+
Summary of Findings (1990 - 2024):
|
| 171 |
+
|
| 172 |
+
1. Average points per player have risen slightly - the game became faster and more efficient.
|
| 173 |
+
2. Assists grew steadily - NBA teams now play with more collaboration and spacing.
|
| 174 |
+
3. The 3-point revolution exploded after 2010 - the single biggest transformation in modern basketball.
|
| 175 |
+
4. Average player height slightly decreased - speed and versatility are prioritized over size.
|
| 176 |
+
5. The ratio of points to assists dropped - showing that more scoring now comes from teamwork.
|
| 177 |
+
|
| 178 |
+
In short: The NBA has evolved from slow, isolation heavy basketball
|
| 179 |
+
into a fast, perimeter oriented, analytics-driven, and team first game.
|
| 180 |
+
""")
|