Spaces:
Runtime error
Runtime error
Create pipeline /eda.py
Browse files- src /pipeline /eda.py +104 -0
src /pipeline /eda.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
eda.py
|
| 3 |
+
------
|
| 4 |
+
Exploratory Data Analysis for the Student Performance Prediction System.
|
| 5 |
+
Generates distribution plots and a correlation heatmap using
|
| 6 |
+
Matplotlib and Seaborn, and prints basic statistical summaries
|
| 7 |
+
(mean, median, std, skew) using NumPy/Pandas.
|
| 8 |
+
|
| 9 |
+
Run: python3 src/eda.py
|
| 10 |
+
Outputs PNG charts into assets/
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import os
|
| 14 |
+
import numpy as np
|
| 15 |
+
import pandas as pd
|
| 16 |
+
import matplotlib.pyplot as plt
|
| 17 |
+
import seaborn as sns
|
| 18 |
+
|
| 19 |
+
from data_preprocessing import run_pipeline, NUMERIC_FEATURES, TARGET_COLUMN
|
| 20 |
+
|
| 21 |
+
sns.set_theme(style="whitegrid")
|
| 22 |
+
ASSETS_DIR = "assets"
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def summarize_statistics(df: pd.DataFrame) -> pd.DataFrame:
|
| 26 |
+
"""Basic probability & statistics summary for numeric features."""
|
| 27 |
+
cols = NUMERIC_FEATURES + [TARGET_COLUMN]
|
| 28 |
+
cols = [c for c in cols if c in df.columns]
|
| 29 |
+
summary = pd.DataFrame({
|
| 30 |
+
"mean": df[cols].mean(),
|
| 31 |
+
"median": df[cols].median(),
|
| 32 |
+
"std_dev": df[cols].std(),
|
| 33 |
+
"skew": df[cols].skew(),
|
| 34 |
+
"min": df[cols].min(),
|
| 35 |
+
"max": df[cols].max(),
|
| 36 |
+
})
|
| 37 |
+
return summary.round(2)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def plot_target_distribution(df: pd.DataFrame) -> None:
|
| 41 |
+
plt.figure(figsize=(8, 5))
|
| 42 |
+
sns.histplot(df[TARGET_COLUMN], kde=True, color="#4C72B0", bins=25)
|
| 43 |
+
plt.title("Distribution of Final Exam Scores")
|
| 44 |
+
plt.xlabel("Final Exam Score")
|
| 45 |
+
plt.ylabel("Number of Students")
|
| 46 |
+
plt.tight_layout()
|
| 47 |
+
plt.savefig(os.path.join(ASSETS_DIR, "score_distribution.png"), dpi=150)
|
| 48 |
+
plt.close()
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def plot_correlation_heatmap(df: pd.DataFrame) -> None:
|
| 52 |
+
cols = NUMERIC_FEATURES + [TARGET_COLUMN]
|
| 53 |
+
cols = [c for c in cols if c in df.columns]
|
| 54 |
+
corr = df[cols].corr()
|
| 55 |
+
|
| 56 |
+
plt.figure(figsize=(9, 7))
|
| 57 |
+
sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", square=True, cbar_kws={"shrink": 0.8})
|
| 58 |
+
plt.title("Feature Correlation Heatmap")
|
| 59 |
+
plt.tight_layout()
|
| 60 |
+
plt.savefig(os.path.join(ASSETS_DIR, "correlation_heatmap.png"), dpi=150)
|
| 61 |
+
plt.close()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def plot_study_hours_vs_score(df: pd.DataFrame) -> None:
|
| 65 |
+
plt.figure(figsize=(8, 5))
|
| 66 |
+
sns.scatterplot(data=df, x="study_hours_per_day", y=TARGET_COLUMN,
|
| 67 |
+
hue="performance_band", palette="viridis", alpha=0.7)
|
| 68 |
+
plt.title("Study Hours vs Final Exam Score")
|
| 69 |
+
plt.xlabel("Study Hours per Day")
|
| 70 |
+
plt.ylabel("Final Exam Score")
|
| 71 |
+
plt.tight_layout()
|
| 72 |
+
plt.savefig(os.path.join(ASSETS_DIR, "study_hours_vs_score.png"), dpi=150)
|
| 73 |
+
plt.close()
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def plot_performance_band_counts(df: pd.DataFrame) -> None:
|
| 77 |
+
plt.figure(figsize=(7, 5))
|
| 78 |
+
order = ["Needs Improvement", "Average", "Good", "Excellent"]
|
| 79 |
+
sns.countplot(data=df, x="performance_band", order=order, hue="performance_band",
|
| 80 |
+
palette="Blues_d", legend=False)
|
| 81 |
+
plt.title("Student Count by Performance Band")
|
| 82 |
+
plt.xlabel("Performance Band")
|
| 83 |
+
plt.ylabel("Count")
|
| 84 |
+
plt.tight_layout()
|
| 85 |
+
plt.savefig(os.path.join(ASSETS_DIR, "performance_band_counts.png"), dpi=150)
|
| 86 |
+
plt.close()
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
os.makedirs(ASSETS_DIR, exist_ok=True)
|
| 91 |
+
df = run_pipeline("data/student_data.csv")
|
| 92 |
+
|
| 93 |
+
print("=== Statistical Summary ===")
|
| 94 |
+
print(summarize_statistics(df))
|
| 95 |
+
|
| 96 |
+
print("\n=== Correlation with target (final_exam_score) ===")
|
| 97 |
+
cols = NUMERIC_FEATURES + [TARGET_COLUMN]
|
| 98 |
+
print(df[cols].corr()[TARGET_COLUMN].sort_values(ascending=False).round(3))
|
| 99 |
+
|
| 100 |
+
plot_target_distribution(df)
|
| 101 |
+
plot_correlation_heatmap(df)
|
| 102 |
+
plot_study_hours_vs_score(df)
|
| 103 |
+
plot_performance_band_counts(df)
|
| 104 |
+
print(f"\nSaved 4 charts to {ASSETS_DIR}/")
|