File size: 4,965 Bytes
cd8bffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b4a582
 
cd8bffe
4b4a582
 
 
 
cd8bffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145e88a
 
 
 
 
 
 
 
 
cd8bffe
 
 
 
 
 
 
 
 
 
 
 
 
adb4f82
cd8bffe
 
 
 
 
 
 
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
# 🧠 Libraries
import os
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

import gradio as gr
import kagglehub

# 🎯 Step 1: Load Dataset
path = kagglehub.dataset_download("wyattowalsh/basketball")
csv_folder = os.path.join(path, "csv")
df = pd.read_csv(os.path.join(csv_folder, "game.csv"))

# 🧹 Step 2: Prepare Data
home_df = df[[
    'game_id', 'team_abbreviation_home', 'wl_home', 'fg3_pct_home', 'tov_home',
    'reb_home', 'ast_home', 'pts_home', 'stl_home', 'blk_home'
]].copy()
home_df.columns = ['game_id', 'team', 'win_label', 'fg3_pct', 'tov', 'reb', 'ast', 'pts', 'stl', 'blk']
home_df['is_home'] = 1

away_df = df[[
    'game_id', 'team_abbreviation_away', 'wl_away', 'fg3_pct_away', 'tov_away',
    'reb_away', 'ast_away', 'pts_away', 'stl_away', 'blk_away'
]].copy()
away_df.columns = ['game_id', 'team', 'win_label', 'fg3_pct', 'tov', 'reb', 'ast', 'pts', 'stl', 'blk']
away_df['is_home'] = 0

data = pd.concat([home_df, away_df], ignore_index=True)
data = data.dropna()
data['win'] = data['win_label'].apply(lambda x: 1 if x == 'W' else 0)

features = ['fg3_pct', 'tov', 'reb', 'ast', 'pts', 'stl', 'blk', 'is_home']
X = data[features]
y = data['win']

# πŸ” Step 3: Train Model
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)

# πŸ“Š Step 4: Evaluation Metrics

# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure()
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.tight_layout()
plt.savefig("confusion_matrix.png")
plt.close()

# Feature Importance
importances = model.feature_importances_
feature_df = pd.DataFrame({'Feature': features, 'Importance': importances}).sort_values(by='Importance', ascending=False)

plt.figure(figsize=(8, 5))
sns.barplot(data=feature_df, x='Importance', y='Feature')
plt.title("πŸ€ What Factors Lead to NBA Wins?")
plt.tight_layout()
plt.savefig("feature_importance.png")
plt.close()

# 🧠 Step 5: Define Prediction Function
def predict_win(fg3_pct, tov, reb, ast, pts, stl, blk, is_home):
    input_data = {
        'fg3_pct': fg3_pct,
        'tov': tov,
        'reb': reb,
        'ast': ast,
        'pts': pts,
        'stl': stl,
        'blk': blk,
        'is_home': 1 if is_home == "Yes" else 0
    }
    input_df = pd.DataFrame([input_data])
    prediction = model.predict(input_df)[0]
    prob = model.predict_proba(input_df)[0][prediction]

    if prediction == 1:
        result = "πŸ† WIN β€” Confidence: {:.2%}".format(prob)
        gif_html = "<img src='https://media.giphy.com/media/l0MYC0LajbaPoEADu/giphy.gif' width='300'>"
    else:
        result = "❌ LOSS β€” Confidence: {:.2%}".format(prob)
        gif_html = "<img src='https://media.giphy.com/media/l3vR85PnGsBwu1PFK/giphy.gif' width='300'>"
    
    return result, gif_html, "confusion_matrix.png", "feature_importance.png"

# 🎨 Step 6: Gradio Interface

inputs = [
    gr.Slider(0.0, 1.0, value=0.35, label="3-Point % (fg3_pct)"),
    gr.Slider(0, 25, value=12, label="Turnovers (tov)"),
    gr.Slider(20, 60, value=44, label="Rebounds (reb)"),
    gr.Slider(5, 40, value=22, label="Assists (ast)"),
    gr.Slider(60, 150, value=110, label="Points (pts)"),
    gr.Slider(0, 20, value=7, label="Steals (stl)"),
    gr.Slider(0, 15, value=5, label="Blocks (blk)"),
    gr.Radio(["Yes", "No"], label="Is it a home game?")
]

intro = f"""
<style>
body {{
    background-color: #f9fbfd;
    font-family: 'Segoe UI', sans-serif;
}}
</style>

## πŸ€ NBA Win Predictor
Welcome! This app uses real NBA game data and machine learning to predict whether a team will **WIN** or **LOSE** based on your input stats.

---

### πŸ‘¨β€πŸ’» Built by: Ahmad Raza  
- Software Engineer 3.82 CGPA
- AI/ML and Full Stack Developer
- Stanford Section Leader 2025
- NASA's Hackathon Nominee 2024
- 16 Hackathons Participant
- 350+ LeetCode

- πŸ“§ [Linkedin](https://www.linkedin.com/in/ahmad-raza-6667552b1/)
- πŸ“§ [Github](https://www.github.com/in/mohahmadraza/)

---

### πŸ“Š Model Accuracy: **{accuracy:.2%}**
Trained on thousands of NBA games using Random Forests.
"""

# πŸ–₯️ Interface
gr.Interface(
    fn=predict_win,
    inputs=inputs,
    outputs=[
        gr.Text(label="Prediction"),
        gr.HTML(label="Reaction GIF"),  # πŸ‘ˆ Fixed this
        gr.Image(type="filepath", label="Confusion Matrix"),
        gr.Image(type="filepath", label="Feature Importance")
    ],
    title="πŸ€ NBA Game Outcome Predictor",
    description=intro,
    theme="default"
).launch(share=True)