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

Update pages/11_Ensembling_Techniques.py

Browse files
Files changed (1) hide show
  1. pages/11_Ensembling_Techniques.py +62 -48
pages/11_Ensembling_Techniques.py CHANGED
@@ -1,74 +1,88 @@
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
- """)
 
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!")