-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplore.py
567 lines (482 loc) · 20.9 KB
/
explore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
from scipy.sparse.construct import random
import streamlit as st
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.cluster import KMeans
from sklearn.cluster import DBSCAN
from sklearn.cluster import Birch
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.svm import SVR
st.set_page_config(page_title="AI Algorithms", page_icon=":shark:")
#Functions to cache datasets
@st.cache
def load_breast_cancer_dataset():
return datasets.load_breast_cancer()
@st.cache
def load_iris_dataset():
return datasets.load_iris()
@st.cache
def load_digits_dataset():
return datasets.load_digits()
@st.cache
def load_wine_dataset():
return datasets.load_wine()
@st.cache
def load_carlifonia_dataset():
return datasets.fetch_california_housing()
@st.cache
def load_diabetes_dataset():
return datasets.load_diabetes()
CANCER = load_breast_cancer_dataset()
IRIS = load_iris_dataset()
DIGITS = load_digits_dataset()
WINE = load_wine_dataset()
CALIFORNIA = load_carlifonia_dataset()
DIABETES = load_diabetes_dataset()
data = None
#Removes the Right Hand Side Navigation Bar during deploy
# CLASSIFICATION PAGE INPUT FUNCTIONS
def kNN_Classifier_inputs():
col1, col2 = st.columns(2)
n_neighbors = col1.number_input('Number of K-Nearest Neighbours', 1, 10)
metric = col2.selectbox(
'Metric',
('euclidean', 'manhattan', 'chebyshev', 'minkowski', 'seuclidian', 'mahalanobis'))
return n_neighbors, metric
#2. MLP
def MLP_Classifier_inputs():
col1, col2 = st.columns(2)
col3, col4 = st.columns(2)
hidden_layer_sizes = col1.number_input('Size of Hidden Layer', 1, 10)
max_iter = col2.number_input('Maximum Iteration', 1, 10)
random_state = col3.number_input('Random State', 0, 10, 1)
activation = col4.selectbox(
'Activation Function',('relu', 'identity', 'logistic', 'tanh'))
solver = st.selectbox(
'Solver Function',['adam','lbfgs','sgd'])
return hidden_layer_sizes, max_iter, activation, solver, random_state
#3. Decision Tree Classifier
def Decision_Tree_Classifier_inputs():
st.write("This is a Decision Tree")
# 4. Support Vector Classifier
def Support_Vector_Classifier_inputs():
kernel = st.selectbox('Choose the Support Vector Classifier Function', ('linear', 'poly', 'rbf', 'sigmoid'))
return kernel
#CLASSIFICATION ALGORITHM FUNCTIONS
def kNN_Classifier(n_neighbors, metric='euclidean',data = data):
# metric : euclidean, manhattan, chebyshev, minkowski, wminkowski, seuclidean, mahalanobis
# Store the feature and target data
X = data.data
y = data.target
# Split the data using Scikit-Learn's train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
clf = KNeighborsClassifier(n_neighbors=n_neighbors, metric = metric)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
return score
def MLP_Classifier(hidden_layer_sizes, max_iter, activation = "relu", solver = "adam",random_state=1, data = data):
# activation{‘identity’, ‘logistic’, ‘tanh’, ‘relu’}, default=’relu’
# solver{‘lbfgs’, ‘sgd’, ‘adam’}, default=’adam’
# Store the feature and target data
X = data.data
y = data.target
# Split the data using Scikit-Learn's train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
clf = MLPClassifier(hidden_layer_sizes=hidden_layer_sizes, max_iter = max_iter,activation = activation,solver=solver,random_state=random_state)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
return score
def Decision_Tree_Classifier(data = data):
# Store the feature and target data
X = data.data
y = data.target
# Split the data using Scikit-Learn's train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
return score
def Support_Vector_Classifier(data = data, kernel = 'linear'):
#kernel{‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’}
# Store the feature and target data
X = data.data
y = data.target
# Split the data using Scikit-Learn's train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
clf = SVC(kernel=kernel)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
return score
def classification_page(dataset_choice, data):
st.title("Classification Algorithms")
st.header(f"The {dataset_choice} dataset")
st.write('---')
class_alg_choice = st.selectbox("Select a classification algorithm: ", ['KNN', 'MLP',
'Decision Tree Classifier',
'Support Vector Classifier'])
if class_alg_choice == 'KNN':
n_neighbors, metric = kNN_Classifier_inputs()
score = kNN_Classifier(n_neighbors, metric, data)
elif class_alg_choice == 'MLP':
hidden_layer_sizes, max_iter, activation, solver, random_state = MLP_Classifier_inputs()
score = MLP_Classifier(hidden_layer_sizes, max_iter, activation, solver, random_state, data)
elif class_alg_choice == 'Decision Tree Classifier':
# Decision_Tree_Classifier_inputs() -- No inputs
score = Decision_Tree_Classifier(data)
elif class_alg_choice == 'Support Vector Classifier':
kernel = Support_Vector_Classifier_inputs()
score = Support_Vector_Classifier(data, kernel)
st.write('---')
st.write("Accuracy : ", score)
st.write('---')
st.write("A sample of 10 rows from the dataset")
data_frame = np.c_[data.data, data.target]
columns = np.append(data.feature_names, ["target"])
data_frame = pd.DataFrame(data_frame, columns=columns)
st.write(data_frame.head(10))
st.write('---')
st.write("Visualization of the first 20 dataset entries")
st.bar_chart(data_frame[:20])
if dataset_choice == 'Cancer':
st.subheader('Correlation matrix of the target with some features')
corr_matrix = data_frame[
['mean radius', 'radius error', 'worst radius',
'mean perimeter', 'perimeter error', 'worst perimeter',
'mean smoothness', 'smoothness error', 'worst smoothness', 'target'
]].corr()
elif dataset_choice == 'Digits':
st.subheader('Correlation matrix of the target with some features')
corr_matrix = data_frame[
['pixel_0_0', 'pixel_0_2', 'pixel_1_4', 'pixel_2_6',
'pixel_3_2', 'pixel_4_4', 'pixel_5_6',
'pixel_6_2', 'pixel_7_4', 'pixel_7_6', 'target'
]].corr()
else:
st.subheader('Correlation matrix')
corr_matrix = data_frame.corr()
corr_fig = plt.figure(figsize=(14,7))
sns.heatmap(corr_matrix, cmap=plt.cm.CMRmap_r, annot=True)
st.pyplot(corr_fig)
# Descriptive statistics
st.write('---')
st.subheader('Descriptive statistics')
st.write(data_frame.describe())
# REGRESSION PAGE INPUTS
#1. Linear Regression
def Linear_Regression_inputs():
st.write("This is the linear regression model")
#2. Ridge Regression
def Ridge_Regression_inputs():
alpha = st.slider('Enter the Alpha', 0.1, 1.0, 0.1)
#Might add an onchanged event to scroll to accuracy
return alpha
# 3. Support Vector Regression
def Support_Vector_Regression_inputs():
kernel = st.selectbox('Support Vector Regression Function', ['linear', 'poly', 'rbf', 'sigmoid']) #Removed precomputed
return kernel
# REGRESSION ALGORITHM FUNCTIONS
def Linear_Regression(data = data):
# Store the feature and target data
X = data.data
y = data.target
# Split the data using Scikit-Learn's train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
reg = LinearRegression()
reg.fit(X_train, y_train)
score = reg.score(X_test, y_test)
return(score)
def Ridge_Regression(data = data, alpha = 0.1):
# Store the feature and target data
X = data.data
y = data.target
# Split the data using Scikit-Learn's train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
reg = Ridge(alpha = alpha)
reg.fit(X_train, y_train)
score = reg.score(X_test, y_test)
return(score)
def Support_Vector_Regression(data = data, kernel = 'linear'):
#kernel{‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’}
# Store the feature and target data
X = data.data
y = data.target
# Split the data using Scikit-Learn's train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
reg = SVR(kernel=kernel)
reg.fit(X_train, y_train)
score = reg.score(X_test, y_test)
return(score)
# REGRESSION INPUTS + ALGORITHMS
def regression_page(dataset_choice, data):
st.title("Regression Algorithms")
st.header(f"The {dataset_choice} dataset")
st.write('---')
reg_alg_choice = st.selectbox("Select a Regression Algorithm: ", ['Linear Regression', 'Ridge Regression',
'Support Vector Regression'])
if reg_alg_choice == 'Linear Regression':
#Linear_Regression_inputs() --> Can be left out because there are no inputs
score = Linear_Regression(data)
elif reg_alg_choice == 'Ridge Regression':
alpha = Ridge_Regression_inputs()
score = Ridge_Regression(data, alpha)
elif reg_alg_choice == 'Support Vector Regression':
kernel = Support_Vector_Regression_inputs()
score = Support_Vector_Regression(data, kernel)
st.write('---')
st.write("Accuracy : ", score) #For training
st.write('---')
st.write("A sample of 10 rows from the dataset")
# st.write(data.data)
data_frame = np.c_[data.data, data.target]
columns = np.append(data.feature_names, ["target"])
data_frame = pd.DataFrame(data_frame, columns=columns)
st.write(data_frame.head(10))
st.write("---")
st.write("Visualization of the first 20 dataset entries")
st.bar_chart(data_frame[:20])
#Plot the correlation matrix
st.write('---')
st.subheader('Correlation matrix')
corr_matrix = data_frame.corr()
corr_fig = plt.figure(figsize=(14,7))
sns.heatmap(corr_matrix, cmap=plt.cm.CMRmap_r, annot=True)
st.pyplot(corr_fig)
# Descriptive statistics
st.write('---')
st.subheader('Descriptive statistics')
st.write(data_frame.describe())
#CALIFORNIA REGRESSION
def california_regression_page(dataset_choice, data):
st.title("Regression Algorithms")
st.header(f"The {dataset_choice} dataset")
st.write('---')
reg_alg_choice = st.selectbox("Select a Regression Algorithm: ", ['Linear Regression', 'Ridge Regression'
])
if reg_alg_choice == 'Linear Regression':
#Linear_Regression_inputs() --> Can be left out because there are no inputs
score = Linear_Regression(data)
elif reg_alg_choice == 'Ridge Regression':
alpha = Ridge_Regression_inputs()
score = Ridge_Regression(data, alpha)
st.write('---')
st.write("Accuracy : ", score) #For training
st.write('---')
st.write("A sample of 10 rows from the dataset")
# st.write(data.data)
data_frame = np.c_[data.data, data.target]
columns = np.append(data.feature_names, ["target"])
data_frame = pd.DataFrame(data_frame, columns=columns)
st.write(data_frame.head(10))
st.write("---")
st.write("Visualization of the first 20 dataset entries")
st.bar_chart(data_frame[:20])
#Plot the correlation matrix
st.write('---')
st.subheader('Correlation matrix')
corr_matrix = data_frame.corr()
corr_fig = plt.figure(figsize=(14,7))
sns.heatmap(corr_matrix, cmap=plt.cm.CMRmap_r, annot=True)
st.pyplot(corr_fig)
# Descriptive statistics
st.write('---')
st.subheader('Descriptive statistics')
st.write(data_frame.describe())
#CLUSTERING PAGE INPUTS
#1. K-Means Clustering
def kMeans_Clustering_inputs():
col1, col2 = st.columns(2)
n_clusters = col1.number_input('Number of clusters', 1, 10)
n_init = col2.number_input('Initial Value', 1, 20, 10)
random_state = st.number_input('Random State', 0, 10, 1, key="random_state",)
return n_clusters, n_init, random_state
# 2. DBSCAN Clustering
def DBSCAN_Clustering_inputs():
col1, col2 = st.columns(2)
eps = st.slider('Eps value', 0.1, 100.0)
min_samples = col1.number_input('Minimum Number Of Samples', 1, 10, 5)
metric = col2.selectbox(
'Metric',
#('euclidean', 'manhattan', 'chebyshev', 'minkowski', 'seucledian', 'mahalanobis'), key="DBSCAN metric")
('euclidean', 'manhattan', 'chebyshev', 'minkowski'), key="DBSCAN metric")
return eps, min_samples, metric
#3 Birch Clustering
def Birch_Clustering_inputs():
col1, col2 = st.columns(2)
n_clusters = st.number_input('Number of Clusters', 1, 10, 5)
threshold = col1.slider('Threshhold Value', 0.1, 1.0, 0.5)
branching_factor = col2.slider('Branching factor', 1, 100, 50)
return n_clusters, threshold, branching_factor
# CLUSTERING ALGORITHMS
def kMeans_Clustering(n_clusters = 3,random_state = 1,n_init=10, data = data):
X = data.data
clustering = KMeans(n_clusters=n_clusters,random_state=random_state,n_init=n_init)
label = clustering.fit_predict(X)
u_labels = np.unique(label)
pca = PCA(2)
df = pca.fit_transform(X)
#plotting the results:
plots = []
fig = plt.figure()
for i in u_labels:
colors = ['r','b']
ax = fig.add_subplot(111)
ax.scatter(df[label == i , 0] , df[label == i , 1] , label = i)
plt.legend()
st.pyplot(fig)
def DBSCAN_Clustering(eps=0.5, min_samples=5, metric='euclidean', data = data):
X = data.data
clustering = DBSCAN(eps=eps, min_samples=min_samples, metric=metric)
clustering.fit(X)
label = clustering.fit_predict(X)
u_labels = np.unique(label)
pca = PCA(2)
df = pca.fit_transform(X)
#plotting the results:
plots = []
fig = plt.figure()
for i in u_labels:
colors = ['r','b']
ax = fig.add_subplot(111)
ax.scatter(df[label == i , 0] , df[label == i , 1] , label = i)
plt.legend()
st.pyplot(fig)
def Birch_Clustering( n_clusters, threshold=0.5, branching_factor=50, data = data):
X = data.data
clustering = Birch(threshold=threshold, branching_factor=branching_factor, n_clusters=n_clusters)
clustering.fit(X)
label = clustering.fit_predict(X)
u_labels = np.unique(label)
pca = PCA(2)
df = pca.fit_transform(X)
#plotting the results:
plots = []
fig = plt.figure()
for i in u_labels:
colors = ['r','b']
ax = fig.add_subplot(111)
ax.scatter(df[label == i , 0] , df[label == i , 1] , label = i)
plt.legend()
st.pyplot(fig)
def clustering_page(dataset_choice, data):
st.title("Clustering Algorithms")
st.header(f"The {dataset_choice} dataset")
st.write('---')
clust_alg_choice = st.selectbox("Clustering algorithm: ", ['k-Means', 'DBSCAN',
'Birch'])
if clust_alg_choice == 'k-Means':
n_clusters, n_init, random_state = kMeans_Clustering_inputs()
kMeans_Clustering(n_clusters, random_state, n_init, data)
elif clust_alg_choice == 'DBSCAN':
eps, min_samples, metric = DBSCAN_Clustering_inputs()
DBSCAN_Clustering(eps, min_samples, metric, data)
elif clust_alg_choice == 'Birch':
n_clusters, threshold, branching_factor = Birch_Clustering_inputs()
Birch_Clustering(n_clusters, threshold, branching_factor, data)
st.write('---')
st.write("A sample of 10 rows from the dataset")
data_frame = np.c_[data.data, data.target]
columns = np.append(data.feature_names, ["target"])
data_frame = pd.DataFrame(data_frame, columns=columns)
st.write(data_frame.head(10))
st.write('---')
st.write("Visualization of the first 20 dataset entries")
st.bar_chart(data_frame[:20])
st.write('---')
if dataset_choice == 'Cancer':
st.subheader('Correlation matrix of the target with some features')
corr_matrix = data_frame[
['mean radius', 'radius error', 'worst radius',
'mean perimeter', 'perimeter error', 'worst perimeter',
'mean smoothness', 'smoothness error', 'worst smoothness', 'target'
]].corr()
elif dataset_choice == 'Digits':
st.subheader('Correlation matrix of the target with some features')
corr_matrix = data_frame[
['pixel_0_0', 'pixel_0_2', 'pixel_1_4', 'pixel_2_6',
'pixel_3_2', 'pixel_4_4', 'pixel_5_6',
'pixel_6_2', 'pixel_7_4', 'pixel_7_6', 'target'
]].corr()
else:
st.subheader('Correlation matrix')
corr_matrix = data_frame.corr()
corr_fig = plt.figure(figsize=(14,7))
sns.heatmap(corr_matrix, cmap=plt.cm.CMRmap_r, annot=True)
st.pyplot(corr_fig)
# Descriptive statistics
st.write('---')
st.subheader('Descriptive statistics')
st.write(data_frame.describe())
# Main Pages
def home_page():
# st.title("Welcome to AI :bulb:")
# col1, col2, col3 = st.columns([1,6,1])
# with col1:
# st.write("")
# with col2:
# st.image("https://media4.giphy.com/media/7VzgMsB6FLCilwS30v/200w.webp?cid=ecf05e470qdohkjl35f3tia0r1sz5xs67chf2zfxmq0sm4an&rid=200w.webp&ct=g")
# with col3:
# st.write("")
col1, col2,col3 = st.columns([1,3.5,1])
col2.title("Welcome to AI :bulb:")
cols1, cols2,cols3 = st.columns([1,2,1])
cols1.write("")
cols2.image("https://media4.giphy.com/media/7VzgMsB6FLCilwS30v/200w.webp?cid=ecf05e470qdohkjl35f3tia0r1sz5xs67chf2zfxmq0sm4an&rid=200w.webp&ct=g")
st.header("The AI Algorithm Exploration Tool:fire:")
st.write("""
This tool has been designed as an exploratory space for Computer Science students\
to have some intuition on algorithms\
<span style='color: yellowgreen;'>**without having to write code!**</span>
<br>
""", unsafe_allow_html=True)
st.write("The tool also allows the students to try out different datasets such as `California`, `Iris` and `Wine`")
st.write("<b> Start Exploring Now! </b>", unsafe_allow_html=True)
def models_page():
with st.sidebar.expander("Algorithmic Problems"):
alg_prob_choice = st.selectbox("Select the algorithmic problem: ", ['Classification', 'Regression', 'Clustering'],)
if alg_prob_choice == 'Classification':
dataset_choice = st.sidebar.selectbox("Select a Dataset for Classification", ['Cancer', 'Iris', 'Digits','Wine'])
if dataset_choice == 'Cancer':
data = CANCER
elif dataset_choice == 'Iris':
data = IRIS
elif dataset_choice == 'Digits':
data = DIGITS
elif dataset_choice == 'Wine':
data = WINE
classification_page(dataset_choice, data)
elif alg_prob_choice == 'Regression':
dataset_choice = st.sidebar.selectbox("Select a Dataset for Regression", ['California', 'Diabetes'])
if dataset_choice == 'California':
data = CALIFORNIA
california_regression_page(dataset_choice, data)
elif dataset_choice == 'Diabetes':
data = DIABETES
regression_page(dataset_choice, data)
else:
dataset_choice = st.sidebar.selectbox("Select a Dataset for Clustering", ['Cancer', 'Iris', 'Digits','Wine'])
if dataset_choice == 'Cancer':
data = CANCER
elif dataset_choice == 'Iris':
data = IRIS
elif dataset_choice == 'Digits':
data = DIGITS
elif dataset_choice == 'Wine':
data = WINE
clustering_page(dataset_choice, data)
st.sidebar.title("EXPLORE")
page_choice = st.sidebar.radio(label="",options=["Home", "Algorithm Tool"])
if page_choice == "Home":
home_page()
else:
models_page()