-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlapp.py
199 lines (129 loc) · 4.95 KB
/
mlapp.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
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')
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(df.isnull().sum())
if st.checkbox("Display the data types"):
st.write(df.dtypes)
if st.checkbox('Display Correlation of data variuos columns'):
st.write(df.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()
if st.checkbox('Display Pairplot'):
st.write(sns.pairplot(df1,diag_kind='kde'))
st.pyplot()
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(50))
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
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('Nmae 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 for our ML project, feel feel free to use it. This dataset is fetched from the UCI Machine learning repository. The analysis in here is to demonstrate how we can present our wok to our stakeholders in an interractive way by building a web app for our machine learning algorithms using different dataset.'
)
st.balloons()
# ..............
if __name__ == '__main__':
main()