Michael Rey
commited on
Commit
ยท
b7adb60
1
Parent(s):
4a3b9c0
added changes
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ from sklearn.preprocessing import StandardScaler
|
|
8 |
from sklearn.linear_model import LogisticRegression
|
9 |
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
10 |
|
|
|
11 |
st.markdown(
|
12 |
"""
|
13 |
<style>
|
@@ -28,22 +29,26 @@ st.markdown(
|
|
28 |
color: #64FFDA;
|
29 |
text-shadow: 1px 1px #FF4C4C;
|
30 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
</style>
|
32 |
""",
|
33 |
unsafe_allow_html=True
|
34 |
)
|
35 |
|
36 |
# Load the League of Legends dataset
|
37 |
-
st.title("
|
38 |
-
st.markdown("
|
39 |
|
40 |
# Load dataset directly
|
41 |
file_path = 'high_diamond_ranked_10min.csv'
|
42 |
df = pd.read_csv(file_path)
|
43 |
-
st.write("### ๐ Dataset Preview")
|
44 |
-
st.dataframe(df.head())
|
45 |
|
46 |
-
#
|
47 |
df = df[['blueFirstBlood', 'blueKills', 'blueDeaths', 'blueAssists', 'blueTotalGold', 'blueTotalExperience', 'blueDragons', 'blueHeralds', 'blueTowersDestroyed', 'blueWins']]
|
48 |
df = df.dropna()
|
49 |
|
@@ -64,58 +69,69 @@ model = LogisticRegression(random_state=42)
|
|
64 |
model.fit(X_train, y_train)
|
65 |
y_pred = model.predict(X_test)
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
st.
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
ax.
|
87 |
-
|
88 |
-
st.pyplot(fig)
|
89 |
-
|
90 |
-
#
|
91 |
-
st.write("###
|
92 |
-
|
93 |
-
|
94 |
-
first_blood = st.selectbox("Did Blue Team Get First Blood?", [0, 1])
|
95 |
-
kills = st.slider("Blue Team Kills", min_value=0, max_value=50, value=5)
|
96 |
-
deaths = st.slider("Blue Team Deaths", min_value=0, max_value=50, value=5)
|
97 |
-
assists = st.slider("Blue Team Assists", min_value=0, max_value=50, value=10)
|
98 |
-
total_gold = st.slider("Blue Team Total Gold", min_value=10000, max_value=100000, value=50000)
|
99 |
-
total_exp = st.slider("Blue Team Total Experience", min_value=10000, max_value=100000, value=50000)
|
100 |
-
dragons = st.slider("Blue Team Dragons Taken", min_value=0, max_value=5, value=1)
|
101 |
-
heralds = st.slider("Blue Team Heralds Taken", min_value=0, max_value=2, value=0)
|
102 |
-
towers = st.slider("Blue Team Towers Destroyed", min_value=0, max_value=11, value=2)
|
103 |
-
|
104 |
-
if st.button("โจ Predict Win"):
|
105 |
-
input_data = scaler.transform([[first_blood, kills, deaths, assists, total_gold, total_exp, dragons, heralds, towers]])
|
106 |
-
prediction = model.predict(input_data)[0]
|
107 |
-
prediction_proba = model.predict_proba(input_data)[0]
|
108 |
-
|
109 |
-
st.subheader("๐ฎ Prediction Result")
|
110 |
-
result_text = "๐
Blue Team is likely to WIN! ๐" if prediction == 1 else "โ๏ธ Blue Team is likely to LOSE. ๐"
|
111 |
-
st.success(result_text) if prediction == 1 else st.error(result_text)
|
112 |
-
st.write(f"Confidence: {prediction_proba[prediction]:.2f}")
|
113 |
-
|
114 |
-
# Win/Loss Bar Chart
|
115 |
-
st.write("### ๐ Win Probability Breakdown")
|
116 |
fig, ax = plt.subplots()
|
117 |
-
ax.
|
118 |
-
ax.
|
119 |
-
ax.
|
120 |
-
ax.set_title("Blue Team Win/Loss Probability")
|
121 |
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from sklearn.linear_model import LogisticRegression
|
9 |
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
10 |
|
11 |
+
# Custom Streamlit styling with sticky navbar
|
12 |
st.markdown(
|
13 |
"""
|
14 |
<style>
|
|
|
29 |
color: #64FFDA;
|
30 |
text-shadow: 1px 1px #FF4C4C;
|
31 |
}
|
32 |
+
.stTabs [data-testid="stHorizontalBlock"] {
|
33 |
+
position: sticky;
|
34 |
+
top: 0;
|
35 |
+
background-color: #1E1E1E;
|
36 |
+
z-index: 10;
|
37 |
+
}
|
38 |
</style>
|
39 |
""",
|
40 |
unsafe_allow_html=True
|
41 |
)
|
42 |
|
43 |
# Load the League of Legends dataset
|
44 |
+
st.title("League of Legends Game Win Predictor")
|
45 |
+
st.markdown("#### Predict whether the Blue Team will dominate the game using Random Forest Classifier", unsafe_allow_html=True)
|
46 |
|
47 |
# Load dataset directly
|
48 |
file_path = 'high_diamond_ranked_10min.csv'
|
49 |
df = pd.read_csv(file_path)
|
|
|
|
|
50 |
|
51 |
+
# Preprocess data and train model (runs once)
|
52 |
df = df[['blueFirstBlood', 'blueKills', 'blueDeaths', 'blueAssists', 'blueTotalGold', 'blueTotalExperience', 'blueDragons', 'blueHeralds', 'blueTowersDestroyed', 'blueWins']]
|
53 |
df = df.dropna()
|
54 |
|
|
|
69 |
model.fit(X_train, y_train)
|
70 |
y_pred = model.predict(X_test)
|
71 |
|
72 |
+
# Top Tabs Navigation
|
73 |
+
tab1, tab2, tab3 = st.tabs(["๐ Dataset", "๐ Visualization", "๐ฎ Prediction"])
|
74 |
+
|
75 |
+
# Dataset Section
|
76 |
+
with tab1:
|
77 |
+
st.write("### ๐ Dataset Preview")
|
78 |
+
st.dataframe(df.head())
|
79 |
+
|
80 |
+
# Visualization Section
|
81 |
+
with tab2:
|
82 |
+
# Display model performance
|
83 |
+
accuracy = accuracy_score(y_test, y_pred)
|
84 |
+
st.write("### ๐ฅ Model Performance")
|
85 |
+
st.write(f"**โ
Model Accuracy:** {accuracy:.2f}")
|
86 |
+
|
87 |
+
# Visualizing performance
|
88 |
+
st.write("### ๐ Performance Breakdown")
|
89 |
+
conf_matrix = confusion_matrix(y_test, y_pred)
|
90 |
+
st.write("Confusion Matrix:")
|
91 |
+
fig, ax = plt.subplots()
|
92 |
+
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='coolwarm', ax=ax)
|
93 |
+
st.pyplot(fig)
|
94 |
+
|
95 |
+
# Feature Importance Visualization
|
96 |
+
st.write("### ๐ Feature Importance")
|
97 |
+
feature_importance = abs(model.coef_[0])
|
98 |
+
features = X.columns
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
fig, ax = plt.subplots()
|
100 |
+
ax.barh(features, feature_importance, color='#4A90E2')
|
101 |
+
ax.set_title("Feature Importance for Blue Team Win Prediction")
|
102 |
+
ax.set_xlabel("Importance")
|
|
|
103 |
st.pyplot(fig)
|
104 |
+
|
105 |
+
# Prediction Section
|
106 |
+
with tab3:
|
107 |
+
st.write("### ๐ฎ Predict Game Outcome")
|
108 |
+
st.markdown("Adjust the stats below to simulate a match scenario!")
|
109 |
+
|
110 |
+
first_blood = st.selectbox("Did Blue Team Get First Blood?", [0, 1])
|
111 |
+
kills = st.slider("Blue Team Kills", min_value=0, max_value=50, value=5)
|
112 |
+
deaths = st.slider("Blue Team Deaths", min_value=0, max_value=50, value=5)
|
113 |
+
assists = st.slider("Blue Team Assists", min_value=0, max_value=50, value=10)
|
114 |
+
total_gold = st.slider("Blue Team Total Gold", min_value=10000, max_value=100000, value=50000)
|
115 |
+
total_exp = st.slider("Blue Team Total Experience", min_value=10000, max_value=100000, value=50000)
|
116 |
+
dragons = st.slider("Blue Team Dragons Taken", min_value=0, max_value=5, value=1)
|
117 |
+
heralds = st.slider("Blue Team Heralds Taken", min_value=0, max_value=2, value=0)
|
118 |
+
towers = st.slider("Blue Team Towers Destroyed", min_value=0, max_value=11, value=2)
|
119 |
+
|
120 |
+
if st.button("โจ Predict Win"):
|
121 |
+
input_data = scaler.transform([[first_blood, kills, deaths, assists, total_gold, total_exp, dragons, heralds, towers]])
|
122 |
+
prediction = model.predict(input_data)[0]
|
123 |
+
prediction_proba = model.predict_proba(input_data)[0]
|
124 |
+
|
125 |
+
st.subheader("๐ฎ Prediction Result")
|
126 |
+
result_text = "๐
Blue Team is likely to WIN! ๐" if prediction == 1 else "โ๏ธ Blue Team is likely to LOSE. ๐"
|
127 |
+
st.success(result_text) if prediction == 1 else st.error(result_text)
|
128 |
+
st.write(f"Confidence: {prediction_proba[prediction]:.2f}")
|
129 |
+
|
130 |
+
# Win/Loss Bar Chart
|
131 |
+
st.write("### ๐ Win Probability Breakdown")
|
132 |
+
fig, ax = plt.subplots()
|
133 |
+
ax.bar(["Win", "Lose"], [prediction_proba[1], prediction_proba[0]], color=["#64FFDA", "#FF4C4C"])
|
134 |
+
ax.set_ylim(0, 1)
|
135 |
+
ax.set_ylabel("Probability")
|
136 |
+
ax.set_title("Blue Team Win/Loss Probability")
|
137 |
+
st.pyplot(fig)
|