Repository navigation
Expand file tree
/
Copy pathpython_eda_app.py
More file actions
125 lines (99 loc) · 6.65 KB
/
Copy pathpython_eda_app.py
File metadata and controls
125 lines (99 loc) · 6.65 KB
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
import streamlit as st
import pandas as pd
import numpy as np
from collections import Counter
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
import missingno
def create_correlation_chart(corr_df): ## Create Correlation Chart using Matplotlib
fig = plt.figure(figsize=(15,15))
plt.imshow(corr_df.values, cmap="Blues")
plt.xticks(range(corr_df.shape[0]), corr_df.columns, rotation=90, fontsize=15)
plt.yticks(range(corr_df.shape[0]), corr_df.columns, fontsize=15)
plt.colorbar()
for i in range(corr_df.shape[0]):
for j in range(corr_df.shape[0]):
plt.text(i,j, "{:.2f}".format(corr_df.values[i, j]), color="red", ha="center", fontsize=14, fontweight="bold")
return fig
def create_missing_values_bar(df):
missing_fig = plt.figure(figsize=(10,5))
ax = missing_fig.add_subplot(111)
missingno.bar(df, figsize=(10,5), fontsize=12, ax=ax)
return missing_fig
def find_cat_cont_columns(df): ## Logic to Separate Continuous & Categorical Columns
cont_columns, cat_columns = [],[]
for col in df.columns:
if len(df[col].unique()) <= 25 or df[col].dtype == np.object_: ## If less than 25 unique values
cat_columns.append(col.strip())
else:
cont_columns.append(col.strip())
return cont_columns, cat_columns
### Web App / Dashboard Code
st.set_page_config(page_icon=":bar_chart:", page_title="EDA Automated using Python")
st.title("Harnessing the Power of Python to Automate EDA :bar_chart: :tea: :coffee:")
st.caption("Upload CSV file to see various Charts related to EDA. Please upload file that has both continuous columns and categorical columns. Once you upload file, various charts, widgets and basic stats will be displayed. As a sample example, you can upload famous <a href='https://www.kaggle.com/competitions/titanic/data?select=train.csv'>Titanic Dataset</a> available from Kaggle.", unsafe_allow_html=True)
st.markdown("Please feel free to visit <a href='https://www.youtube.com/watch?v=jS3uhxD-5Cs'>this video link</a> if you want to learn how to create this Web App.", unsafe_allow_html=True)
upload = st.file_uploader(label="Upload File Here:", type=["csv"])
if upload: ## File as Bytes
df = pd.read_csv(upload)
tab1, tab2, tab3 = st.tabs(["Dataset Overview :clipboard:", "Individual Column Stats :bar_chart:", "Explore Relation Between Features :chart:"])
with tab1: ## Dataset Overview Tab
st.subheader("1. Dataset")
st.write(df)
cont_columns, cat_columns = find_cat_cont_columns(df)
st.subheader("2. Dataset Overview")
st.markdown("<span style='font-weight:bold;'>{}</span> : {}".format("Rows", df.shape[0]), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> : {}".format("Duplicates", df.shape[0] - df.drop_duplicates().shape[0]), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> : {}".format("Features", df.shape[1]), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> : {}".format("Categorical Columns", len(cat_columns)), unsafe_allow_html=True)
st.write(cat_columns)
st.markdown("<span style='font-weight:bold;'>{}</span> : {}".format("Continuous Columns", len(cont_columns)), unsafe_allow_html=True)
st.write(cont_columns)
corr_df = df[cont_columns].corr()
corr_fig = create_correlation_chart(corr_df)
st.subheader("3. Correlation Chart")
st.pyplot(corr_fig, use_container_width=True)
st.subheader("4. Missing Values Distribution")
missing_fig = create_missing_values_bar(df)
st.pyplot(missing_fig, use_container_width=True)
with tab2: ## Individual Column Stats
df_descr = df.describe()
st.subheader("Analyze Individual Feature Distribution")
st.markdown("#### 1. Understand Continuous Feature")
feature = st.selectbox(label="Select Continuous Feature", options=cont_columns, index=0)
na_cnt = df[feature].isna().sum()
st.markdown("<span style='font-weight:bold;'>{}</span> : {}".format("Count", df_descr[feature]['count']), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> : {} / ({:.2f} %)".format("Missing Count", na_cnt, na_cnt/df.shape[0]), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> : {:.2f}".format("Mean", df_descr[feature]['mean']), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> : {:.2f}".format("Standard Deviation", df_descr[feature]['std']), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> : {}".format("Minimum", df_descr[feature]['min']), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> : {}".format("Maximum", df_descr[feature]['max']), unsafe_allow_html=True)
st.markdown("<span style='font-weight:bold;'>{}</span> :".format("Quantiles"), unsafe_allow_html=True)
st.write(df_descr[[feature]].T[['25%', "50%", "75%"]])
## Histogram using Plotly
hist_fig = px.histogram(df, x=feature, nbins=50, title=f"Distribution of {feature}")
st.plotly_chart(hist_fig, use_container_width=True)
st.markdown("#### 2. Understand Categorical Feature")
feature = st.selectbox(label="Select Categorical Feature", options=cat_columns, index=0)
### Categorical Columns Distribution
cnts = Counter(df[feature].values)
df_cnts = pd.DataFrame({"Type": cnts.keys(), "Values": cnts.values()})
bar_fig = px.bar(df_cnts, x="Type", y="Values", title=f"Distribution of {feature}", color="Values")
st.plotly_chart(bar_fig, use_container_width=True)
with tab3: ## Explore Relation Between Features
st.subheader("Explore Relationship Between Features of Dataset")
col1, col2 = st.columns(2)
with col1:
x_axis = st.selectbox(label="X-Axis", options=cont_columns, index=0)
with col2:
y_axis = st.selectbox(label="Y-Axis", options=cont_columns, index=1)
color_encode = st.selectbox(label="Color-Encode", options=[None,] + cat_columns)
if color_encode:
scatter_fig = px.scatter(df, x=x_axis, y=y_axis, color=color_encode,
title=f"{x_axis.capitalize()} vs {y_axis.capitalize()}")
else:
scatter_fig = px.scatter(df, x=x_axis, y=y_axis,
title=f"{x_axis.capitalize()} vs {y_axis.capitalize()}")
st.plotly_chart(scatter_fig, use_container_width=True)