File size: 4,779 Bytes
13cc756
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
"""
STEP 2: Fetch Data from MongoDB, Train ML Model, Evaluate & Predict
===================================================================
This script retrieves the 1,000 student documents from MongoDB using
PyMongo, performs preprocessing, splits into train/test, trains a
Linear Regression model, calculates R2 & MAE, and outputs comparisons.
"""

import numpy as np
import pandas as pd
from pymongo import MongoClient
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
import matplotlib.pyplot as plt

def main():
    print("==================================================")
    print("   STEP 2: FETCH, PREPROCESS, TRAIN & EVALUATE    ")
    print("==================================================")
    
    # 1. Connect to MongoDB and fetch records
    print("[1] Connecting to MongoDB to fetch records...")
    client = MongoClient("mongodb://localhost:27017/")
    db = client["student_performance_db"]
    collection = db["students"]
    
    cursor = collection.find()
    df = pd.DataFrame(list(cursor))
    print(f"[+] Retrieved {len(df)} records from MongoDB.")
    
    print("\n--- 5 Sample Records ---")
    print(df.head(5).to_string(index=False))
    
    # 2. Data Preprocessing
    print("\n[2] Preprocessing Data...")
    # Drop MongoDB '_id' and 'student_id' because they are non-numerical identifiers
    df_clean = df.drop(columns=['_id', 'student_id'])
    
    # Check for missing values
    print("[+] Missing values check:\n", df_clean.isnull().sum())
    
    # Split into Features (X) and Target (y)
    X = df_clean[['study_hours', 'attendance', 'previous_marks', 'assignments_score']]
    y = df_clean['final_marks']
    
    # 80% Training and 20% Testing Split
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.20, random_state=42
    )
    print(f"[+] Train dataset size : {len(X_train)} students (80%)")
    print(f"[+] Test dataset size  : {len(X_test)} students (20%)")
    
    # 3. Train Linear Regression Model
    print("\n[3] Training Linear Regression Model...")
    model = LinearRegression()
    model.fit(X_train, y_train)
    print("[+] Model trained successfully!")
    
    # 4. Model Evaluation
    y_pred = model.predict(X_test)
    r2 = r2_score(y_test, y_pred)
    mae = mean_absolute_error(y_test, y_pred)
    rmse = np.sqrt(mean_squared_error(y_test, y_pred))
    
    print("\n--- Model Evaluation Results ---")
    print(f"R-squared (R2) Score : {r2:.4f} ({r2*100:.2f}% accuracy)")
    print(f"Mean Absolute Error  : {mae:.2f} marks")
    print(f"Root Mean Sq. Error  : {rmse:.2f} marks")
    
    # 5. Display First 10 Sample Predictions
    print("\n--- Actual vs Predicted Marks (First 10 Test Students) ---")
    comparison = pd.DataFrame({
        "Actual Marks": y_test.values[:10],
        "Predicted Marks": np.round(y_pred[:10], 1),
        "Difference": np.round(np.abs(y_test.values[:10] - y_pred[:10]), 1)
    })
    print(comparison.to_string(index=True))
    
    # 6. Save Visualization
    plt.figure(figsize=(12, 5))
    
    plt.subplot(1, 2, 1)
    plt.scatter(y_test, y_pred, color='#2563EB', alpha=0.6, edgecolors='k', s=40)
    plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], color='#DC2626', linestyle='--', linewidth=2, label='Perfect Fit')
    plt.title("Actual vs Predicted Marks (Linear Regression)", fontweight='bold')
    plt.xlabel("Actual Marks")
    plt.ylabel("Predicted Marks")
    plt.grid(True, linestyle=':', alpha=0.6)
    plt.legend()
    
    plt.subplot(1, 2, 2)
    indices = np.arange(10)
    width = 0.35
    plt.bar(indices - width/2, y_test.values[:10], width, label='Actual', color='#3B82F6')
    plt.bar(indices + width/2, y_pred[:10], width, label='Predicted', color='#10B981')
    plt.title("Comparison for 10 Students", fontweight='bold')
    plt.xlabel("Student Index")
    plt.ylabel("Marks")
    plt.xticks(indices, [f"S{i+1}" for i in indices])
    plt.grid(True, linestyle=':', alpha=0.6)
    plt.legend()
    
    plt.tight_layout()
    plt.savefig("actual_vs_predicted.png", dpi=300)
    plt.close()
    print("\n[+] Saved visualization plot as 'actual_vs_predicted.png'")
    
    # 7. Predict for a new student
    sample_student = pd.DataFrame([{
        'study_hours': 7.0,
        'attendance': 92.0,
        'previous_marks': 80.0,
        'assignments_score': 9
    }])
    prediction = model.predict(sample_student)[0]
    print(f"\n[+] Demo Prediction for Student with 7 hrs study, 92% attendance, 80 prev marks, 9/10 assignments:")
    print(f"    --> Predicted Final Marks: {prediction:.1f} / 100")
    
    client.close()

if __name__ == "__main__":
    main()