-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_app.py
215 lines (143 loc) · 5.86 KB
/
main_app.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
import streamlit as st
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
#from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn import model_selection
#from sklearn.preprocessing import LabelEncoder
matplotlib.use('Agg')
from PIL import Image
st.title('Machine Learning Using Streamlit')
image = Image.open("Techy.jfif")
#st.image(image,use_column_width=True)
# Get the new width and height
new_width = st.sidebar.slider("Adjust Image Width", min_value=1, max_value=image.width, value=image.width)
new_height = st.sidebar.slider("Adjust Image Height", min_value=1, max_value=image.height, value=image.height)
# Reshape the image to the new dimensions
resized_image = image.resize((new_width, new_height))
# Display the resized image
st.image(resized_image, caption=f"Resized Image ({new_width}x{new_height})", use_column_width=True)
def main():
activities=['EDA','Visualisation','model','About us']
option=st.sidebar.selectbox('Selection option:',activities)
#DEALING WITH THE EDA PART
if option=='EDA':
st.subheader("Exploratory Data Analysis")
data=st.file_uploader("Upload dataset:",type=['csv','xlsx','txt','json'])
st.success("Data successfully loaded")
if data is not None:
df=pd.read_csv(data)
st.dataframe(df.head(50))
if st.checkbox("Display shape"):
st.write(df.shape)
if st.checkbox("Display columns"):
st.write(df.columns)
if st.checkbox("Select multiple columns"):
selected_columns=st.multiselect('Select preferred columns:',df.columns)
df1=df[selected_columns]
st.dataframe(df1)
if st.checkbox("Display summary"):
st.write(df1.describe().T)
if st.checkbox('Display Null Values'):
st.write(df1.isnull().sum())
if st.checkbox("Display the data types"):
st.write(df1.dtypes)
if st.checkbox('Display Correlation of data variuos columns'):
st.write(df1.corr())
#DEALING WITH THE VISUALISATION PART
elif option=='Visualisation':
st.subheader("Data Visualisation")
data=st.file_uploader("Upload dataset:",type=['csv','xlsx','txt','json'])
st.success("Data successfully loaded")
if data is not None:
df=pd.read_csv(data)
st.dataframe(df.head(50))
if st.checkbox('Select Multiple columns to plot'):
selected_columns=st.multiselect('Select your preferred columns',df.columns)
df1=df[selected_columns]
st.dataframe(df1)
if st.checkbox('Display Heatmap'):
st.write(sns.heatmap(df1.corr(),vmax=1,square=True,annot=True,cmap='viridis'))
st.pyplot()
st.set_option('deprecation.showPyplotGlobalUse', False)
if st.checkbox('Display Pairplot'):
st.write(sns.pairplot(df1,diag_kind='kde'))
st.pyplot()
st.set_option('deprecation.showPyplotGlobalUse', False)
if st.checkbox('Display Pie Chart'):
all_columns=df.columns.to_list()
pie_columns=st.selectbox("select column to display",all_columns)
pieChart=df[pie_columns].value_counts().plot.pie(autopct="%1.1f%%")
st.write(pieChart)
st.pyplot()
# DEALING WITH THE MODEL BUILDING PART
elif option=='model':
st.subheader("Model Building")
data=st.file_uploader("Upload dataset:",type=['csv','xlsx','txt','json'])
st.success("Data successfully loaded")
if data is not None:
df=pd.read_csv(data)
st.dataframe(df.head(5))
if st.checkbox('Select Multiple columns'):
new_data=st.multiselect("Select your preferred columns. NB: Let your target variable be the last column to be selected",df.columns)
df1=df[new_data]
st.dataframe(df1)
#Dividing my data into X and y variables
X=df1.iloc[:,0:-1]
y=df1.iloc[:,-1]
seed=st.sidebar.slider('Seed',1,200)
classifier_name=st.sidebar.selectbox('Select your preferred classifier:',('KNN','SVM','LR','naive_bayes','decision tree'))
def add_parameter(name_of_clf):
params=dict()
if name_of_clf=='SVM':
C=st.sidebar.slider('C',0.01, 15.0)
params['C']=C
else:
name_of_clf=='KNN'
K=st.sidebar.slider('K',1,15)
params['K']=K
return params
#calling the function
params=add_parameter(classifier_name)
#defing a function for our classifier
def get_classifier(name_of_clf,params):
clf= None
if name_of_clf=='SVM':
clf=SVC(C=params['C'])
elif name_of_clf=='KNN':
clf=KNeighborsClassifier(n_neighbors=params['K'])
elif name_of_clf=='LR':
clf=LogisticRegression()
elif name_of_clf=='naive_bayes':
clf=GaussianNB()
elif name_of_clf=='decision tree':
clf=DecisionTreeClassifier()
else:
st.warning('Select your choice of algorithm')
return clf
#calling the function
clf=get_classifier(classifier_name,params)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2, random_state=seed)
clf.fit(X_train,y_train)
y_pred=clf.predict(X_test)
st.write('Predictions:',y_pred)
accuracy=accuracy_score(y_test,y_pred)
st.write('Name of classifier:',classifier_name)
st.write('Accuracy',accuracy)
#DELING WITH THE ABOUT US PAGE
elif option=='About us':
st.markdown('This is an interactive web page ML project. Datasets are fetched from the UCI Machine learning repository. The analysis in here is to demonstrate how we can present work to stakeholders in an interractive way by building a web app for machine learning algorithms using different dataset.'
)
st.balloons()
# ..............
if __name__ == '__main__':
main()