File size: 4,924 Bytes
b1a2e2c
 
df19938
b1a2e2c
 
df19938
91229ce
b1a2e2c
e16e7a5
18555be
 
 
 
 
 
3a15786
18555be
3a15786
 
 
 
18555be
3a15786
18555be
3a15786
18555be
b1a2e2c
e16e7a5
18555be
df19938
e16e7a5
b1a2e2c
3a15786
18555be
3a15786
18555be
3a15786
 
148dd50
e16e7a5
3a15786
 
 
 
 
 
 
 
18555be
3a15786
 
 
 
 
 
 
e16e7a5
b1a2e2c
3a15786
b1a2e2c
91229ce
 
3a15786
148dd50
df19938
72ba179
3a15786
18555be
3a15786
df19938
 
3a15786
148dd50
3a15786
 
 
 
 
 
 
 
 
 
df19938
e16e7a5
3a15786
 
 
 
148dd50
3a15786
 
 
 
18555be
3a15786
 
 
 
 
 
148dd50
3a15786
 
 
 
148dd50
3a15786
 
91229ce
3a15786
b1a2e2c
 
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
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()