File size: 6,746 Bytes
164bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a21e10b
164bb77
a21e10b
2717a3c
eadb45e
164bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94fff84
164bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94fff84
164bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2717a3c
164bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st

# EDA Pkgs
import pandas as pd
import numpy as np

# Viz Pkgs
import matplotlib
matplotlib.use('Agg')
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

regressor = LogisticRegression()

def main():
	""" Common ML Dataset Explorer """
	html_temp = """
	<div style="background-color:pink;"><p style="color:white;font-size:75px;padding:10px">Room Occupancy Predictor 💡</p></div>
	"""
	st.markdown(html_temp,unsafe_allow_html=True)


	def file_selector(folder_path='./dataset'):
		filenames = os.listdir(folder_path)
		selected_filename = st.selectbox("Select A file",filenames)
		return os.path.join(folder_path,selected_filename)

	filename = file_selector()
	st.info("You Selected {}".format(filename))

	# Read Data
	df = pd.read_csv(filename)
	# Show Dataset

	if st.checkbox("Show Dataset"):
		st.write(df.astype(str))

	# Show Columns
	if st.button("Column Names"):
		st.write(df.columns)

	# Show Shape
	if st.checkbox("Shape of Dataset"):
		data_dim = st.radio("Show Dimension By ",("Rows","Columns"))
		if data_dim == 'Rows':
			st.text("Number of Rows")
			st.write(df.shape[0])
		elif data_dim == 'Columns':
			st.text("Number of Columns")
			st.write(df.shape[1])
		else:
			st.write(df.shape)

	# Select Columns
	if st.checkbox("Select Columns To Show"):
		all_columns = df.columns.tolist()
		selected_columns = st.multiselect("Select",all_columns)
		new_df = df[selected_columns]
		st.dataframe(new_df)
		
	# Show Values
	if st.button("Value Counts"):
		st.text("Value Counts By Target/Class")
		st.write(df.iloc[:,-1].value_counts())


	# Show Datatypes
	if st.button("Data Types"):
		st.text(df.dtypes)



	# Show Summary
	if st.checkbox("Summary"):
		st.write(df.describe().T)

	## Plot and Visualization

	st.subheader("Data Visualization")
	# Correlation
	# Seaborn Plot
	if st.checkbox("Correlation Plot[Seaborn]"):
		st.set_option('deprecation.showPyplotGlobalUse', False)
		st.write(sns.heatmap(df.corr(),annot=True))
		st.pyplot()


	# Pie Chart
	if st.checkbox("Pie Plot"):
		all_columns_names = df.columns.tolist()
		if st.button("Generate Pie Plot"):
			st.success("Generating A Pie Plot")
			st.write(df.iloc[:,-1].value_counts().plot.pie(autopct="%1.1f%%"))
			st.pyplot()

	# Count Plot
	if st.checkbox("Plot of Value Counts"):
		st.text("Value Counts By Target")
		all_columns_names = df.columns.tolist()
		primary_col = st.selectbox("Primary Columm to GroupBy",all_columns_names)
		selected_columns_names = st.multiselect("Select Columns",all_columns_names)
		if st.button("Plot"):
			st.text("Generate Plot")
			if selected_columns_names:
				vc_plot = df.groupby(primary_col)[selected_columns_names].count()
			else:
				vc_plot = df.iloc[:,-1].value_counts()
			st.write(vc_plot.plot(kind="bar"))
			st.pyplot()


	# Customizable Plot

	st.subheader("Customizable Plot")
	all_columns_names = df.columns.tolist()
	type_of_plot = st.selectbox("Select Type of Plot",["area","bar","line","hist","box","kde"])
	selected_columns_names = st.multiselect("Select Columns To Plot",all_columns_names)

	if st.button("Generate Plot"):
		st.success("Generating Customizable Plot of {} for {}".format(type_of_plot,selected_columns_names))

		# Plot By Streamlit
		if type_of_plot == 'area':
			cust_data = df[selected_columns_names]
			st.area_chart(cust_data)

		elif type_of_plot == 'bar':
			cust_data = df[selected_columns_names]
			st.bar_chart(cust_data)

		elif type_of_plot == 'line':
			cust_data = df[selected_columns_names]
			st.line_chart(cust_data)

		# Custom Plot
		elif type_of_plot:
			cust_plot= df[selected_columns_names].plot(kind=type_of_plot)
			st.write(cust_plot)
			st.pyplot()

	#Evaluate Model
	st.subheader("Model, Deployment, and Evaluation")
	# Impute nans with mean for numeris and most frequent for categoricals
	cat_imp = SimpleImputer(strategy="most_frequent")
	if len(df.loc[:,df.dtypes == 'object'].columns) != 0:
		df.loc[:,df.dtypes == 'object'] = cat_imp.fit_transform(df.loc[:,df.dtypes == 'object'])
		imp = SimpleImputer(missing_values = np.nan, strategy="mean")
		df.loc[:,df.dtypes != 'object'] = imp.fit_transform(df.loc[:,df.dtypes != 'object'])

# One hot encoding for categorical variables

	features = st.multiselect('select features and target variable',df.columns.tolist())

	cats = df.dtypes == 'object'
	le = LabelEncoder()
	for x in df.columns[cats]:
		df.loc[:,x] = le.fit_transform(df[x])
		onehotencoder = OneHotEncoder()
		df.loc[:,~cats].join(pd.DataFrame(data=onehotencoder.
		fit_transform (df.loc[:,cats]).toarray(), columns=onehotencoder.get_feature_names_out()))

	chosen_target = st.sidebar.selectbox("Please choose target column", (df.columns))

	X = df.loc[:, df.columns != chosen_target]
	scaler = MinMaxScaler(feature_range=(0,1))

	scaler.fit(X)

	X = pd.DataFrame(scaler.transform(X))
	X.columns = df.loc[:, df.columns != chosen_target].columns
	y = df[chosen_target]
# Train test
	X_train, X_test, y_train, y_test =  train_test_split(X, y, test_size=0.2, random_state=42)

	type = st.sidebar.selectbox("Algorithm type", ("Classification", "Classification"))
	if type == "Classification":
		chosen_classifier = st.sidebar.selectbox("Please choose a classifier", ('Logistic Regression', 'Naive Bayes'))
		if chosen_classifier == 'Logistic Regression':
			max_iter = st.sidebar.slider('max iterations', 1, 100, 10)
	if type == "Classification":
		if chosen_classifier == 'Logistic Regression':
			alg = LogisticRegression()
			model = alg.fit(X_train, y_train)
			predictions = alg.predict(X_test)
			predictions_train = alg.predict(X_train)
		elif chosen_classifier=='Naive Bayes':
			alg = GaussianNB()
			model = alg.fit(X_train, y_train)
			predictions = alg.predict(X_test)
			predictions_train = alg.predict(X_train)


	error_metrics = {}
	if  type == 'Classification':
		error_metrics['Accuracy_test'] = accuracy_score(y_test, predictions)
		error_metrics['Accuracy_train'] = accuracy_score(y_train, predictions_train)
		st.write('### Accuracy Train: ' + str(round(error_metrics['Accuracy_train'], 3)) +
				' -- Accuracy Test: ' +  str(round(error_metrics['Accuracy_test'], 3)))




	if st.button("Thanks"):
		st.balloons()

	st.sidebar.header("About App")
	st.sidebar.info("A Simple ML App for predicting Room Occupancy")


	st.sidebar.header("Developer")
	st.sidebar.info("Nasim Obeid")
	st.sidebar.text("Built with Streamlit")


if __name__ == '__main__':
	main()