saherPervaiz commited on
Commit
40166c0
·
verified ·
1 Parent(s): 5d2751f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -141
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import streamlit as st
2
  import pandas as pd
 
 
3
  from sklearn.model_selection import train_test_split
4
  from sklearn.preprocessing import LabelEncoder
5
  from sklearn.ensemble import RandomForestClassifier
@@ -9,9 +11,34 @@ from sklearn.neighbors import KNeighborsClassifier
9
  from sklearn.tree import DecisionTreeClassifier
10
  from sklearn.naive_bayes import GaussianNB
11
  from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
12
- import numpy as np
13
- import matplotlib.pyplot as plt
14
- import seaborn as sns
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # File uploader
17
  st.title("Model Training with Metrics")
@@ -29,65 +56,13 @@ if uploaded_file is not None:
29
  if df.empty:
30
  st.warning("The dataset is empty. Please upload a valid CSV file.")
31
  else:
32
- # Handle Null Values (Missing Data)
33
- st.write("Handling Missing (Null) Values:")
34
- # Option to drop rows with null values or fill them
35
- fill_method = st.selectbox("Choose how to handle missing values", ["Drop rows", "Fill with mean/median"])
36
- if fill_method == "Drop rows":
37
- df = df.dropna()
38
- elif fill_method == "Fill with mean/median":
39
- for col in df.columns:
40
- if df[col].dtype in ['float64', 'int64']:
41
- df[col].fillna(df[col].mean(), inplace=True) # For numeric columns, fill with mean
42
- else:
43
- df[col].fillna(df[col].mode()[0], inplace=True) # For categorical columns, fill with mode
44
-
45
- # Handle Outliers using IQR method
46
- st.write("Handling Outliers:")
47
- # Define function to remove outliers using IQR
48
- def remove_outliers_iqr(dataframe):
49
- Q1 = dataframe.quantile(0.25)
50
- Q3 = dataframe.quantile(0.75)
51
- IQR = Q3 - Q1
52
- # Filter out rows that are outside the IQR range
53
- return dataframe[~((dataframe < (Q1 - 1.5 * IQR)) | (dataframe > (Q3 + 1.5 * IQR))).any(axis=1)]
54
-
55
- # Remove outliers from the numerical columns
56
- df = remove_outliers_iqr(df)
57
-
58
- # Handle Extreme Values by Capping (Winsorization)
59
- st.write("Handling Extreme Values (Capping):")
60
- def cap_extreme_values(dataframe):
61
- for col in dataframe.select_dtypes(include=[np.number]).columns:
62
- # Define the thresholds for extreme values (95th percentile and 5th percentile)
63
- lower_limit = dataframe[col].quantile(0.05)
64
- upper_limit = dataframe[col].quantile(0.95)
65
- # Cap the extreme values
66
- dataframe[col] = np.clip(dataframe[col], lower_limit, upper_limit)
67
- return dataframe
68
-
69
- df = cap_extreme_values(df)
70
-
71
- # Show cleaned dataset
72
- st.write("Cleaned Dataset:")
73
- st.dataframe(df)
74
-
75
  target = st.selectbox("Select Target Variable", df.columns)
76
  features = [col for col in df.columns if col != target]
77
  X = df[features]
78
  y = df[target]
79
 
80
- # Label Encoding for categorical columns
81
- label_encoder = LabelEncoder()
82
-
83
- # Encode the target variable (if it's categorical)
84
- if y.dtype == 'object' or len(y.unique()) <= 10: # If the target variable is categorical
85
- y = label_encoder.fit_transform(y)
86
-
87
- # Encode categorical feature columns (if any)
88
- for col in X.columns:
89
- if X[col].dtype == 'object' or len(X[col].unique()) <= 10: # If the column is categorical
90
- X[col] = label_encoder.fit_transform(X[col])
91
 
92
  # Ensure there is enough data before proceeding with train-test split
93
  if len(X) == 0 or len(y) == 0:
@@ -135,101 +110,45 @@ if uploaded_file is not None:
135
  # Create a metrics DataFrame
136
  metrics_df = pd.DataFrame(metrics)
137
 
138
- # Display results in a table using st.dataframe
139
- st.subheader("Model Performance Metrics")
140
- st.dataframe(metrics_df)
141
-
142
- # Download options
143
- st.subheader("Download Model Performance Report in Different Formats")
144
-
145
- # CSV
146
- st.download_button(
147
- label="Download as CSV",
148
- data=metrics_df.to_csv(index=False),
149
- file_name="model_report.csv",
150
- mime="text/csv"
151
  )
152
 
153
- # Excel
 
 
 
 
 
154
  st.download_button(
155
- label="Download as Excel",
156
- data=metrics_df.to_excel(index=False, engine='openpyxl'),
157
  file_name="model_report.xlsx",
158
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
159
  )
160
 
161
- # JSON
162
- st.download_button(
163
- label="Download as JSON",
164
- data=metrics_df.to_json(orient='records'),
165
- file_name="model_report.json",
166
- mime="application/json"
167
- )
168
-
169
- # PDF (using `fpdf` library)
170
- from fpdf import FPDF
171
-
172
- def generate_pdf(df):
173
- pdf = FPDF()
174
- pdf.add_page()
175
- pdf.set_font("Arial", size=12)
176
- pdf.cell(200, 10, txt="Model Performance Report", ln=True, align="C")
177
- pdf.ln(10)
178
-
179
- # Add table header
180
- pdf.set_font("Arial", style='B', size=10)
181
- for header in df.columns:
182
- pdf.cell(40, 10, header, border=1)
183
- pdf.ln()
184
-
185
- # Add table rows
186
- pdf.set_font("Arial", size=10)
187
- for row in df.values:
188
- for value in row:
189
- pdf.cell(40, 10, str(value), border=1)
190
- pdf.ln()
191
-
192
- return pdf.output(dest='S').encode('latin1')
193
-
194
- # PDF download
195
  st.download_button(
196
- label="Download as PDF",
197
- data=generate_pdf(metrics_df),
198
- file_name="model_report.pdf",
199
- mime="application/pdf"
200
- )
201
-
202
- # Option to download the dataset
203
- st.download_button(
204
- label="Download Dataset",
205
- data=df.to_csv(index=False),
206
- file_name="dataset.csv",
207
- mime="text/csv"
208
  )
209
 
210
- # Generate and download PNG report
211
- st.subheader("Download Report as PNG")
212
-
213
- # Create table plot using matplotlib
214
- fig, ax = plt.subplots(figsize=(12, 4)) # Adjust the figure size to match the table's layout
215
- ax.axis('tight')
216
- ax.axis('off')
217
- table_data = metrics_df.values
218
- table_columns = metrics_df.columns.tolist()
219
-
220
- table = ax.table(cellText=table_data, colLabels=table_columns, loc='center', cellLoc='center', colLoc='center')
221
- table.auto_set_font_size(False)
222
- table.set_fontsize(10)
223
- table.scale(1.2, 1.2) # Adjust the scale for better appearance
224
-
225
- # Save the table as a PNG file
226
- png_file = "model_report.png"
227
- fig.savefig(png_file, bbox_inches='tight', dpi=300)
228
-
229
- # Provide a download button for the PNG file
230
- with open(png_file, "rb") as file:
231
  st.download_button(
232
- label="Download as PNG",
233
  data=file,
234
  file_name="model_report.png",
235
  mime="image/png"
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import io
5
  from sklearn.model_selection import train_test_split
6
  from sklearn.preprocessing import LabelEncoder
7
  from sklearn.ensemble import RandomForestClassifier
 
11
  from sklearn.tree import DecisionTreeClassifier
12
  from sklearn.naive_bayes import GaussianNB
13
  from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
14
+ from tabulate import tabulate
15
+
16
+ # Function to convert DataFrame to Excel format
17
+ def to_excel(df):
18
+ output = io.BytesIO()
19
+ with pd.ExcelWriter(output, engine='openpyxl') as writer:
20
+ df.to_excel(writer, index=False, sheet_name='Cleaned Dataset')
21
+ output.seek(0)
22
+ return output
23
+
24
+ # Function to save table as PNG
25
+ def save_table_as_png(df):
26
+ fig, ax = plt.subplots(figsize=(8, 6))
27
+ ax.axis('tight')
28
+ ax.axis('off')
29
+
30
+ # Create a table from the DataFrame
31
+ table = ax.table(cellText=df.values, colLabels=df.columns, loc='center', cellLoc='center')
32
+ table.auto_set_font_size(False)
33
+ table.set_fontsize(10)
34
+ table.scale(1.2, 1.2)
35
+
36
+ # Save the table as a PNG image
37
+ img_path = "/tmp/model_report.png"
38
+ plt.savefig(img_path, format="png", bbox_inches="tight")
39
+ plt.close(fig)
40
+
41
+ return img_path
42
 
43
  # File uploader
44
  st.title("Model Training with Metrics")
 
56
  if df.empty:
57
  st.warning("The dataset is empty. Please upload a valid CSV file.")
58
  else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  target = st.selectbox("Select Target Variable", df.columns)
60
  features = [col for col in df.columns if col != target]
61
  X = df[features]
62
  y = df[target]
63
 
64
+ # Determine if the target is continuous or categorical
65
+ is_classification = y.dtype == 'object' or len(y.unique()) <= 10 # If target is categorical or has few unique values, treat as classification
 
 
 
 
 
 
 
 
 
66
 
67
  # Ensure there is enough data before proceeding with train-test split
68
  if len(X) == 0 or len(y) == 0:
 
110
  # Create a metrics DataFrame
111
  metrics_df = pd.DataFrame(metrics)
112
 
113
+ # Add bold formatting to the headers for tabulate
114
+ bold_headers = [f"\033[1m{header}\033[0m" for header in metrics_df.columns]
115
+
116
+ # Format table with tabulate
117
+ table = tabulate(
118
+ metrics_df,
119
+ headers=bold_headers,
120
+ tablefmt="fancy_grid",
121
+ showindex=False,
122
+ numalign="center",
123
+ stralign="center"
 
 
124
  )
125
 
126
+ # Display results in Streamlit
127
+ st.subheader("Model Performance Metrics")
128
+ st.markdown(f"**Model Performance Metrics**")
129
+ st.text(table)
130
+
131
+ # Option to download the model performance metrics (Results Table)
132
  st.download_button(
133
+ label="Download Model Report (Excel)",
134
+ data=to_excel(metrics_df), # The metrics dataframe
135
  file_name="model_report.xlsx",
136
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
137
  )
138
 
139
+ # Option to download the cleaned dataset
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  st.download_button(
141
+ label="Download Cleaned Dataset (Excel)",
142
+ data=to_excel(df), # The cleaned dataset is 'df'
143
+ file_name="cleaned_dataset.xlsx",
144
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
 
 
 
 
 
 
 
 
145
  )
146
 
147
+ # Option to download the report as PNG
148
+ img_path = save_table_as_png(metrics_df)
149
+ with open(img_path, "rb") as file:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  st.download_button(
151
+ label="Download Model Report (PNG)",
152
  data=file,
153
  file_name="model_report.png",
154
  mime="image/png"