suvradeepp commited on
Commit
21c4b10
·
verified ·
1 Parent(s): ea1fbd3

Create bagging_classifier_viz.py

Browse files
Files changed (1) hide show
  1. bagging_classifier_viz.py +89 -0
bagging_classifier_viz.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import streamlit as st
3
+ import numpy as np
4
+ from sklearn.model_selection import train_test_split
5
+ from sklearn.datasets import make_moons
6
+ from sklearn.tree import DecisionTreeClassifier
7
+ from sklearn.neighbors import KNeighborsClassifier
8
+ from sklearn.svm import SVC
9
+ from sklearn.ensemble import BaggingClassifier
10
+ from sklearn.metrics import accuracy_score
11
+
12
+ # Generate data
13
+ X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
14
+ X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
15
+
16
+ # Function to draw meshgrid for decision boundary visualization
17
+ def draw_meshgrid():
18
+ a = np.arange(start=X[:, 0].min() - 1, stop=X[:, 0].max() + 1, step=0.01)
19
+ b = np.arange(start=X[:, 1].min() - 1, stop=X[:, 1].max() + 1, step=0.01)
20
+ XX, YY = np.meshgrid(a, b)
21
+ input_array = np.array([XX.ravel(), YY.ravel()]).T
22
+ return XX, YY, input_array
23
+
24
+ plt.style.use('seaborn-v0_8-bright')
25
+
26
+ st.sidebar.markdown("# Bagging Classifier")
27
+
28
+ # Sidebar inputs
29
+ estimators = st.sidebar.selectbox(
30
+ 'Select base estimator',
31
+ ('Decision Tree', 'KNN', 'SVM')
32
+ )
33
+
34
+ n_estimators = int(st.sidebar.number_input('Enter number of estimators', min_value=1, value=10))
35
+ max_samples = st.sidebar.slider('Max Samples', 1, 375, 375, step=25)
36
+ bootstrap_samples = st.sidebar.radio("Bootstrap Samples", ('True', 'False')) == 'True'
37
+ max_features = st.sidebar.slider('Max Features', 1, 2, 2, key=1234)
38
+ bootstrap_features = st.sidebar.radio("Bootstrap Features", ('False', 'True'), key=2345) == 'True'
39
+
40
+ # Load initial graph
41
+ fig, ax = plt.subplots()
42
+ ax.scatter(X.T[0], X.T[1], c=y, cmap='rainbow')
43
+ orig = st.pyplot(fig)
44
+
45
+ if st.sidebar.button('Run Algorithm'):
46
+ if estimators == "Decision Tree":
47
+ estimator = DecisionTreeClassifier()
48
+ elif estimators == "KNN":
49
+ estimator = KNeighborsClassifier()
50
+ else:
51
+ estimator = SVC()
52
+
53
+ clf = estimator.fit(X_train, y_train)
54
+ y_pred_tree = clf.predict(X_test)
55
+
56
+ bag_clf = BaggingClassifier(
57
+ estimator=estimator,
58
+ n_estimators=n_estimators,
59
+ max_samples=max_samples,
60
+ bootstrap=bootstrap_samples,
61
+ max_features=max_features,
62
+ bootstrap_features=bootstrap_features,
63
+ random_state=42
64
+ )
65
+ bag_clf.fit(X_train, y_train)
66
+ y_pred = bag_clf.predict(X_test)
67
+
68
+ orig.empty()
69
+
70
+ fig, ax = plt.subplots()
71
+ fig1, ax1 = plt.subplots()
72
+
73
+ XX, YY, input_array = draw_meshgrid()
74
+ labels = clf.predict(input_array)
75
+ labels1 = bag_clf.predict(input_array)
76
+
77
+ col1, col2 = st.columns(2)
78
+ with col1:
79
+ st.header(estimators)
80
+ ax.scatter(X.T[0], X.T[1], c=y, cmap='rainbow')
81
+ ax.contourf(XX, YY, labels.reshape(XX.shape), alpha=0.5, cmap='rainbow')
82
+ orig = st.pyplot(fig)
83
+ st.subheader(f"Accuracy for {estimators}: {round(accuracy_score(y_test, y_pred_tree), 2)}")
84
+ with col2:
85
+ st.header("Bagging Classifier")
86
+ ax1.scatter(X.T[0], X.T[1], c=y, cmap='rainbow')
87
+ ax1.contourf(XX, YY, labels1.reshape(XX.shape), alpha=0.5, cmap='rainbow')
88
+ orig1 = st.pyplot(fig1)
89
+ st.subheader(f"Accuracy for Bagging: {round(accuracy_score(y_test, y_pred), 2)}")