-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
309 lines (254 loc) · 12.3 KB
/
Copy pathapp.py
File metadata and controls
309 lines (254 loc) · 12.3 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
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
import streamlit as st # For creating web apps
import pandas as pd # For data manipulation and analysis
import numpy as np # For numerical operations
import matplotlib.pyplot as plt # For plotting graphs ,plt.tight_layout()
import seaborn as sns # For statistical data visualization
from sklearn.preprocessing import LabelEncoder, StandardScaler # For data preprocessing
from sklearn.decomposition import PCA # For Principal Component Analysis
from sklearn.cluster import KMeans # For KMeans clustering
from io import BytesIO # For handling bytes-like objects
import base64 # For base64 encoding/decoding
# Custom CSS for Streamlit elements
custom_css = """
<style>
"container": {"padding": "5!important","background-color":'black'}
/* Sidebar styling */
.sidebar .sidebar-content {
background-color: #0B3C5D; /* Navy blue */
color: #FFFFFF; /* White */
font-size: 18px;
}
/* Main content styling */
.css-1l02zno, .css-14zkor0 {
color: #FFFFFF; /* White */
font-size: 18px;
}
/* Header and button styling */
.st-cq {
background-color: #17BEBB; /* Cyan */
color: #FFFFFF; /* White */
font-size: 24px;
font-weight: bold;
border-radius: 10px;
padding: 8px 12px;
margin-bottom: 20px;
}
/* Background pattern */
body {
background-color: #000000; /* Black */
}
/* Footer styling */
.stFooter {
font-size: 14px;
color: #FFFFFF; /* White */
text-align: center;
margin-top: 20px;
}
</style>
"""
# Define the main function
def main():
# Set the title and configuration of the Streamlit page
st.set_page_config(page_title="Imama Kainat with Data", layout="wide")
# Display custom CSS
st.markdown(custom_css, unsafe_allow_html=True)
# Sidebar with activities selection
activities = ["Home", "Exploratory Data Analysis", "Data Visualization", "Data Preprocessing", "Machine Learning"]
choice = st.sidebar.radio("Select Activity", activities)
# Main content based on the selected activity
if choice == "Home":
st.header("Welcome to Data with Mamai Nataki")
st.write("""
This application offers a variety of tools to help you explore, visualize, preprocess, and analyze your data.
You can perform Exploratory Data Analysis, visualize your data, preprocess it for machine learning, and even apply
clustering algorithms. Please select an activity from the sidebar to get started.
""")
elif choice == "Exploratory Data Analysis":
st.header("Exploratory Data Analysis ")
# Upload dataset
uploaded_file = st.file_uploader("Upload a dataset", type=["csv", "txt", "xlsx"])
if uploaded_file is not None:
try:
df = pd.read_csv(uploaded_file)
st.dataframe(df.head())
# Data preprocessing
st.subheader("Data Preprocessing")
st.write("Handling missing values:")
st.write(df.isnull().sum())
# Handling missing values
if st.checkbox("Handle Missing Values"):
df = handle_missing_values(df)
st.write("Missing values handled. New data:")
st.dataframe(df.head())
# Show dataset shape
if st.checkbox("Show Dataset Shape"):
st.write(df.shape)
# Show columns
if st.checkbox("Show Columns"):
all_columns = df.columns.tolist()
st.write(all_columns)
# Show summary statistics
if st.checkbox("Show Summary Statistics"):
st.write(df.describe())
# Show selected columns
if st.checkbox("Show Selected Columns"):
selected_columns = st.multiselect("Select Columns", all_columns)
if selected_columns:
selected_df = df[selected_columns]
st.dataframe(selected_df)
# Show value counts
if st.checkbox("Show Value Counts"):
column = st.selectbox("Select column", df.columns)
st.write(df[column].value_counts())
# Plot correlation matrix
if st.checkbox("Plot Correlation Matrix"):
try:
numeric_df = df.select_dtypes(include=np.number) # Select only numeric columns
fig, ax = plt.subplots()
ax = sns.heatmap(numeric_df.corr(), annot=True, cmap="coolwarm")
st.pyplot(fig)
except Exception as e:
st.error(f"An error occurred while plotting the correlation matrix: {e}")
except Exception as e:
st.error(f"An error occurred: {e}")
elif choice == "Data Visualization":
st.header("Data Visualization ")
# Upload dataset
uploaded_file = st.file_uploader("Upload a dataset", type=["csv", "txt", "xlsx"])
if uploaded_file is not None:
try:
# Read the uploaded file
df = pd.read_csv(uploaded_file)
st.dataframe(df.head())
# Show value counts as bar plot
if st.checkbox("Show Value Counts (Bar Plot)"):
column = st.selectbox("Select column for value counts", df.columns)
st.bar_chart(df[column].value_counts())
# Customizable plot
st.subheader("Customizable Plot")
plot_type = st.selectbox("Select plot type", ["area", "bar", "line", "scatter"])
selected_columns = st.multiselect("Select columns to plot", df.columns)
if st.button("Generate Plot"):
if plot_type and selected_columns:
if len(selected_columns) < 2:
st.error("Please select at least two columns for plotting.")
else:
x_col = selected_columns[0]
y_col = selected_columns[1]
fig, ax = plt.subplots()
if plot_type == "area":
ax.fill_between(df[x_col], df[y_col])
elif plot_type == "bar":
ax.bar(df[x_col], df[y_col])
elif plot_type == "line":
ax.plot(df[x_col], df[y_col])
elif plot_type == "scatter":
ax.scatter(df[x_col], df[y_col])
st.pyplot(fig)
except Exception as e:
st.error(f"An error occurred: {e}")
elif choice == "Data Preprocessing":
st.header("Data Preprocessing ")
# Upload dataset
uploaded_file = st.file_uploader("Upload a dataset for preprocessing", type=["csv", "txt"])
if uploaded_file is not None:
try:
df = pd.read_csv(uploaded_file)
st.dataframe(df.head())
# Data preprocessing
st.subheader("Data Preprocessing")
st.write("Handling missing values:")
st.write(df.isnull().sum())
# Handling missing values
if st.checkbox("Handle Missing Values"):
df = handle_missing_values(df)
st.write("Missing values handled. New data:")
st.dataframe(df.head())
# Encoding categorical data if present
categorical_cols = df.select_dtypes(include=['object']).columns.tolist()
if categorical_cols:
st.write("Encoding categorical data:")
df_encoded = encode_categorical_data(df, categorical_cols)
st.write("Encoded data:")
st.dataframe(df_encoded.head())
# Scaling data if numerical columns exist
numerical_cols = df.select_dtypes(include=['float64', 'int64']).columns.tolist()
if numerical_cols:
st.write("Scaling numerical data:")
df_scaled = scale_numerical_data(df[numerical_cols])
st.write("Scaled data:")
st.dataframe(df_scaled.head())
# Download link for preprocessed data
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # B64 encoding
href = f'<a href="data:file/csv;base64,{b64}" download="preprocessed_data.csv">Download Preprocessed Data</a>'
st.markdown(href, unsafe_allow_html=True)
except Exception as e:
st.error(f"An error occurred: {e}")
elif choice == "Machine Learning":
st.header("Machine Learning ")
# Upload dataset
uploaded_file = st.file_uploader("Upload a dataset for clustering", type=["csv", "txt"])
if uploaded_file is not None:
try:
df = pd.read_csv(uploaded_file)
st.dataframe(df.head())
# Preprocessing for clustering (label encoding and PCA)
le = LabelEncoder()
df_encoded = df.apply(le.fit_transform)
pca = PCA(n_components=2)
principal_components = pca.fit_transform(df_encoded)
df_pca = pd.DataFrame(data=principal_components, columns=["Principal Component 1", "Principal Component 2"])
# KMeans clustering
k = st.slider("Select number of clusters", min_value=2, max_value=10)
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(df_encoded)
df["Cluster"] = kmeans.labels_
# Visualize clusters
st.subheader("Cluster Visualization")
fig, ax = plt.subplots()
scatter = ax.scatter(df_pca["Principal Component 1"], df_pca["Principal Component 2"], c=df["Cluster"], cmap="viridis")
legend = ax.legend(*scatter.legend_elements(), loc="upper right", title="Clusters")
ax.add_artist(legend)
st.pyplot(fig)
# Show cluster details
st.subheader("Cluster Details")
# Select numeric columns for aggregation
numeric_df = df.select_dtypes(include=np.number)
st.write(numeric_df.groupby("Cluster").mean())
# Download link for clustering results
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # B64 encoding
href = f'<a href="data:file/csv;base64,{b64}" download="clustering_results.csv">Download Clustering Results</a>'
st.markdown(href, unsafe_allow_html=True)
except Exception as e:
st.error(f"An error occurred: {e}")
# Footer
st.markdown('<p class="stFooter">Developed by Imama Kainat</p>', unsafe_allow_html=True)
def handle_missing_values(df):
"""Handle missing values in the DataFrame."""
# Replace missing numerical values with mean
numerical_cols = df.select_dtypes(include=['float64', 'int64']).columns.tolist()
for col in numerical_cols:
df[col].fillna(df[col].mean(), inplace=True)
# Replace missing categorical values with mode
categorical_cols = df.select_dtypes(include=['object']).columns.tolist()
for col in categorical_cols:
df[col].fillna(df[col].mode()[0], inplace=True)
return df
def encode_categorical_data(df, categorical_cols):
"""Encode categorical data using LabelEncoder."""
le = LabelEncoder()
for col in categorical_cols:
df[col] = le.fit_transform(df[col])
return df
def scale_numerical_data(df):
"""Scale numerical data using StandardScaler."""
scaler = StandardScaler()
df_scaled = scaler.fit_transform(df)
df_scaled = pd.DataFrame(df_scaled, columns=df.columns)
return df_scaled
# Run the main function
if __name__ == "__main__":
main()
# streamlit run p.py