Harika22 commited on
Commit
c0573cd
·
verified ·
1 Parent(s): ee2f3a0

Update pages/11_Ensembling_Techniques.py

Browse files
Files changed (1) hide show
  1. pages/11_Ensembling_Techniques.py +74 -0
pages/11_Ensembling_Techniques.py CHANGED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
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.neighbors import KNeighborsClassifier
9
+ from sklearn.model_selection import train_test_split
10
+ from sklearn.metrics import accuracy_score
11
+ from matplotlib.colors import ListedColormap
12
+
13
+ st.set_page_config(page_title="Ensemble Learning Visualized", page_icon="🧭", layout="wide")
14
+ st.title("🗺️ Ensemble Learning Techniques")
15
+
16
+ X, y = make_classification(n_samples=300, n_features=2, n_redundant=0,
17
+ n_clusters_per_class=1, n_classes=2, random_state=42)
18
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
19
+
20
+ technique = st.radio("Choose Ensemble Technique:", ["Voting Classifier", "Bagging", "Random Forest"])
21
+
22
+ cm = plt.cm.RdYlBu
23
+ cm_bright = ListedColormap(["#FF4C60", "#1f78d1"])
24
+
25
+ def plot_decision_boundary(clf, X, y, title):
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, 300), np.linspace(y_min, y_max, 300))
29
+ Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
30
+ Z = Z.reshape(xx.shape)
31
+ fig, ax = plt.subplots()
32
+ ax.contourf(xx, yy, Z, cmap=cm, alpha=0.6)
33
+ scatter = ax.scatter(X[:, 0], X[:, 1], c=y, cmap=cm_bright, edgecolors='k')
34
+ ax.set_title(title)
35
+ ax.set_xlabel("Feature 1")
36
+ ax.set_ylabel("Feature 2")
37
+ st.pyplot(fig)
38
+
39
+ if technique == "Voting Classifier":
40
+ st.subheader("🗳️ Voting Classifier")
41
+ clf1 = LogisticRegression()
42
+ clf2 = KNeighborsClassifier(n_neighbors=5)
43
+ clf3 = DecisionTreeClassifier()
44
+ eclf = VotingClassifier(estimators=[('lr', clf1), ('knn', clf2), ('dt', clf3)], voting='hard')
45
+ eclf.fit(X_train, y_train)
46
+ y_pred = eclf.predict(X_test)
47
+ acc = accuracy_score(y_test, y_pred)
48
+ st.write(f"**Accuracy**: {acc:.2f}")
49
+ plot_decision_boundary(eclf, X_test, y_test, "Voting Classifier Decision Region")
50
+
51
+ elif technique == "Bagging":
52
+ st.subheader("🧺 Bagging with Decision Trees")
53
+ base_model = DecisionTreeClassifier()
54
+ bagging = BaggingClassifier(base_model, n_estimators=10, random_state=42)
55
+ bagging.fit(X_train, y_train)
56
+ y_pred = bagging.predict(X_test)
57
+ acc = accuracy_score(y_test, y_pred)
58
+ st.write(f"**Accuracy**: {acc:.2f}")
59
+ plot_decision_boundary(bagging, X_test, y_test, "Bagging Decision Region")
60
+
61
+ elif technique == "Random Forest":
62
+ st.subheader("🌲 Random Forest")
63
+ rf = RandomForestClassifier(n_estimators=100, random_state=42)
64
+ rf.fit(X_train, y_train)
65
+ y_pred = rf.predict(X_test)
66
+ acc = accuracy_score(y_test, y_pred)
67
+ st.write(f"**Accuracy**: {acc:.2f}")
68
+ plot_decision_boundary(rf, X_test, y_test, "Random Forest Decision Region")
69
+
70
+ st.markdown("---")
71
+ st.markdown("""
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
+ """)