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|
+
import numpy as np
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
from sklearn.model_selection import train_test_split
|
| 9 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 10 |
+
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
| 11 |
+
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import kagglehub
|
| 14 |
+
|
| 15 |
+
# π― Step 1: Load Dataset
|
| 16 |
+
path = kagglehub.dataset_download("wyattowalsh/basketball")
|
| 17 |
+
csv_folder = os.path.join(path, "csv")
|
| 18 |
+
df = pd.read_csv(os.path.join(csv_folder, "game.csv"))
|
| 19 |
+
|
| 20 |
+
# π§Ή Step 2: Prepare Data
|
| 21 |
+
home_df = df[[
|
| 22 |
+
'game_id', 'team_abbreviation_home', 'wl_home', 'fg3_pct_home', 'tov_home',
|
| 23 |
+
'reb_home', 'ast_home', 'pts_home', 'stl_home', 'blk_home'
|
| 24 |
+
]].copy()
|
| 25 |
+
home_df.columns = ['game_id', 'team', 'win_label', 'fg3_pct', 'tov', 'reb', 'ast', 'pts', 'stl', 'blk']
|
| 26 |
+
home_df['is_home'] = 1
|
| 27 |
+
|
| 28 |
+
away_df = df[[
|
| 29 |
+
'game_id', 'team_abbreviation_away', 'wl_away', 'fg3_pct_away', 'tov_away',
|
| 30 |
+
'reb_away', 'ast_away', 'pts_away', 'stl_away', 'blk_away'
|
| 31 |
+
]].copy()
|
| 32 |
+
away_df.columns = ['game_id', 'team', 'win_label', 'fg3_pct', 'tov', 'reb', 'ast', 'pts', 'stl', 'blk']
|
| 33 |
+
away_df['is_home'] = 0
|
| 34 |
+
|
| 35 |
+
data = pd.concat([home_df, away_df], ignore_index=True)
|
| 36 |
+
data = data.dropna()
|
| 37 |
+
data['win'] = data['win_label'].apply(lambda x: 1 if x == 'W' else 0)
|
| 38 |
+
|
| 39 |
+
features = ['fg3_pct', 'tov', 'reb', 'ast', 'pts', 'stl', 'blk', 'is_home']
|
| 40 |
+
X = data[features]
|
| 41 |
+
y = data['win']
|
| 42 |
+
|
| 43 |
+
# π Step 3: Train Model
|
| 44 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 45 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 46 |
+
model.fit(X_train, y_train)
|
| 47 |
+
|
| 48 |
+
y_pred = model.predict(X_test)
|
| 49 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 50 |
+
|
| 51 |
+
# π Step 4: Evaluation Metrics
|
| 52 |
+
|
| 53 |
+
# Confusion Matrix
|
| 54 |
+
cm = confusion_matrix(y_test, y_pred)
|
| 55 |
+
plt.figure()
|
| 56 |
+
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
|
| 57 |
+
plt.xlabel("Predicted")
|
| 58 |
+
plt.ylabel("Actual")
|
| 59 |
+
plt.title("Confusion Matrix")
|
| 60 |
+
plt.tight_layout()
|
| 61 |
+
plt.savefig("confusion_matrix.png")
|
| 62 |
+
plt.close()
|
| 63 |
+
|
| 64 |
+
# Feature Importance
|
| 65 |
+
importances = model.feature_importances_
|
| 66 |
+
feature_df = pd.DataFrame({'Feature': features, 'Importance': importances}).sort_values(by='Importance', ascending=False)
|
| 67 |
+
|
| 68 |
+
plt.figure(figsize=(8, 5))
|
| 69 |
+
sns.barplot(data=feature_df, x='Importance', y='Feature')
|
| 70 |
+
plt.title("π What Factors Lead to NBA Wins?")
|
| 71 |
+
plt.tight_layout()
|
| 72 |
+
plt.savefig("feature_importance.png")
|
| 73 |
+
plt.close()
|
| 74 |
+
|
| 75 |
+
# π§ Step 5: Define Prediction Function
|
| 76 |
+
def predict_win(fg3_pct, tov, reb, ast, pts, stl, blk, is_home):
|
| 77 |
+
input_data = {
|
| 78 |
+
'fg3_pct': fg3_pct,
|
| 79 |
+
'tov': tov,
|
| 80 |
+
'reb': reb,
|
| 81 |
+
'ast': ast,
|
| 82 |
+
'pts': pts,
|
| 83 |
+
'stl': stl,
|
| 84 |
+
'blk': blk,
|
| 85 |
+
'is_home': 1 if is_home == "Yes" else 0
|
| 86 |
+
}
|
| 87 |
+
input_df = pd.DataFrame([input_data])
|
| 88 |
+
prediction = model.predict(input_df)[0]
|
| 89 |
+
prob = model.predict_proba(input_df)[0][prediction]
|
| 90 |
+
|
| 91 |
+
if prediction == 1:
|
| 92 |
+
return "π WIN β Confidence: {:.2%}".format(prob), \
|
| 93 |
+
"https://media.giphy.com/media/l0MYC0LajbaPoEADu/giphy.gif", \
|
| 94 |
+
"confusion_matrix.png", "feature_importance.png"
|
| 95 |
+
else:
|
| 96 |
+
return "β LOSS β Confidence: {:.2%}".format(prob), \
|
| 97 |
+
"https://media.giphy.com/media/l3vR85PnGsBwu1PFK/giphy.gif", \
|
| 98 |
+
"confusion_matrix.png", "feature_importance.png"
|
| 99 |
+
|
| 100 |
+
# π¨ Step 6: Gradio Interface
|
| 101 |
+
|
| 102 |
+
inputs = [
|
| 103 |
+
gr.Slider(0.0, 1.0, value=0.35, label="3-Point % (fg3_pct)"),
|
| 104 |
+
gr.Slider(0, 25, value=12, label="Turnovers (tov)"),
|
| 105 |
+
gr.Slider(20, 60, value=44, label="Rebounds (reb)"),
|
| 106 |
+
gr.Slider(5, 40, value=22, label="Assists (ast)"),
|
| 107 |
+
gr.Slider(60, 150, value=110, label="Points (pts)"),
|
| 108 |
+
gr.Slider(0, 20, value=7, label="Steals (stl)"),
|
| 109 |
+
gr.Slider(0, 15, value=5, label="Blocks (blk)"),
|
| 110 |
+
gr.Radio(["Yes", "No"], label="Is it a home game?")
|
| 111 |
+
]
|
| 112 |
+
|
| 113 |
+
intro = f"""
|
| 114 |
+
<style>
|
| 115 |
+
body {{
|
| 116 |
+
background-color: #f9fbfd;
|
| 117 |
+
font-family: 'Segoe UI', sans-serif;
|
| 118 |
+
}}
|
| 119 |
+
</style>
|
| 120 |
+
|
| 121 |
+
## π NBA Win Predictor
|
| 122 |
+
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.
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
### π¨βπ» Built by: Ahmad Raza
|
| 127 |
+
- Python Developer | AI Enthusiast
|
| 128 |
+
- Contributor to Volund (AI Chatbot) π§ , Study Sage π, MediInfo π
|
| 129 |
+
- Passionate about using data to solve real-world problems
|
| 130 |
+
- π§ [Contact Me](mailto:sktfscm21557034@gmail.com)
|
| 131 |
+
|
| 132 |
+
---
|
| 133 |
+
|
| 134 |
+
### π Model Accuracy: **{accuracy:.2%}**
|
| 135 |
+
Trained on thousands of NBA games using Random Forests.
|
| 136 |
+
"""
|
| 137 |
+
|
| 138 |
+
# π₯οΈ Interface
|
| 139 |
+
gr.Interface(
|
| 140 |
+
fn=predict_win,
|
| 141 |
+
inputs=inputs,
|
| 142 |
+
outputs=[
|
| 143 |
+
gr.Text(label="Prediction"),
|
| 144 |
+
gr.Image(type="url", label="Reaction GIF"),
|
| 145 |
+
gr.Image(type="filepath", label="Confusion Matrix"),
|
| 146 |
+
gr.Image(type="filepath", label="Feature Importance")
|
| 147 |
+
],
|
| 148 |
+
title="π NBA Game Outcome Predictor",
|
| 149 |
+
description=intro,
|
| 150 |
+
theme="default"
|
| 151 |
+
).launch(share=True)
|