suvradeepp commited on
Commit
49575d3
·
1 Parent(s): 42deeb1
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)}")
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)
decision_tree_steps.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.metrics import accuracy_score
8
+ from sklearn.tree import plot_tree
9
+ from sklearn.tree import export_graphviz
10
+ from os import system
11
+ from graphviz import Source
12
+ from sklearn import tree
13
+
14
+ def draw_meshgrid():
15
+ a = np.arange(start=X[:, 0].min() - 1, stop=X[:, 0].max() + 1, step=0.01)
16
+ b = np.arange(start=X[:, 1].min() - 1, stop=X[:, 1].max() + 1, step=0.01)
17
+
18
+ XX, YY = np.meshgrid(a, b)
19
+
20
+ input_array = np.array([XX.ravel(), YY.ravel()]).T
21
+
22
+ return XX, YY, input_array
23
+
24
+ X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
25
+ X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
26
+
27
+ plt.style.use('fivethirtyeight')
28
+
29
+ st.sidebar.markdown("# Decision Tree Classifier")
30
+
31
+ criterion = st.sidebar.selectbox(
32
+ 'Criterion',
33
+ ('gini', 'entropy')
34
+ )
35
+
36
+ splitter = st.sidebar.selectbox(
37
+ 'Splitter',
38
+ ('best', 'random')
39
+ )
40
+
41
+ max_depth = int(st.sidebar.number_input('Max Depth'))
42
+
43
+ min_samples_split = st.sidebar.slider('Min Samples Split', 1, X_train.shape[0], 2,key=1234)
44
+
45
+ min_samples_leaf = st.sidebar.slider('Min Samples Leaf', 1, X_train.shape[0], 1,key=1235)
46
+
47
+ max_features = st.sidebar.slider('Max Features', 1, 2, 2,key=1236)
48
+
49
+ max_leaf_nodes = int(st.sidebar.number_input('Max Leaf Nodes'))
50
+
51
+ min_impurity_decrease = st.sidebar.number_input('Min Impurity Decrease')
52
+
53
+ # Load initial graph
54
+ fig, ax = plt.subplots()
55
+
56
+ # Plot initial graph
57
+ ax.scatter(X.T[0], X.T[1], c=y, cmap='rainbow')
58
+ orig = st.pyplot(fig)
59
+
60
+ if st.sidebar.button('Run Algorithm'):
61
+
62
+ orig.empty()
63
+
64
+ if max_depth == 0:
65
+ max_depth = None
66
+
67
+ if max_leaf_nodes == 0:
68
+ max_leaf_nodes = None
69
+
70
+ clf = DecisionTreeClassifier(criterion=criterion,splitter=splitter,max_depth=max_depth,random_state=42,min_samples_split=min_samples_split,min_samples_leaf=min_samples_leaf,max_features=max_features,max_leaf_nodes=max_leaf_nodes,min_impurity_decrease=min_impurity_decrease)
71
+ clf.fit(X_train, y_train)
72
+ y_pred = clf.predict(X_test)
73
+
74
+ XX, YY, input_array = draw_meshgrid()
75
+ labels = clf.predict(input_array)
76
+
77
+ ax.contourf(XX, YY, labels.reshape(XX.shape), alpha=0.5, cmap='rainbow')
78
+ plt.xlabel("Col1")
79
+ plt.ylabel("Col2")
80
+ orig = st.pyplot(fig)
81
+ st.subheader("Accuracy for Decision Tree " + str(round(accuracy_score(y_test, y_pred), 2)))
82
+
83
+ tree = export_graphviz(clf,feature_names=["Col1","Col2"])
84
+
85
+ st.graphviz_chart(tree)
gd_sgd_app.py ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import plotly.graph_objs as go
4
+
5
+ def convex_function(x, y):
6
+ return x**2 + y**2
7
+
8
+ def non_convex_function(x, y):
9
+ return np.sin(x) * np.cos(y) * x * y
10
+
11
+ def gradient_descent(func, grad_func, start, learning_rate, n_iter):
12
+ path = [start]
13
+ for _ in range(n_iter):
14
+ grad = grad_func(path[-1])
15
+ next_point = path[-1] - learning_rate * grad
16
+ path.append(next_point)
17
+ return np.array(path)
18
+
19
+ def stochastic_gradient_descent(func, grad_func, start, learning_rate, n_iter):
20
+ path = [start]
21
+ for _ in range(n_iter):
22
+ grad = grad_func(path[-1]) + np.random.normal(0, 0.1, 2)
23
+ next_point = path[-1] - learning_rate * grad
24
+ path.append(next_point)
25
+ return np.array(path)
26
+
27
+ def grad_convex(point):
28
+ x, y = point
29
+ return np.array([2*x, 2*y])
30
+
31
+ def grad_non_convex(point):
32
+ x, y = point
33
+ return np.array([np.cos(x) * np.cos(y) * y + np.sin(x) * np.sin(y) * x, np.cos(x) * np.cos(y) * x - np.sin(x) * np.sin(y) * y])
34
+
35
+ def simulated_annealing(func, start, temp, cooling_rate, n_iter):
36
+ path = [start]
37
+ current_point = start
38
+ lowest_point = current_point
39
+ for i in range(n_iter):
40
+ next_point = current_point + np.random.normal(0, 1, 2)
41
+ delta_E = func(next_point[0], next_point[1]) - func(current_point[0], current_point[1])
42
+ if delta_E < 0 or np.exp(-delta_E / temp) > np.random.rand():
43
+ current_point = next_point
44
+ if func(current_point[0], current_point[1]) < func(lowest_point[0], lowest_point[1]):
45
+ lowest_point = current_point
46
+ path.append(current_point)
47
+ temp *= cooling_rate
48
+ return np.array(path), lowest_point
49
+
50
+ def plot_3d_surface(func, path, title, alphas=None, lowest_point=None):
51
+ x_min, x_max = min(path[:, 0].min(), -6), max(path[:, 0].max(), 6)
52
+ y_min, y_max = min(path[:, 1].min(), -6), max(path[:, 1].max(), 6)
53
+
54
+ x = np.linspace(x_min, x_max, 200)
55
+ y = np.linspace(y_min, y_max, 200)
56
+ X, Y = np.meshgrid(x, y)
57
+ Z = func(X, Y)
58
+
59
+ fig = go.Figure(data=[go.Surface(z=Z, x=X, y=Y, opacity=0.7)])
60
+ if alphas is None:
61
+ alphas = [1.0] * len(path)
62
+
63
+ for i in range(len(path) - 1):
64
+ fig.add_trace(go.Scatter3d(
65
+ x=path[i:i+2, 0],
66
+ y=path[i:i+2, 1],
67
+ z=func(path[i:i+2, 0], path[i:i+2, 1]),
68
+ mode='lines',
69
+ line=dict(color='orange', width=4),
70
+ opacity=alphas[i],
71
+ showlegend=False
72
+ ))
73
+ fig.add_trace(go.Scatter3d(
74
+ x=path[:, 0],
75
+ y=path[:, 1],
76
+ z=func(path[:, 0], path[:, 1]),
77
+ mode='markers',
78
+ marker=dict(size=4, color='orange', opacity=alphas[-1]),
79
+ name='Path'
80
+ ))
81
+ fig.add_trace(go.Scatter3d(
82
+ x=[path[0, 0]],
83
+ y=[path[0, 1]],
84
+ z=[func(path[0, 0], path[0, 1])],
85
+ mode='markers',
86
+ marker=dict(size=6, color='green', opacity=alphas[0]),
87
+ name='Start'
88
+ ))
89
+
90
+ if lowest_point is not None:
91
+ fig.add_trace(go.Scatter3d(
92
+ x=[lowest_point[0]],
93
+ y=[lowest_point[1]],
94
+ z=[func(lowest_point[0], lowest_point[1])],
95
+ mode='markers',
96
+ marker=dict(size=6, color='red', opacity=alphas[-1]),
97
+ name='Lowest Observed'
98
+ ))
99
+
100
+ fig.update_layout(title=title, scene=dict(
101
+ xaxis_title='X',
102
+ yaxis_title='Y',
103
+ zaxis_title='Z'))
104
+ return fig
105
+
106
+ st.title("Convex and Non-Convex SGD Optimization")
107
+
108
+
109
+ tab1, tab2, tab3 = st.tabs(["Gradient Descent", "Stochastic Gradient Descent", "Simulated Annealing"])
110
+
111
+ st.sidebar.header("Parameters")
112
+
113
+ learning_rate = st.sidebar.slider("Learning Rate", 0.01, 1.0, 0.1)
114
+ n_iter = st.sidebar.slider("Number of Iterations", 10, 100, 50)
115
+ convex_start_x = st.sidebar.slider("Convex Start X", -3.0, 3.0, 2.5)
116
+ convex_start_y = st.sidebar.slider("Convex Start Y", -3.0, 3.0, 2.5)
117
+ non_convex_start_x = st.sidebar.slider("Non-Convex Start X", -3.0, 3.0, 2.5)
118
+ non_convex_start_y = st.sidebar.slider("Non-Convex Start Y", -3.0, 3.0, 2.5)
119
+ temp = st.sidebar.slider("Initial Temperature (Simulated Annealing)", 1.0, 10.0, 5.0)
120
+ cooling_rate = st.sidebar.slider("Cooling Rate (Simulated Annealing)", 0.8, 0.99, 0.95)
121
+
122
+ convex_start = np.array([convex_start_x, convex_start_y])
123
+ non_convex_start = np.array([non_convex_start_x, non_convex_start_y])
124
+
125
+ with tab1:
126
+ st.header("Gradient Descent")
127
+ st.write("Visualizing gradient descent on convex and non-convex functions.")
128
+
129
+ with st.expander("Gradient Descent Algorithm and Math"):
130
+ st.markdown(r"""
131
+ ### Gradient Descent Algorithm
132
+ **Step-by-step Algorithm**:
133
+ 1. Initialize starting point $\mathbf{x}_0$.
134
+ 2. For each iteration $t$:
135
+ - Compute the gradient $\nabla f(\mathbf{x}_t)$.
136
+ - Update the current point: $\mathbf{x}_{t+1} = \mathbf{x}_t - \alpha \nabla f(\mathbf{x}_t)$.
137
+ **Mathematical Formulation**:
138
+ $$
139
+ \mathbf{x}_{t+1} = \mathbf{x}_t - \alpha \nabla f(\mathbf{x}_t)
140
+ $$
141
+ where:
142
+ - $\mathbf{x}_t$ is the current point.
143
+ - $\alpha$ is the learning rate.
144
+ - $\nabla f(\mathbf{x}_t)$ is the gradient of the function at $\mathbf{x}_t$.
145
+ """)
146
+
147
+ convex_path_gd = gradient_descent(convex_function, grad_convex, convex_start, learning_rate, n_iter)
148
+ non_convex_path_gd = gradient_descent(non_convex_function, grad_non_convex, non_convex_start, learning_rate, n_iter)
149
+
150
+ st.plotly_chart(plot_3d_surface(convex_function, convex_path_gd, "Convex Function (GD)"))
151
+ st.plotly_chart(plot_3d_surface(non_convex_function, non_convex_path_gd, "Non-Convex Function (GD)"))
152
+
153
+ with tab2:
154
+ st.header("Stochastic Gradient Descent")
155
+ st.write("Visualizing stochastic gradient descent on convex and non-convex functions.")
156
+
157
+ with st.expander("Stochastic Gradient Descent Algorithm and Math"):
158
+ st.markdown(r"""
159
+ ### Stochastic Gradient Descent Algorithm
160
+ **Step-by-step Algorithm**:
161
+ 1. Initialize starting point $\mathbf{x}_0$.
162
+ 2. For each iteration $t$:
163
+ - Compute a stochastic approximation of the gradient $\nabla f(\mathbf{x}_t) + \text{noise}$.
164
+ - Update the current point: $\mathbf{x}_{t+1} = \mathbf{x}_t - \alpha \left(\nabla f(\mathbf{x}_t) + \text{noise}\right)$.
165
+ **Mathematical Formulation**:
166
+ $$
167
+ \mathbf{x}_{t+1} = \mathbf{x}_t - \alpha \left(\nabla f(\mathbf{x}_t) + \text{noise}\right)
168
+ $$
169
+ where:
170
+ - $\mathbf{x}_t$ is the current point.
171
+ - $\alpha$ is the learning rate.
172
+ - $\nabla f(\mathbf{x}_t)$ is the gradient of the function at $\mathbf{x}_t$.
173
+ - $\text{noise}$ is a small random perturbation.
174
+ """)
175
+
176
+ convex_path_sgd = stochastic_gradient_descent(convex_function, grad_convex, convex_start, learning_rate, n_iter)
177
+ non_convex_path_sgd = stochastic_gradient_descent(non_convex_function, grad_non_convex, non_convex_start, learning_rate, n_iter)
178
+
179
+ st.plotly_chart(plot_3d_surface(convex_function, convex_path_sgd, "Convex Function (SGD)"))
180
+ st.plotly_chart(plot_3d_surface(non_convex_function, non_convex_path_sgd, "Non-Convex Function (SGD)"))
181
+
182
+ with tab3:
183
+ st.header("Simulated Annealing")
184
+ st.write("Visualizing simulated annealing on a non-convex function.")
185
+
186
+ with st.expander("Simulated Annealing Algorithm and Math"):
187
+ st.markdown(r"""
188
+ ### Simulated Annealing Algorithm
189
+ **Step-by-step Algorithm**:
190
+ 1. Initialize starting point $\mathbf{x}_0$ and temperature $T$.
191
+ 2. For each iteration $t$:
192
+ - Generate a new point $\mathbf{x}'$ in the neighborhood of the current point $\mathbf{x}_t$.
193
+ - Compute the change in function value $\Delta E = f(\mathbf{x}') - f(\mathbf{x}_t)$.
194
+ - If $\Delta E < 0$, accept the new point $\mathbf{x}_{t+1} = \mathbf{x}'$.
195
+ - If $\Delta E \geq 0$, accept the new point with a probability $\exp\left(\frac{-\Delta E}{T}\right)$.
196
+ - Update the temperature $T$.
197
+ **Mathematical Formulation**:
198
+ $$
199
+ \mathbf{x}_{t+1} =
200
+ \begin{cases}
201
+ \mathbf{x}' & \text{if } \Delta E < 0 \\
202
+ \mathbf{x}' & \text{with probability } \exp\left(\frac{-\Delta E}{T}\right) \text{ if } \Delta E \geq 0 \\
203
+ \mathbf{x}_t & \text{otherwise}
204
+ \end{cases}
205
+ $$
206
+ where:
207
+ - $\mathbf{x}_t$ is the current point.
208
+ - $\mathbf{x}'$ is the new point.
209
+ - $T$ is the temperature.
210
+ - $\Delta E = f(\mathbf{x}') - f(\mathbf{x}_t)$ is the change in function value.
211
+ - $\exp\left(\frac{-\Delta E}{T}\right)$ is the acceptance probability.
212
+ """)
213
+
214
+ non_convex_path_sa, lowest_point = simulated_annealing(non_convex_function, non_convex_start, temp, cooling_rate, n_iter)
215
+
216
+ # Visualizing the path with alpha changing based on iteration
217
+ alphas = np.linspace(0.1, 1, len(non_convex_path_sa))
218
+ fig_sa = plot_3d_surface(non_convex_function, non_convex_path_sa, "Non-Convex Function (SA)", alphas=alphas, lowest_point=lowest_point)
219
+
220
+ # Adding blue points for other iteration's observed minimums
221
+ other_mins = non_convex_path_sa[:-1]
222
+ fig_sa.add_trace(go.Scatter3d(
223
+ x=other_mins[:, 0],
224
+ y=other_mins[:, 1],
225
+ z=non_convex_function(other_mins[:, 0], other_mins[:, 1]),
226
+ mode='markers',
227
+ marker=dict(size=4, color='blue'),
228
+ name='Observed Minima'
229
+ ))
230
+
231
+ # Adding the final minimum point in red
232
+ fig_sa.add_trace(go.Scatter3d(
233
+ x=[lowest_point[0]],
234
+ y=[lowest_point[1]],
235
+ z=[non_convex_function(lowest_point[0], lowest_point[1])],
236
+ mode='markers',
237
+ marker=dict(size=6, color='red'),
238
+ name='Lowest Observed'
239
+ ))
240
+
241
+ # Adding the starting point in green
242
+ fig_sa.add_trace(go.Scatter3d(
243
+ x=[non_convex_path_sa[0, 0]],
244
+ y=[non_convex_path_sa[0, 1]],
245
+ z=[non_convex_function(non_convex_path_sa[0, 0], non_convex_path_sa[0, 1])],
246
+ mode='markers',
247
+ marker=dict(size=6, color='green'),
248
+ name='Start'
249
+ ))
250
+
251
+ st.plotly_chart(fig_sa)
voting_classifier_viz.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import matplotlib.pyplot as plt
3
+ from sthelper import StHelper
4
+ import data_helper
5
+
6
+ # Import all datasets
7
+ concentric, linear, outlier, spiral, ushape, xor = data_helper.load_dataset()
8
+
9
+ # Configure matplotlib styling
10
+ plt.style.use('seaborn-v0_8-bright')
11
+
12
+ # Dataset selection dropdown
13
+ st.sidebar.markdown("# Voting Classifier")
14
+ dataset = st.sidebar.selectbox(
15
+ "Dataset",
16
+ ("U-Shaped", "Linearly Separable", "Outlier", "Two Spirals", "Concentric Circles", "XOR")
17
+ )
18
+
19
+ # Estimator multi-select
20
+ estimators = st.sidebar.multiselect(
21
+ 'Estimators',
22
+ [
23
+ 'KNN',
24
+ 'Logistic Regression',
25
+ 'Gaussian Naive Bayes',
26
+ 'SVM',
27
+ 'Random Forest'
28
+ ]
29
+ )
30
+
31
+ # Voting type radio button
32
+ voting_type = st.sidebar.radio(
33
+ "Voting Type",
34
+ (
35
+ 'hard',
36
+ 'soft',
37
+ )
38
+ )
39
+
40
+ st.header(dataset)
41
+ fig, ax = plt.subplots()
42
+
43
+ # Plot initial graph
44
+ df = data_helper.load_initial_graph(dataset, ax)
45
+ orig = st.pyplot(fig)
46
+
47
+ # Extract X and Y
48
+ X = df.iloc[:, :2].values
49
+ y = df.iloc[:, -1].values
50
+
51
+ # Create sthelper object
52
+ sthelper = StHelper(X, y)
53
+
54
+ # On button click
55
+ if st.sidebar.button("Run Algorithm"):
56
+ algos = sthelper.create_base_estimators(estimators, voting_type)
57
+ voting_clf, voting_clf_accuracy = sthelper.train_voting_classifier(algos, voting_type)
58
+ sthelper.draw_main_graph(voting_clf, ax)
59
+ orig.pyplot(fig)
60
+ figs = sthelper.plot_other_graphs(algos)
61
+
62
+ # Plot accuracies
63
+ st.sidebar.header("Classification Metrics")
64
+ st.sidebar.text("Voting Classifier accuracy: " + str(voting_clf_accuracy))
65
+ accuracies = sthelper.calculate_base_model_accuracy(algos)
66
+ for i in range(len(accuracies)):
67
+ st.sidebar.text("Accuracy for Model " + str(i + 1) + " - " + str(accuracies[i]))
68
+
69
+ counter = 0
70
+ for i in st.columns(len(figs)):
71
+ with i:
72
+ st.pyplot(figs[counter])
73
+ st.text(counter)
74
+ counter += 1
voting_regressor_viz.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import streamlit as st
3
+ from sklearn.linear_model import LinearRegression
4
+ from sklearn.svm import SVR
5
+ from sklearn.tree import DecisionTreeRegressor
6
+ from sklearn.ensemble import VotingRegressor
7
+ from sklearn.model_selection import train_test_split
8
+ import numpy as np
9
+ from sklearn.metrics import r2_score,mean_absolute_error
10
+
11
+ def train_voting_regressor(algos):
12
+
13
+ vr = VotingRegressor(algos)
14
+ vr.fit(X_train,y_train)
15
+
16
+ y_pred = vr.predict(X_test1)
17
+
18
+ r2 = r2_score(y_test1,y_pred)
19
+ mae = mean_absolute_error(y_test1,y_pred)
20
+
21
+ return vr,r2,mae
22
+
23
+ plt.style.use('seaborn-bright')
24
+
25
+ # Create a random dataset
26
+ rng = np.random.RandomState(1)
27
+ X = np.sort(5 * rng.rand(80, 1), axis=0)
28
+ y = np.sin(X).ravel()
29
+ y[::5] += 3 * (0.5 - rng.rand(16))
30
+
31
+ # Random state - 8
32
+ X_train,X_test1,y_train,y_test1 = train_test_split(X,y,test_size=0.1,random_state=8)
33
+
34
+ # Predict
35
+ X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
36
+
37
+
38
+ st.sidebar.markdown("# Voting Regressor")
39
+
40
+ # Estimator Multi-select
41
+ estimators = st.sidebar.multiselect(
42
+ 'Estimators',
43
+ [
44
+ 'Linear Regression',
45
+ 'SVR',
46
+ 'Decision Tree Regressor'
47
+ ]
48
+ )
49
+
50
+ # Build estimators
51
+ algos = []
52
+
53
+ if 'Linear Regression' in estimators:
54
+ lr_reg = LinearRegression()
55
+ algos.append(('lr', lr_reg))
56
+ if 'SVR' in estimators:
57
+ svr_reg = SVR()
58
+ algos.append(('svr', svr_reg))
59
+ if 'Decision Tree Regressor' in estimators:
60
+ dt_reg = DecisionTreeRegressor(max_depth=5)
61
+ algos.append(('dt', dt_reg))
62
+
63
+ fig, ax = plt.subplots()
64
+ ax.scatter(X, y, s=100,color="yellow", edgecolor="black")
65
+ orig = st.pyplot(fig)
66
+
67
+
68
+ if st.sidebar.button("Run Algorithm"):
69
+ vr,r2,mae = train_voting_regressor(algos)
70
+ y_2 = vr.predict(X_test)
71
+ ax.plot(X_test, y_2, linewidth=3,label="Voting Regressor")
72
+ ax.legend()
73
+ orig.pyplot(fig)
74
+ figs = []
75
+ r2_scores = []
76
+ maes = []
77
+ for i in algos:
78
+ i[1].fit(X_train,y_train)
79
+ y_pred = i[1].predict(X_test)
80
+ y_pred1 = i[1].predict(X_test1)
81
+ r2_scores.append(r2_score(y_test1,y_pred1))
82
+ maes.append(mean_absolute_error(y_test1,y_pred1))
83
+ ax.plot(X_test, y_pred, linewidth=1,label=i[0],linestyle='dashdot')
84
+ ax.legend()
85
+
86
+ counter = 0
87
+ for i in st.beta_columns(len(algos)):
88
+ with i:
89
+ orig.pyplot(fig)
90
+ counter += 1
91
+
92
+ st.sidebar.subheader("Regression Metrics")
93
+ st.sidebar.text("R2 score Voting Regressor " + str(round(r2,2)))
94
+ st.sidebar.text("MAE Voting Regressor " + str(round(mae,2)))
95
+
96
+ for i in range(len(algos)):
97
+ st.sidebar.text("*"*35)
98
+ st.sidebar.text("R2 score for " + algos[i][0] + " " + str(round(r2_scores[i],2)))
99
+ st.sidebar.text("MAE score for " + algos[i][0] + " " + str(round(maes[i], 2)))