File size: 5,891 Bytes
6bdf439
 
 
 
9c0f32e
 
6bdf439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# NBA Evolution Analysis (1990–2024)
# Course: Introduction to Data Science
# Author: Tomer Golan
# Objective: Analyze how the NBA game evolved over time using EDA and visualization
url = "https://huggingface.co/datasets/Tomertg/stats/resolve/main/players_stats_by_season_full_details.csv"
df = pd.read_csv(url)

# Step 1: Import required libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.ticker import MaxNLocator

# Configure visual style
sns.set(style="whitegrid", font_scale=1.1, palette="muted")

# Load dataset
df = pd.read_csv("players_stats_by_season_full_details.csv")

# Focus only on NBA data
df = df[df['League'] == 'NBA']

# Select relevant columns
cols = ['Season', 'PTS', 'AST', '3PM', 'height_cm']
df = df[cols]

# Preview
print("Shape:", df.shape)
df.head()

# Drop missing values and ensure numeric columns
df = df.dropna()
df['Season_Year'] = df['Season'].str.extract(r'(\d{4})').astype(int)

# Filter for reasonable timeline
df = df[df['Season_Year'].between(1990, 2024)]

# Convert numeric columns properly
for col in ['PTS', 'AST', '3PM', 'height_cm']:
    df[col] = pd.to_numeric(df[col], errors='coerce')

print("After cleaning:", df.shape)

# Summary statistics
df[['PTS', 'AST', '3PM', 'height_cm']].describe()

# Group by year and calculate averages
trend = df.groupby('Season_Year')[['PTS', 'AST', '3PM', 'height_cm']].mean().reset_index()

# Rename columns for clarity
trend.rename(columns={
    'PTS': 'Avg_Points',
    'AST': 'Avg_Assists',
    '3PM': 'Avg_3PM',
    'height_cm': 'Avg_Height'
}, inplace=True)

trend.head()

# Helper function for clean, clear barplots
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()

# Average Points & Assists 

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


# The 3 Point Revolution 

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

# Average Height Over Time
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()

# Points vs Assists over time
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()


# Ratio of Points to Assists 
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()

# Summary of Findings
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.
""")