Spaces:
Sleeping
Sleeping
Update pages/11_Ensembling_Techniques.py
Browse files
pages/11_Ensembling_Techniques.py
CHANGED
|
@@ -1,74 +1,88 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from sklearn.datasets import make_classification
|
| 5 |
from sklearn.ensemble import VotingClassifier, BaggingClassifier, RandomForestClassifier
|
| 6 |
-
from sklearn.linear_model import LogisticRegression
|
| 7 |
from sklearn.tree import DecisionTreeClassifier
|
| 8 |
-
from sklearn.
|
| 9 |
-
from sklearn.
|
| 10 |
from sklearn.metrics import accuracy_score
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
st.set_page_config(page_title="Ensemble
|
| 14 |
-
st.title("πΊοΈ Ensemble Learning Techniques")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
def plot_decision_boundary(
|
| 26 |
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
|
| 27 |
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
|
| 28 |
-
xx, yy = np.meshgrid(np.linspace(x_min, x_max,
|
| 29 |
-
|
|
|
|
| 30 |
Z = Z.reshape(xx.shape)
|
|
|
|
| 31 |
fig, ax = plt.subplots()
|
| 32 |
-
ax.contourf(xx, yy, Z,
|
| 33 |
-
|
| 34 |
ax.set_title(title)
|
| 35 |
-
ax.set_xlabel("Feature 1")
|
| 36 |
-
ax.set_ylabel("Feature 2")
|
| 37 |
st.pyplot(fig)
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
st.subheader("π³οΈ Voting Classifier")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
clf1 = LogisticRegression()
|
| 42 |
-
clf2 =
|
| 43 |
-
clf3 = DecisionTreeClassifier()
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
acc = accuracy_score(y_test, y_pred)
|
| 48 |
-
st.write(f"**Accuracy
|
| 49 |
-
plot_decision_boundary(
|
|
|
|
| 50 |
|
| 51 |
-
elif
|
| 52 |
-
st.subheader("π§Ί Bagging
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
acc = accuracy_score(y_test, y_pred)
|
| 58 |
-
st.write(f"**Accuracy
|
| 59 |
-
plot_decision_boundary(
|
|
|
|
| 60 |
|
| 61 |
-
elif
|
| 62 |
st.subheader("π² Random Forest")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
acc = accuracy_score(y_test, y_pred)
|
| 67 |
-
st.write(f"**Accuracy
|
| 68 |
-
plot_decision_boundary(
|
|
|
|
| 69 |
|
| 70 |
st.markdown("---")
|
| 71 |
-
st.
|
| 72 |
-
This interactive app helps you understand **Ensemble Methods** using decision boundaries.
|
| 73 |
-
Select from Voting, Bagging, and Random Forest and visualize how they classify data differently.
|
| 74 |
-
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from sklearn.datasets import make_moons
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
|
|
|
| 4 |
from sklearn.ensemble import VotingClassifier, BaggingClassifier, RandomForestClassifier
|
|
|
|
| 5 |
from sklearn.tree import DecisionTreeClassifier
|
| 6 |
+
from sklearn.linear_model import LogisticRegression
|
| 7 |
+
from sklearn.svm import SVC
|
| 8 |
from sklearn.metrics import accuracy_score
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
import numpy as np
|
| 11 |
|
| 12 |
+
st.set_page_config(page_title="Ensemble Methods", page_icon="π€", layout="wide")
|
|
|
|
| 13 |
|
| 14 |
+
st.markdown("<h1 style='text-align: center;'>π€ Ensemble Learning Visualized</h1>", unsafe_allow_html=True)
|
| 15 |
+
st.markdown("### Select an Ensemble Method from the options below:")
|
|
|
|
| 16 |
|
| 17 |
+
st.sidebar.title("π€ Choose an Ensemble Technique")
|
| 18 |
+
model_choice = st.sidebar.radio("Select Ensemble Method:", ["Voting", "Bagging", "Random Forest"])
|
| 19 |
|
| 20 |
+
X, y = make_moons(n_samples=300, noise=0.25, random_state=42)
|
| 21 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
| 22 |
|
| 23 |
+
def plot_decision_boundary(model, X, y, title):
|
| 24 |
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
|
| 25 |
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
|
| 26 |
+
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200),
|
| 27 |
+
np.linspace(y_min, y_max, 200))
|
| 28 |
+
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
|
| 29 |
Z = Z.reshape(xx.shape)
|
| 30 |
+
|
| 31 |
fig, ax = plt.subplots()
|
| 32 |
+
ax.contourf(xx, yy, Z, alpha=0.3, cmap='RdYlBu')
|
| 33 |
+
ax.scatter(X[:, 0], X[:, 1], c=y, cmap='RdYlBu', edgecolor='k')
|
| 34 |
ax.set_title(title)
|
|
|
|
|
|
|
| 35 |
st.pyplot(fig)
|
| 36 |
|
| 37 |
+
# Model Training and Plotting
|
| 38 |
+
if model_choice == "Voting":
|
| 39 |
st.subheader("π³οΈ Voting Classifier")
|
| 40 |
+
st.write("""
|
| 41 |
+
Combines multiple classifiers (Logistic Regression, SVM, and Decision Tree) to vote on predictions.
|
| 42 |
+
You can choose between **Hard Voting** (majority class) and **Soft Voting** (average probabilities).
|
| 43 |
+
""")
|
| 44 |
clf1 = LogisticRegression()
|
| 45 |
+
clf2 = SVC(probability=True)
|
| 46 |
+
clf3 = DecisionTreeClassifier(max_depth=5)
|
| 47 |
+
|
| 48 |
+
voting_clf = VotingClassifier(estimators=[
|
| 49 |
+
('lr', clf1), ('svc', clf2), ('dt', clf3)],
|
| 50 |
+
voting='soft')
|
| 51 |
+
|
| 52 |
+
voting_clf.fit(X_train, y_train)
|
| 53 |
+
y_pred = voting_clf.predict(X_test)
|
| 54 |
acc = accuracy_score(y_test, y_pred)
|
| 55 |
+
st.write(f"π― **Accuracy:** {acc:.2f}")
|
| 56 |
+
plot_decision_boundary(voting_clf, X, y, "Voting Classifier Decision Region")
|
| 57 |
+
st.markdown("π [Open Voting Ensemble Notebook](https://colab.research.google.com/drive/1LPZR9RnvEXP8mzOLOBfSVVyHHZ7GFns4?usp=sharing)", unsafe_allow_html=True)
|
| 58 |
|
| 59 |
+
elif model_choice == "Bagging":
|
| 60 |
+
st.subheader("π§Ί Bagging Classifier")
|
| 61 |
+
st.write("""
|
| 62 |
+
Trains multiple Decision Trees on random subsets (with replacement) of data and averages their predictions.
|
| 63 |
+
Helps reduce variance and overfitting.
|
| 64 |
+
""")
|
| 65 |
+
bag_clf = BaggingClassifier(DecisionTreeClassifier(), n_estimators=100, random_state=42)
|
| 66 |
+
bag_clf.fit(X_train, y_train)
|
| 67 |
+
y_pred = bag_clf.predict(X_test)
|
| 68 |
acc = accuracy_score(y_test, y_pred)
|
| 69 |
+
st.write(f"π― **Accuracy:** {acc:.2f}")
|
| 70 |
+
plot_decision_boundary(bag_clf, X, y, "Bagging Classifier Decision Region")
|
| 71 |
+
st.markdown("π [Open Bagging Ensemble Notebook](https://colab.research.google.com/drive/1cumZl7H9fqyORfaw236WWxQViJxvSKHV?usp=sharing)", unsafe_allow_html=True)
|
| 72 |
|
| 73 |
+
elif model_choice == "Random Forest":
|
| 74 |
st.subheader("π² Random Forest")
|
| 75 |
+
st.write("""
|
| 76 |
+
A forest of randomized decision trees.
|
| 77 |
+
Each tree sees a bootstrapped sample and a random subset of features at every split.
|
| 78 |
+
""")
|
| 79 |
+
rf_clf = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 80 |
+
rf_clf.fit(X_train, y_train)
|
| 81 |
+
y_pred = rf_clf.predict(X_test)
|
| 82 |
acc = accuracy_score(y_test, y_pred)
|
| 83 |
+
st.write(f"π― **Accuracy:** {acc:.2f}")
|
| 84 |
+
plot_decision_boundary(rf_clf, X, y, "Random Forest Decision Region")
|
| 85 |
+
st.markdown("π [Open Random Forest Notebook](https://colab.research.google.com/drive/1S6YyfTx9N35E5fpPF0z6ZDm85BSp1deT?usp=sharing)", unsafe_allow_html=True)
|
| 86 |
|
| 87 |
st.markdown("---")
|
| 88 |
+
st.success("β
Ensemble techniques improve model stability, reduce overfitting, and deliver better results. Try them on your data!")
|
|
|
|
|
|
|
|
|