Download app.py from michaelryt/Game_Win_Predictor_Using_Random_Forest_Classifier: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/spaces/michaelryt/Game_Win_Predictor_Using_Random_Forest_Classifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/michaelryt/Game_Win_Predictor_Using_Random_Forest_Classifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/michaelryt/Game_Win_Predictor_Using_Random_Forest_Classifier/resolve/main/app.py
4.98 kB
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.metrics import accuracy_score, classification_report, confusion_matrix | |
| # Custom Streamlit styling with sticky navbar | |
| st.markdown( | |
| """ | |
| <style> | |
| body { | |
| background-color: #1E1E1E; | |
| color: #FFFFFF; | |
| font-family: 'Arial', sans-serif; | |
| } | |
| .stButton>button { | |
| background-color: #4A90E2; | |
| color: #FFFFFF; | |
| border-radius: 15px; | |
| padding: 12px 24px; | |
| font-size: 16px; | |
| font-weight: bold; | |
| } | |
| .title { | |
| color: #64FFDA; | |
| text-shadow: 1px 1px #FF4C4C; | |
| } | |
| .stTabs [data-testid="stHorizontalBlock"] { | |
| position: sticky; | |
| top: 0; | |
| background-color: #1E1E1E; | |
| z-index: 10; | |
| } | |
| </style> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| # Load the League of Legends dataset | |
| st.title("League of Legends Game Win Predictor") | |
| st.markdown("#### Predict whether the Blue Team will dominate the game using Random Forest Classifier", unsafe_allow_html=True) | |
| # Load dataset directly | |
| file_path = 'high_diamond_ranked_10min.csv' | |
| df = pd.read_csv(file_path) | |
| # Preprocess data and train model (runs once) | |
| df = df[['blueFirstBlood', 'blueKills', 'blueDeaths', 'blueAssists', 'blueTotalGold', 'blueTotalExperience', 'blueDragons', 'blueHeralds', 'blueTowersDestroyed', 'blueWins']] | |
| df = df.dropna() | |
| # Define features and target | |
| X = df.drop('blueWins', axis=1) | |
| y = df['blueWins'] | |
| # Split data | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Scale data | |
| scaler = StandardScaler() | |
| X_train = scaler.fit_transform(X_train) | |
| X_test = scaler.transform(X_test) | |
| # Train Logistic Regression Model | |
| model = LogisticRegression(random_state=42) | |
| model.fit(X_train, y_train) | |
| y_pred = model.predict(X_test) | |
| # Top Tabs Navigation | |
| tab1, tab2, tab3 = st.tabs(["๐ Dataset", "๐ Visualization", "๐ฎ Prediction"]) | |
| # Dataset Section | |
| with tab1: | |
| st.write("### ๐ Dataset Preview") | |
| st.dataframe(df.head()) | |
| # Visualization Section | |
| with tab2: | |
| # Display model performance | |
| accuracy = accuracy_score(y_test, y_pred) | |
| st.write("### ๐ฅ Model Performance") | |
| st.write(f"**โ Model Accuracy:** {accuracy:.2f}") | |
| # Visualizing performance | |
| st.write("### ๐ Performance Breakdown") | |
| conf_matrix = confusion_matrix(y_test, y_pred) | |
| st.write("Confusion Matrix:") | |
| fig, ax = plt.subplots() | |
| sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='coolwarm', ax=ax) | |
| st.pyplot(fig) | |
| # Feature Importance Visualization | |
| st.write("### ๐ Feature Importance") | |
| feature_importance = abs(model.coef_[0]) | |
| features = X.columns | |
| fig, ax = plt.subplots() | |
| ax.barh(features, feature_importance, color='#4A90E2') | |
| ax.set_title("Feature Importance for Blue Team Win Prediction") | |
| ax.set_xlabel("Importance") | |
| st.pyplot(fig) | |
| # Prediction Section | |
| with tab3: | |
| st.write("### ๐ฎ Predict Game Outcome") | |
| st.markdown("Adjust the stats below to simulate a match scenario!") | |
| first_blood = st.selectbox("Did Blue Team Get First Blood?", [0, 1]) | |
| kills = st.slider("Blue Team Kills", min_value=0, max_value=50, value=5) | |
| deaths = st.slider("Blue Team Deaths", min_value=0, max_value=50, value=5) | |
| assists = st.slider("Blue Team Assists", min_value=0, max_value=50, value=10) | |
| total_gold = st.slider("Blue Team Total Gold", min_value=10000, max_value=100000, value=50000) | |
| total_exp = st.slider("Blue Team Total Experience", min_value=10000, max_value=100000, value=50000) | |
| dragons = st.slider("Blue Team Dragons Taken", min_value=0, max_value=5, value=1) | |
| heralds = st.slider("Blue Team Heralds Taken", min_value=0, max_value=2, value=0) | |
| towers = st.slider("Blue Team Towers Destroyed", min_value=0, max_value=11, value=2) | |
| if st.button("โจ Predict Win"): | |
| input_data = scaler.transform([[first_blood, kills, deaths, assists, total_gold, total_exp, dragons, heralds, towers]]) | |
| prediction = model.predict(input_data)[0] | |
| prediction_proba = model.predict_proba(input_data)[0] | |
| st.subheader("๐ฎ Prediction Result") | |
| result_text = "๐ Blue Team is likely to WIN! ๐" if prediction == 1 else "โ๏ธ Blue Team is likely to LOSE. ๐" | |
| st.success(result_text) if prediction == 1 else st.error(result_text) | |
| st.write(f"Confidence: {prediction_proba[prediction]:.2f}") | |
| # Win/Loss Bar Chart | |
| st.write("### ๐ Win Probability Breakdown") | |
| fig, ax = plt.subplots() | |
| ax.bar(["Win", "Lose"], [prediction_proba[1], prediction_proba[0]], color=["#64FFDA", "#FF4C4C"]) | |
| ax.set_ylim(0, 1) | |
| ax.set_ylabel("Probability") | |
| ax.set_title("Blue Team Win/Loss Probability") | |
| st.pyplot(fig) | |