Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 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,40 +9,9 @@ from sklearn.neighbors import KNeighborsClassifier
|
|
| 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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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 with bold headings
|
| 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 |
-
|
| 33 |
-
# Set the font size and bold the header row
|
| 34 |
-
table.auto_set_font_size(False)
|
| 35 |
-
table.set_fontsize(10)
|
| 36 |
-
table.scale(1.2, 1.2)
|
| 37 |
-
|
| 38 |
-
# Bold the column headers
|
| 39 |
-
for (i, j) in zip(range(len(df.columns)), table[0]):
|
| 40 |
-
table[0, j].set_text_props(weight='bold') # Make column headers bold
|
| 41 |
-
|
| 42 |
-
# Save the table as a PNG image
|
| 43 |
-
img_path = "/tmp/model_report.png"
|
| 44 |
-
plt.savefig(img_path, format="png", bbox_inches="tight")
|
| 45 |
-
plt.close(fig)
|
| 46 |
-
|
| 47 |
-
return img_path
|
| 48 |
|
| 49 |
# File uploader
|
| 50 |
st.title("Model Training with Metrics")
|
|
@@ -62,13 +29,65 @@ if uploaded_file is not None:
|
|
| 62 |
if df.empty:
|
| 63 |
st.warning("The dataset is empty. Please upload a valid CSV file.")
|
| 64 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
target = st.selectbox("Select Target Variable", df.columns)
|
| 66 |
features = [col for col in df.columns if col != target]
|
| 67 |
X = df[features]
|
| 68 |
y = df[target]
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Ensure there is enough data before proceeding with train-test split
|
| 74 |
if len(X) == 0 or len(y) == 0:
|
|
@@ -116,45 +135,101 @@ if uploaded_file is not None:
|
|
| 116 |
# Create a metrics DataFrame
|
| 117 |
metrics_df = pd.DataFrame(metrics)
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
bold_headers = [f"\033[1m{header}\033[0m" for header in metrics_df.columns]
|
| 121 |
-
|
| 122 |
-
# Format table with tabulate
|
| 123 |
-
table = tabulate(
|
| 124 |
-
metrics_df,
|
| 125 |
-
headers=bold_headers,
|
| 126 |
-
tablefmt="fancy_grid",
|
| 127 |
-
showindex=False,
|
| 128 |
-
numalign="center",
|
| 129 |
-
stralign="center"
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
# Display results in Streamlit
|
| 133 |
st.subheader("Model Performance Metrics")
|
| 134 |
-
st.
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
#
|
| 138 |
st.download_button(
|
| 139 |
-
label="Download
|
| 140 |
-
data=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
file_name="model_report.xlsx",
|
| 142 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 143 |
)
|
| 144 |
|
| 145 |
-
#
|
| 146 |
st.download_button(
|
| 147 |
-
label="Download
|
| 148 |
-
data=
|
| 149 |
-
file_name="
|
| 150 |
-
mime="application/
|
| 151 |
)
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
st.download_button(
|
| 157 |
-
label="Download
|
| 158 |
data=file,
|
| 159 |
file_name="model_report.png",
|
| 160 |
mime="image/png"
|
|
|
|
| 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 |
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 |
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 |
# 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"
|