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

Create bagging_regressor_viz.py

Browse files
Files changed (1) hide show
  1. bagging_regressor_viz.py +96 -0
bagging_regressor_viz.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ import streamlit as st
4
+ from sklearn.tree import DecisionTreeRegressor
5
+ from sklearn.ensemble import BaggingRegressor
6
+ from sklearn.svm import SVR
7
+ from sklearn.neighbors import KNeighborsRegressor
8
+ from sklearn.metrics import r2_score
9
+
10
+ plt.style.use('seaborn-v0_8-bright')
11
+
12
+ n_train = 150
13
+ n_test = 100
14
+ noise = 0.1
15
+
16
+ np.random.seed(0)
17
+
18
+ # Generate data
19
+ def f(x):
20
+ x = x.ravel()
21
+ return np.exp(-x ** 2) + 1.5 * np.exp(-(x - 2) ** 2)
22
+
23
+ def generate(n_samples, noise):
24
+ X = np.random.rand(n_samples) * 10 - 5
25
+ X = np.sort(X).ravel()
26
+ y = np.exp(-X ** 2) + 1.5 * np.exp(-(X - 2) ** 2) + np.random.normal(0.0, noise, n_samples)
27
+ X = X.reshape((n_samples, 1))
28
+ return X, y
29
+
30
+ X_train, y_train = generate(n_samples=n_train, noise=noise)
31
+ X_test, y_test = generate(n_samples=n_test, noise=noise)
32
+
33
+ st.sidebar.markdown("# Bagging Regressor")
34
+
35
+ estimator = st.sidebar.selectbox(
36
+ 'Select base estimator',
37
+ ('Decision Tree', 'SVM', 'KNN')
38
+ )
39
+
40
+ n_estimators = int(st.sidebar.number_input('Enter number of estimators', min_value=1, value=10))
41
+
42
+ max_samples = st.sidebar.slider('Max Samples', 1, n_train, n_train, step=25)
43
+
44
+ bootstrap_samples = st.sidebar.radio(
45
+ "Bootstrap Samples",
46
+ ('True', 'False')
47
+ ) == 'True' # Convert string to boolean
48
+
49
+ # Load initial graph
50
+ fig, ax = plt.subplots()
51
+
52
+ # Plot initial graph
53
+ ax.scatter(X_train, y_train, color="yellow", edgecolor="black")
54
+ orig = st.pyplot(fig)
55
+
56
+ if st.sidebar.button('Run Algorithm'):
57
+
58
+ if estimator == 'Decision Tree':
59
+ algo = DecisionTreeRegressor()
60
+ elif estimator == 'SVM':
61
+ algo = SVR()
62
+ else:
63
+ algo = KNeighborsRegressor()
64
+
65
+ reg = algo.fit(X_train, y_train)
66
+
67
+ bag_reg = BaggingRegressor(
68
+ estimator=algo, # Updated parameter name
69
+ n_estimators=n_estimators,
70
+ max_samples=max_samples,
71
+ bootstrap=bootstrap_samples
72
+ ).fit(X_train, y_train)
73
+
74
+ bag_reg_predict = bag_reg.predict(X_test)
75
+ reg_predict = reg.predict(X_test)
76
+
77
+ # R2 scores
78
+ bag_r2 = r2_score(y_test, bag_reg_predict)
79
+ reg_r2 = r2_score(y_test, reg_predict)
80
+
81
+ orig.empty()
82
+
83
+ fig, ax = plt.subplots()
84
+ fig1, ax1 = plt.subplots()
85
+
86
+ st.subheader(f"Bagging - {estimator} (R2 score - {bag_r2:.2f})")
87
+ ax1.scatter(X_train, y_train, color="yellow", edgecolor="black")
88
+ ax1.plot(X_test, bag_reg_predict, linewidth=1, label="Bagging")
89
+ ax1.legend()
90
+ st.pyplot(fig1)
91
+
92
+ st.subheader(f"{estimator} (R2 score - {reg_r2:.2f})")
93
+ ax.scatter(X_train, y_train, color="yellow", edgecolor="black")
94
+ ax.plot(X_test, reg_predict, linewidth=1, color='red', label=estimator)
95
+ ax.legend()
96
+ st.pyplot(fig)