Spaces:
Running
Running
File size: 21,598 Bytes
c095e08 |
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
"""
NBA ML Prediction System - Data Visualization
==============================================
Create and save visualizations for analysis and reporting.
All graphs are saved to the 'graphs' folder.
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
from pathlib import Path
from typing import Optional, List, Dict
from datetime import datetime
import logging
from src.config import PROJECT_ROOT, RAW_DATA_DIR, PROCESSED_DATA_DIR
logger = logging.getLogger(__name__)
# =============================================================================
# CONFIGURATION
# =============================================================================
GRAPHS_DIR = PROJECT_ROOT / "graphs"
GRAPHS_DIR.mkdir(exist_ok=True)
# Style settings
plt.style.use('dark_background')
COLORS = {
'primary': '#7c3aed',
'secondary': '#00d4ff',
'success': '#10b981',
'warning': '#f59e0b',
'danger': '#ef4444',
'gradient': ['#7c3aed', '#00d4ff', '#f472b6']
}
def save_figure(fig, name: str, dpi: int = 150):
"""Save figure to graphs folder."""
path = GRAPHS_DIR / f"{name}.png"
fig.savefig(path, dpi=dpi, bbox_inches='tight', facecolor='#1a1a2e', edgecolor='none')
logger.info(f"Saved graph to {path}")
plt.close(fig)
return path
# =============================================================================
# TEAM PERFORMANCE VISUALIZATIONS
# =============================================================================
class TeamVisualizer:
"""Visualization for team-level statistics."""
def plot_elo_history(self, elo_history: pd.DataFrame, team_abbrev: str = None) -> Path:
"""
Plot ELO rating history over time.
Args:
elo_history: DataFrame with columns [date, team, elo]
"""
fig, ax = plt.subplots(figsize=(14, 7))
if team_abbrev:
data = elo_history[elo_history['team'] == team_abbrev]
ax.plot(data['date'], data['elo'], color=COLORS['primary'], linewidth=2)
ax.fill_between(data['date'], 1500, data['elo'], alpha=0.3, color=COLORS['primary'])
title = f"{team_abbrev} ELO Rating History"
else:
# Plot top 5 teams
for i, (team, data) in enumerate(elo_history.groupby('team')):
if i < 5:
ax.plot(data['date'], data['elo'], label=team, linewidth=2)
ax.legend(loc='upper left')
title = "ELO Rating History - Top Teams"
ax.axhline(y=1500, color='white', linestyle='--', alpha=0.3, label='Average')
ax.set_xlabel('Date', fontsize=12)
ax.set_ylabel('ELO Rating', fontsize=12)
ax.set_title(title, fontsize=16, fontweight='bold')
ax.grid(True, alpha=0.2)
return save_figure(fig, f"elo_history_{team_abbrev or 'all'}")
def plot_team_comparison(self, team_stats: pd.DataFrame,
metrics: List[str] = None) -> Path:
"""
Radar chart comparing multiple teams.
"""
if metrics is None:
metrics = ['PTS', 'AST', 'REB', 'STL', 'BLK', 'FG_PCT']
# Normalize metrics to 0-1 scale
normalized = team_stats[metrics].copy()
for col in metrics:
normalized[col] = (normalized[col] - normalized[col].min()) / (normalized[col].max() - normalized[col].min())
# Create radar chart
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw=dict(polar=True))
angles = np.linspace(0, 2 * np.pi, len(metrics), endpoint=False).tolist()
angles += angles[:1]
for i, (idx, row) in enumerate(team_stats.head(5).iterrows()):
values = normalized.loc[idx, metrics].tolist()
values += values[:1]
ax.plot(angles, values, linewidth=2, label=row.get('TEAM_ABBREVIATION', f'Team {i+1}'))
ax.fill(angles, values, alpha=0.1)
ax.set_xticks(angles[:-1])
ax.set_xticklabels(metrics)
ax.legend(loc='upper right', bbox_to_anchor=(1.3, 1))
ax.set_title('Team Comparison', fontsize=16, fontweight='bold', pad=20)
return save_figure(fig, "team_comparison_radar")
def plot_standings(self, standings: pd.DataFrame) -> Path:
"""
Horizontal bar chart of team standings by win percentage.
"""
fig, ax = plt.subplots(figsize=(12, 10))
data = standings.sort_values('W_PCT', ascending=True).tail(15)
colors = [COLORS['primary'] if i >= 7 else COLORS['secondary']
for i in range(len(data))]
bars = ax.barh(data['TEAM_ABBREVIATION'], data['W_PCT'], color=colors, edgecolor='white', linewidth=0.5)
# Add playoff line
ax.axvline(x=0.5, color=COLORS['warning'], linestyle='--', linewidth=2, label='Playoff Cutoff')
ax.set_xlabel('Win Percentage', fontsize=12)
ax.set_title('Team Standings', fontsize=16, fontweight='bold')
ax.set_xlim(0, 1)
ax.legend()
ax.grid(axis='x', alpha=0.2)
# Add value labels
for bar, val in zip(bars, data['W_PCT']):
ax.text(val + 0.02, bar.get_y() + bar.get_height()/2,
f'{val:.1%}', va='center', fontsize=10)
return save_figure(fig, "standings")
# =============================================================================
# GAME PREDICTION VISUALIZATIONS
# =============================================================================
class GameVisualizer:
"""Visualization for game predictions and analysis."""
def plot_prediction_calibration(self, predictions: pd.DataFrame) -> Path:
"""
Calibration curve - how well do probabilities match actual outcomes.
"""
fig, ax = plt.subplots(figsize=(10, 10))
# Bin predictions
bins = np.linspace(0, 1, 11)
bin_centers = (bins[:-1] + bins[1:]) / 2
predicted_proba = predictions['predicted_proba']
actual = predictions['actual']
bin_indices = np.digitize(predicted_proba, bins) - 1
bin_indices = np.clip(bin_indices, 0, 9)
actual_fractions = []
for i in range(10):
mask = bin_indices == i
if mask.sum() > 0:
actual_fractions.append(actual[mask].mean())
else:
actual_fractions.append(np.nan)
# Perfect calibration line
ax.plot([0, 1], [0, 1], 'w--', linewidth=2, label='Perfect Calibration')
# Actual calibration
ax.plot(bin_centers, actual_fractions, 'o-', color=COLORS['primary'],
linewidth=3, markersize=10, label='Model Calibration')
ax.set_xlabel('Predicted Probability', fontsize=12)
ax.set_ylabel('Actual Win Rate', fontsize=12)
ax.set_title('Prediction Calibration Curve', fontsize=16, fontweight='bold')
ax.legend()
ax.grid(True, alpha=0.2)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
return save_figure(fig, "calibration_curve")
def plot_accuracy_by_confidence(self, predictions: pd.DataFrame) -> Path:
"""
How does accuracy change with prediction confidence?
"""
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
# Calculate confidence as distance from 0.5
predictions = predictions.copy()
predictions['confidence'] = abs(predictions['predicted_proba'] - 0.5)
predictions['correct'] = predictions['predicted'] == predictions['actual']
# Bin by confidence
bins = np.linspace(0, 0.5, 6)
predictions['confidence_bin'] = pd.cut(predictions['confidence'], bins)
accuracy_by_conf = predictions.groupby('confidence_bin')['correct'].mean()
count_by_conf = predictions.groupby('confidence_bin').size()
# Accuracy plot
ax1.bar(range(len(accuracy_by_conf)), accuracy_by_conf.values,
color=COLORS['primary'], edgecolor='white')
ax1.set_xticks(range(len(accuracy_by_conf)))
ax1.set_xticklabels(['Low', 'Med-Low', 'Medium', 'Med-High', 'High'], rotation=45)
ax1.set_ylabel('Accuracy', fontsize=12)
ax1.set_title('Accuracy by Confidence Level', fontsize=14, fontweight='bold')
ax1.set_ylim(0, 1)
ax1.axhline(y=0.5, color='white', linestyle='--', alpha=0.3)
# Count plot
ax2.bar(range(len(count_by_conf)), count_by_conf.values,
color=COLORS['secondary'], edgecolor='white')
ax2.set_xticks(range(len(count_by_conf)))
ax2.set_xticklabels(['Low', 'Med-Low', 'Medium', 'Med-High', 'High'], rotation=45)
ax2.set_ylabel('Number of Predictions', fontsize=12)
ax2.set_title('Prediction Distribution', fontsize=14, fontweight='bold')
plt.tight_layout()
return save_figure(fig, "accuracy_by_confidence")
def plot_feature_importance(self, importance_df: pd.DataFrame, top_n: int = 15) -> Path:
"""
Bar chart of feature importance.
"""
fig, ax = plt.subplots(figsize=(12, 8))
data = importance_df.head(top_n).sort_values('avg_importance', ascending=True)
bars = ax.barh(data['feature'], data['avg_importance'],
color=COLORS['primary'], edgecolor='white', linewidth=0.5)
ax.set_xlabel('Importance Score', fontsize=12)
ax.set_title('Top Features for Game Prediction', fontsize=16, fontweight='bold')
ax.grid(axis='x', alpha=0.2)
return save_figure(fig, "feature_importance")
# =============================================================================
# MVP VISUALIZATIONS
# =============================================================================
class MVPVisualizer:
"""Visualization for MVP race analysis."""
def plot_mvp_race(self, mvp_df: pd.DataFrame) -> Path:
"""
Horizontal bar chart of MVP race standings.
"""
fig, ax = plt.subplots(figsize=(12, 8))
data = mvp_df.head(10).sort_values('mvp_score', ascending=True)
colors = plt.cm.Purples(np.linspace(0.3, 0.9, len(data)))
bars = ax.barh(data['PLAYER_NAME'], data['mvp_score'], color=colors, edgecolor='white')
ax.set_xlabel('MVP Score', fontsize=12)
ax.set_title('MVP Race 2024-25', fontsize=16, fontweight='bold')
ax.grid(axis='x', alpha=0.2)
# Add value labels
for bar, val in zip(bars, data['mvp_score']):
ax.text(val + 0.5, bar.get_y() + bar.get_height()/2,
f'{val:.1f}', va='center', fontsize=10)
return save_figure(fig, "mvp_race")
def plot_mvp_similarity(self, mvp_df: pd.DataFrame) -> Path:
"""
Scatter plot of MVP score vs historical similarity.
"""
fig, ax = plt.subplots(figsize=(10, 8))
scatter = ax.scatter(mvp_df['mvp_similarity'], mvp_df['mvp_score'],
s=mvp_df['PTS'] * 10, c=mvp_df['mvp_score'],
cmap='Purples', alpha=0.7, edgecolor='white')
# Add labels for top candidates
for idx, row in mvp_df.head(5).iterrows():
ax.annotate(row['PLAYER_NAME'],
(row['mvp_similarity'], row['mvp_score']),
xytext=(10, 10), textcoords='offset points',
fontsize=10, color='white')
ax.set_xlabel('Similarity to Historical MVPs', fontsize=12)
ax.set_ylabel('MVP Score', fontsize=12)
ax.set_title('MVP Score vs Historical Similarity', fontsize=16, fontweight='bold')
ax.grid(True, alpha=0.2)
cbar = plt.colorbar(scatter, ax=ax)
cbar.set_label('MVP Score', fontsize=10)
return save_figure(fig, "mvp_similarity_scatter")
def plot_stat_comparison(self, mvp_df: pd.DataFrame,
stats: List[str] = None) -> Path:
"""
Bar chart comparing stats of top MVP candidates.
"""
if stats is None:
stats = ['PTS', 'REB', 'AST']
fig, axes = plt.subplots(1, len(stats), figsize=(5 * len(stats), 6))
if len(stats) == 1:
axes = [axes]
top_players = mvp_df.head(5)
for ax, stat in zip(axes, stats):
colors = plt.cm.Purples(np.linspace(0.4, 0.9, len(top_players)))
bars = ax.bar(top_players['PLAYER_NAME'], top_players[stat], color=colors, edgecolor='white')
ax.set_ylabel(stat, fontsize=12)
ax.set_title(f'{stat} Comparison', fontsize=14, fontweight='bold')
ax.tick_params(axis='x', rotation=45)
for bar, val in zip(bars, top_players[stat]):
ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5,
f'{val:.1f}', ha='center', fontsize=10)
plt.tight_layout()
return save_figure(fig, "mvp_stat_comparison")
# =============================================================================
# CHAMPIONSHIP VISUALIZATIONS
# =============================================================================
class ChampionshipVisualizer:
"""Visualization for championship predictions."""
def plot_championship_odds(self, champ_df: pd.DataFrame) -> Path:
"""
Pie chart of championship probabilities.
"""
fig, ax = plt.subplots(figsize=(10, 10))
data = champ_df.head(8)
colors = plt.cm.Purples(np.linspace(0.3, 0.9, len(data)))
wedges, texts, autotexts = ax.pie(
data['champ_probability'],
labels=data['TEAM_ABBREVIATION'],
autopct='%1.1f%%',
colors=colors,
explode=[0.05] * len(data),
shadow=True,
startangle=90
)
for text in texts:
text.set_fontsize(12)
text.set_color('white')
for autotext in autotexts:
autotext.set_fontsize(10)
autotext.set_color('white')
ax.set_title('Championship Probabilities', fontsize=16, fontweight='bold')
return save_figure(fig, "championship_odds_pie")
def plot_strength_vs_experience(self, champ_df: pd.DataFrame) -> Path:
"""
Scatter plot of team strength vs playoff experience.
"""
fig, ax = plt.subplots(figsize=(12, 8))
scatter = ax.scatter(
champ_df['playoff_experience'],
champ_df['strength_rating'],
s=champ_df['champ_probability'] * 3000,
c=champ_df['champ_probability'],
cmap='Purples',
alpha=0.7,
edgecolor='white',
linewidth=2
)
# Add labels
for idx, row in champ_df.iterrows():
ax.annotate(
row['TEAM_ABBREVIATION'],
(row['playoff_experience'], row['strength_rating']),
xytext=(10, 5), textcoords='offset points',
fontsize=11, color='white', fontweight='bold'
)
ax.set_xlabel('Playoff Experience Index', fontsize=12)
ax.set_ylabel('Strength Rating', fontsize=12)
ax.set_title('Championship Contenders: Strength vs Experience', fontsize=16, fontweight='bold')
ax.grid(True, alpha=0.2)
cbar = plt.colorbar(scatter, ax=ax)
cbar.set_label('Championship Probability', fontsize=10)
return save_figure(fig, "strength_vs_experience")
# =============================================================================
# SEASON ANALYSIS VISUALIZATIONS
# =============================================================================
class SeasonVisualizer:
"""Visualization for historical season analysis."""
def plot_scoring_trends(self, season_data: pd.DataFrame) -> Path:
"""
Line chart of scoring trends across seasons.
"""
fig, ax = plt.subplots(figsize=(14, 7))
ax.plot(season_data['season'], season_data['avg_pts'],
color=COLORS['primary'], linewidth=3, marker='o', markersize=8)
ax.fill_between(season_data['season'], season_data['avg_pts'], alpha=0.3, color=COLORS['primary'])
ax.set_xlabel('Season', fontsize=12)
ax.set_ylabel('Average Points Per Game', fontsize=12)
ax.set_title('NBA Scoring Trends Over Time', fontsize=16, fontweight='bold')
ax.tick_params(axis='x', rotation=45)
ax.grid(True, alpha=0.2)
return save_figure(fig, "scoring_trends")
def plot_three_point_revolution(self, season_data: pd.DataFrame) -> Path:
"""
Dual-axis chart showing 3PA and 3P% trends.
"""
fig, ax1 = plt.subplots(figsize=(14, 7))
ax2 = ax1.twinx()
ax1.bar(season_data['season'], season_data['avg_3pa'],
color=COLORS['secondary'], alpha=0.7, label='3-Point Attempts')
ax2.plot(season_data['season'], season_data['avg_3pct'],
color=COLORS['primary'], linewidth=3, marker='o', label='3-Point %')
ax1.set_xlabel('Season', fontsize=12)
ax1.set_ylabel('3-Point Attempts', fontsize=12, color=COLORS['secondary'])
ax2.set_ylabel('3-Point Percentage', fontsize=12, color=COLORS['primary'])
ax1.set_title('The 3-Point Revolution', fontsize=16, fontweight='bold')
ax1.tick_params(axis='x', rotation=45)
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, loc='upper left')
return save_figure(fig, "three_point_revolution")
# =============================================================================
# MASTER VISUALIZER
# =============================================================================
class NBAVisualizer:
"""Master class combining all visualization capabilities."""
def __init__(self):
self.team = TeamVisualizer()
self.game = GameVisualizer()
self.mvp = MVPVisualizer()
self.championship = ChampionshipVisualizer()
self.season = SeasonVisualizer()
def generate_all_visualizations(self, data: Dict[str, pd.DataFrame]) -> List[Path]:
"""
Generate all available visualizations from provided data.
Args:
data: Dict with keys like 'standings', 'mvp', 'championship', etc.
Returns:
List of paths to saved graphs
"""
saved_paths = []
if 'standings' in data:
saved_paths.append(self.team.plot_standings(data['standings']))
if 'mvp' in data:
saved_paths.append(self.mvp.plot_mvp_race(data['mvp']))
saved_paths.append(self.mvp.plot_stat_comparison(data['mvp']))
if 'championship' in data:
saved_paths.append(self.championship.plot_championship_odds(data['championship']))
saved_paths.append(self.championship.plot_strength_vs_experience(data['championship']))
if 'predictions' in data:
saved_paths.append(self.game.plot_calibration(data['predictions']))
saved_paths.append(self.game.plot_accuracy_by_confidence(data['predictions']))
logger.info(f"Generated {len(saved_paths)} visualizations")
return saved_paths
# =============================================================================
# CLI INTERFACE
# =============================================================================
if __name__ == "__main__":
print(f"Generating sample visualizations to {GRAPHS_DIR}...")
# Create sample data for testing
sample_mvp = pd.DataFrame({
'PLAYER_NAME': ['Shai Gilgeous-Alexander', 'Nikola Jokic', 'Jayson Tatum',
'Luka Doncic', 'Giannis Antetokounmpo'],
'PTS': [31.5, 26.8, 27.2, 28.5, 30.5],
'REB': [5.5, 12.5, 8.2, 8.8, 11.5],
'AST': [6.0, 9.2, 4.8, 8.2, 6.5],
'mvp_score': [85.2, 82.1, 78.5, 77.2, 76.8],
'mvp_similarity': [0.92, 0.95, 0.85, 0.88, 0.90]
})
sample_champ = pd.DataFrame({
'TEAM_ABBREVIATION': ['OKC', 'CLE', 'BOS', 'DEN', 'MEM', 'HOU', 'NYK', 'GSW'],
'W_PCT': [0.70, 0.68, 0.65, 0.62, 0.60, 0.58, 0.55, 0.52],
'playoff_experience': [0.3, 0.5, 0.8, 0.9, 0.4, 0.2, 0.5, 0.95],
'strength_rating': [45, 42, 40, 38, 35, 33, 30, 28],
'champ_probability': [0.18, 0.15, 0.14, 0.12, 0.10, 0.09, 0.08, 0.07]
})
viz = NBAVisualizer()
# Generate sample visualizations
print("Creating MVP race chart...")
viz.mvp.plot_mvp_race(sample_mvp)
print("Creating MVP stat comparison...")
viz.mvp.plot_stat_comparison(sample_mvp)
print("Creating championship odds pie chart...")
viz.championship.plot_championship_odds(sample_champ)
print("Creating strength vs experience chart...")
viz.championship.plot_strength_vs_experience(sample_champ)
print(f"\n✅ Visualizations saved to: {GRAPHS_DIR}")
|