|
|
|
|
|
|
|
|
|
|
|
|
|
|
url = "https://huggingface.co/datasets/Tomertg/stats/resolve/main/players_stats_by_season_full_details.csv" |
|
|
df = pd.read_csv(url) |
|
|
|
|
|
|
|
|
import pandas as pd |
|
|
import matplotlib.pyplot as plt |
|
|
import seaborn as sns |
|
|
from matplotlib.ticker import MaxNLocator |
|
|
|
|
|
|
|
|
sns.set(style="whitegrid", font_scale=1.1, palette="muted") |
|
|
|
|
|
|
|
|
df = pd.read_csv("players_stats_by_season_full_details.csv") |
|
|
|
|
|
|
|
|
df = df[df['League'] == 'NBA'] |
|
|
|
|
|
|
|
|
cols = ['Season', 'PTS', 'AST', '3PM', 'height_cm'] |
|
|
df = df[cols] |
|
|
|
|
|
|
|
|
print("Shape:", df.shape) |
|
|
df.head() |
|
|
|
|
|
|
|
|
df = df.dropna() |
|
|
df['Season_Year'] = df['Season'].str.extract(r'(\d{4})').astype(int) |
|
|
|
|
|
|
|
|
df = df[df['Season_Year'].between(1990, 2024)] |
|
|
|
|
|
|
|
|
for col in ['PTS', 'AST', '3PM', 'height_cm']: |
|
|
df[col] = pd.to_numeric(df[col], errors='coerce') |
|
|
|
|
|
print("After cleaning:", df.shape) |
|
|
|
|
|
|
|
|
df[['PTS', 'AST', '3PM', 'height_cm']].describe() |
|
|
|
|
|
|
|
|
trend = df.groupby('Season_Year')[['PTS', 'AST', '3PM', 'height_cm']].mean().reset_index() |
|
|
|
|
|
|
|
|
trend.rename(columns={ |
|
|
'PTS': 'Avg_Points', |
|
|
'AST': 'Avg_Assists', |
|
|
'3PM': 'Avg_3PM', |
|
|
'height_cm': 'Avg_Height' |
|
|
}, inplace=True) |
|
|
|
|
|
trend.head() |
|
|
|
|
|
|
|
|
def make_barplot(data, x, y, color, title, xlabel, ylabel): |
|
|
plt.figure(figsize=(9,6)) |
|
|
ax = sns.barplot(x=x, y=y, data=data, color=color, edgecolor='black') |
|
|
ax.yaxis.set_major_locator(MaxNLocator(integer=True)) |
|
|
plt.title(title, fontsize=15, weight='bold') |
|
|
plt.xlabel(xlabel) |
|
|
plt.ylabel(ylabel) |
|
|
plt.grid(True, linestyle='--', alpha=0.6) |
|
|
plt.tight_layout() |
|
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
plt.figure(figsize=(10,6)) |
|
|
|
|
|
bar_data = trend.melt(id_vars='Season_Year', value_vars=['Avg_Points', 'Avg_Assists'], |
|
|
var_name='Metric', value_name='Average') |
|
|
|
|
|
ax = sns.barplot(x='Season_Year', y='Average', hue='Metric', data=bar_data, |
|
|
palette=['orange', 'mediumseagreen'], edgecolor='black') |
|
|
|
|
|
ax.yaxis.set_major_locator(MaxNLocator(integer=True)) |
|
|
plt.title("Average Points and Assists per Player (NBA 1990–2024)", fontsize=15, weight='bold') |
|
|
plt.xlabel("Season Year") |
|
|
plt.ylabel("Average per Player") |
|
|
plt.xticks(rotation=45) |
|
|
plt.legend(title="Metric", loc='upper left') |
|
|
plt.grid(True, linestyle='--', alpha=0.6) |
|
|
plt.tight_layout() |
|
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
plt.figure(figsize=(10,6)) |
|
|
ax = sns.barplot(x='Season_Year', y='Avg_3PM', data=trend, color='dodgerblue', edgecolor='black') |
|
|
ax.yaxis.set_major_locator(MaxNLocator(integer=True)) |
|
|
plt.title("Average 3-Point Shots Made per Player (1990–2024)", fontsize=15, weight='bold') |
|
|
plt.xlabel("Season Year") |
|
|
plt.ylabel("Average 3PM per Player") |
|
|
plt.xticks(rotation=45) |
|
|
plt.grid(True, linestyle='--', alpha=0.6) |
|
|
plt.tight_layout() |
|
|
plt.show() |
|
|
|
|
|
|
|
|
plt.figure(figsize=(10,6)) |
|
|
ax = sns.lineplot(x='Season_Year', y='Avg_Height', data=trend, color='purple', linewidth=2.5, marker='o') |
|
|
ax.yaxis.set_major_locator(MaxNLocator(integer=True)) |
|
|
plt.title("Average Player Height (1990–2024)", fontsize=15, weight='bold') |
|
|
plt.xlabel("Season Year") |
|
|
plt.ylabel("Average Height (cm)") |
|
|
plt.grid(True, linestyle='--', alpha=0.6) |
|
|
plt.tight_layout() |
|
|
plt.show() |
|
|
|
|
|
|
|
|
plt.figure(figsize=(10,6)) |
|
|
scatter = sns.scatterplot( |
|
|
x='Avg_Assists', |
|
|
y='Avg_Points', |
|
|
data=trend, |
|
|
hue='Season_Year', |
|
|
palette='coolwarm', |
|
|
size='Avg_3PM', |
|
|
sizes=(50, 300), |
|
|
alpha=0.8, |
|
|
edgecolor='black' |
|
|
) |
|
|
|
|
|
sns.regplot( |
|
|
x='Avg_Assists', |
|
|
y='Avg_Points', |
|
|
data=trend, |
|
|
scatter=False, |
|
|
color='black', |
|
|
line_kws={'linestyle':'--', 'linewidth':2} |
|
|
) |
|
|
|
|
|
plt.title("Points vs Assists Over Time (Color = Year, Size = 3PM)", fontsize=15, weight='bold') |
|
|
plt.xlabel("Average Assists per Player") |
|
|
plt.ylabel("Average Points per Player") |
|
|
plt.grid(True, linestyle='--', alpha=0.6) |
|
|
plt.legend(title="Season Year", loc='upper left', bbox_to_anchor=(1.02, 1)) |
|
|
plt.tight_layout() |
|
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
trend['PTS_AST_Ratio'] = trend['Avg_Points'] / trend['Avg_Assists'] |
|
|
|
|
|
plt.figure(figsize=(10,6)) |
|
|
ax = sns.lineplot(x='Season_Year', y='PTS_AST_Ratio', data=trend, color='crimson', linewidth=2.5, marker='o') |
|
|
ax.yaxis.set_major_locator(MaxNLocator(integer=True)) |
|
|
plt.title("Ratio of Points to Assists Over Time (Team Play Indicator)", fontsize=15, weight='bold') |
|
|
plt.xlabel("Season Year") |
|
|
plt.ylabel("Points / Assists Ratio") |
|
|
plt.grid(True, linestyle='--', alpha=0.6) |
|
|
plt.tight_layout() |
|
|
plt.show() |
|
|
|
|
|
corr = trend[['Avg_Points', 'Avg_Assists', 'Avg_3PM', 'Avg_Height']].corr() |
|
|
|
|
|
plt.figure(figsize=(6,4)) |
|
|
sns.heatmap(corr, annot=True, cmap='coolwarm', fmt=".2f") |
|
|
plt.title("Correlation Between NBA Performance Metrics", fontsize=14, weight='bold') |
|
|
plt.tight_layout() |
|
|
plt.show() |
|
|
|
|
|
|
|
|
print(""" |
|
|
Summary of Findings (1990 - 2024): |
|
|
|
|
|
1. Average points per player have risen slightly - the game became faster and more efficient. |
|
|
2. Assists grew steadily - NBA teams now play with more collaboration and spacing. |
|
|
3. The 3-point revolution exploded after 2010 - the single biggest transformation in modern basketball. |
|
|
4. Average player height slightly decreased - speed and versatility are prioritized over size. |
|
|
5. The ratio of points to assists dropped - showing that more scoring now comes from teamwork. |
|
|
|
|
|
In short: The NBA has evolved from slow, isolation heavy basketball |
|
|
into a fast, perimeter oriented, analytics-driven, and team first game. |
|
|
""") |
|
|
|