Preprocessing / app.py
SanthiSastra's picture
Update app.py
72ba179 verified
Raw History Blame Contribute Delete
4.92 kB
import gradio as gr
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from docx import Document
from docx.shared import Inches
from scipy.stats import skew
def analyze_nature(df):
num_cols = df.select_dtypes(include=[np.number]).columns
insights = []
for col in num_cols:
# Symmetry Check
s = skew(df[col].dropna())
shape = "Symmetric" if abs(s) < 0.5 else ("Right-Skewed" if s > 0.5 else "Left-Skewed")
# Outlier Check (IQR)
Q1, Q3 = df[col].quantile(0.25), df[col].quantile(0.75)
IQR = Q3 - Q1
outlier_count = ((df[col] < (Q1 - 1.5 * IQR)) | (df[col] > (Q3 + 1.5 * IQR))).sum()
insights.append(f"• {col}: {shape} (Skew: {s:.3f}), Outliers: {outlier_count}")
return "\n".join(insights)
def perform_mining(file):
if file is None: return [None]*6
df = pd.read_csv(file.name)
num_cols = df.select_dtypes(include=[np.number]).columns.tolist()
# Statistical Summary & Nature
stats = df.describe().round(3).reset_index()
nature_report = analyze_nature(df)
# Plots Generation (15 distinct graphs)
fig = plt.figure(figsize=(16, 22))
for i in range(15):
plt.subplot(5, 3, i+1)
if i < 4 and i < len(num_cols): # Histograms
sns.histplot(df[num_cols[i]].dropna(), kde=True, color='teal')
plt.title(f"Hist: {num_cols[i]}")
elif i < 8 and (i-4) < len(num_cols): # Boxplots
sns.boxplot(y=df[num_cols[i-4]].dropna(), color='gold')
plt.title(f"Box: {num_cols[i-4]}")
elif i == 8: # Correlogram
sns.heatmap(df[num_cols].corr(), annot=True, cmap='coolwarm', fmt=".2f", cbar=False)
plt.title("Correlogram")
elif i < 12 and (i-9) < len(num_cols): # Violin Plots
sns.violinplot(y=df[num_cols[i-9]].dropna(), color='lightgreen')
plt.title(f"Density: {num_cols[i-9]}")
else: # Scatter Plots
idx = (i-12) % len(num_cols)
sns.scatterplot(x=df[num_cols[0]], y=df[num_cols[idx]], alpha=0.6)
plt.title(f"Scatter: {num_cols[idx]}")
plt.tight_layout()
plot_path = "mining_dashboard.png"
plt.savefig(plot_path)
plt.close()
# Report Generation
doc_path = "Sastra_Data_Mining_Report.docx"
doc = Document()
doc.add_heading('Amrita Manthan: Preprocessing Report-Prof.B.Santhi-SRC-SASTRA', 0)
doc.add_heading('Nature of Data & Outlier Analysis', level=1)
doc.add_paragraph(nature_report)
doc.add_picture(plot_path, width=Inches(6))
doc.save(doc_path)
return stats, f"Rows: {df.shape[0]}, Cols: {df.shape[1]}", plot_path, nature_report, "Analysis Complete", doc_path
with gr.Blocks(theme=gr.themes.Default()) as demo:
# --- CENTERED LOGO AND LEFT ALIGNED TITLES ---
with gr.Column(elem_id="header-container"):
with gr.Row(): # Force centering of the image row
gr.Image("logo.jpg", width=120, show_label=False, container=False, scale=0)
# Left-aligned text
gr.Markdown("<h1 style='color: #0000FF; text-align: left; margin-bottom:0;'><b>Amrita Manthan</b></h1>")
gr.Markdown("<h2 style='color: #0000FF; text-align: left; margin-top:0;'><b>Preprocessing & Data Mining Portal</b></h2>")
gr.Markdown("<h3 style='color: #0000FF; text-align: left;'><b>Prof. B. Santhi, SRC, SASTRA</b></h3>")
with gr.Tabs():
with gr.TabItem("Step 1: Theory & Input"):
gr.Markdown("### **Concepts & Formulae**")
gr.Markdown(r"**Normalization (Z-Score):** $$z = \frac{x - \mu}{\sigma}$$")
gr.Markdown(r"**Skewness (Symmetry):** $$\gamma_1 = \frac{E[(X-\mu)^3]}{\sigma^3}$$")
u_file = gr.File(label="Upload CSV Dataset")
run_btn = gr.Button("RUN DATA MINING ENGINE", variant="primary")
with gr.TabItem("Step 2: Descriptive Summary"):
out_desc = gr.Markdown()
out_stats = gr.DataFrame(label="Descriptive Statistics (3-Decimal Correction)")
with gr.TabItem("Step 3: Nature of Data"):
out_nature = gr.Textbox(label="Outlier & Symmetry Report", lines=12)
gr.Markdown("**Interpretation:** *Symmetric* (Mean ≈ Median), *Skewed* (Long tail exists), *Outliers* (Points outside 1.5*IQR).")
with gr.TabItem("Step 4: Visual Dashboard"):
out_msg = gr.Label(label="Status")
out_img = gr.Image(label="EDA Dashboard (15 Plots including Correlogram)")
with gr.TabItem("Step 5: Export"):
out_file = gr.File(label="Download Full Report (DOCX)")
run_btn.click(perform_mining, u_file, [out_stats, out_desc, out_img, out_nature, out_msg, out_file])
demo.launch()