File size: 5,333 Bytes
317b73e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
annotations_creators:
- machine-generated
language:
- en
license:
- mit
task_categories:
- tabular-regression
tags:
- education
- machine-learning
- regression
- student-performance
- mongodb
size_categories:
- 1K<n<10K
---

# Student Performance Prediction Using Python, MongoDB, and Scikit-Learn

A beginner-friendly Machine Learning project for B.Tech Computer Science and Engineering (CSE) students.

---

## πŸ“Œ Project Overview
- **Objective:** Predict students' final examination marks out of 100 based on study hours, attendance, previous marks, and assignment completion.
- **Database:** MongoDB (`student_performance_db`)
- **Dataset:** 1,000 realistic student records
- **Python Driver:** PyMongo
- **Machine Learning Model:** Linear Regression
- **Evaluation Metrics:** $R^2$ Score (96.1%) and Mean Absolute Error (2.04 marks)

---

## πŸ“‚ Project Structure
```text
ca2 mdb/
β”‚
β”œβ”€β”€ main.py                          # Complete all-in-one executable script
β”œβ”€β”€ step1_insert_to_mongodb.py       # Step 1: Generate dataset & insert into MongoDB
β”œβ”€β”€ step2_train_and_predict.py       # Step 2: Query MongoDB, train model, evaluate & predict
β”œβ”€β”€ Student_Performance_Prediction.ipynb # Jupyter Notebook implementation
β”œβ”€β”€ requirements.txt                 # Required Python packages
β”œβ”€β”€ student_data.csv                 # Generated dataset backup
β”œβ”€β”€ actual_vs_predicted.png          # High-resolution output evaluation plot
β”œβ”€β”€ PROJECT_REPORT.md                # Full academic project report (8 sections)
β”œβ”€β”€ VIVA_QUESTIONS.md                # Viva / oral exam preparation guide
└── README.md                        # Project instructions and quickstart
```

---

## βš™οΈ Prerequisites & Installation

### 1. Ensure MongoDB Server is Running
If you have MongoDB Community Server installed on Windows:
- MongoDB runs as a background service on port `27017`.
- You can verify with PowerShell:
  ```powershell
  Get-Service -Name "*mongo*"
  ```
  If stopped, start it with:
  ```powershell
  net start MongoDB
  ```

### 2. Install Required Python Libraries
Open your terminal / command prompt in this project folder and run:
```bash
pip install -r requirements.txt
```
*(Or install individually: `pip install pymongo pandas numpy scikit-learn matplotlib`)*

---

## πŸš€ How to Run the Project

### Option A: Run the Complete Pipeline (Recommended)
Run the all-in-one script:
```bash
python main.py
```
This script will:
1. Generate 1,000 realistic student records.
2. Connect to MongoDB and insert all 1,000 documents.
3. Retrieve records from MongoDB using PyMongo.
4. Display 5 sample records.
5. Preprocess the dataset and split it 80/20 into train/test sets.
6. Train a Linear Regression model.
7. Print $R^2$ Score, MAE, and RMSE.
8. Display a table of Actual vs Predicted Marks for sample students.
9. Save visualization graphs to `actual_vs_predicted.png`.
10. Test prediction on a custom student profile.

---

### Option B: Run Modular Scripts Step-by-Step

**Step 1: Populate MongoDB**
```bash
python step1_insert_to_mongodb.py
```

**Step 2: Train Model & Evaluate**
```bash
python step2_train_and_predict.py
```

---

### Option C: Jupyter Notebook / VS Code Interactive
Open `Student_Performance_Prediction.ipynb` in VS Code or Jupyter Lab, and run the cells sequentially.

---

## πŸ“Š Expected Output Summary

```text
==================================================
STEP 3: FETCHING DATA FROM MONGODB USING PYMONGO
==================================================
-> Successfully fetched 1000 records from MongoDB.

--- 5 Sample Records Retreived from MongoDB ---
                     _id student_id  study_hours  attendance  previous_marks  assignments_score  final_marks
6ae78958eca66c4748b15e77    STU1001          4.2        67.2            54.4                  6         70.2
6ae78958eca66c4748b15e78    STU1002          9.1        81.1            53.6                  4         81.3
6ae78958eca66c4748b15e79    STU1003          7.2        94.0            89.8                  8        100.0
6ae78958eca66c4748b15e7a    STU1004          6.1        88.6            53.7                 10         84.1
6ae78958eca66c4748b15e7b    STU1005          2.3        91.5            55.0                  9         67.2

==================================================
STEP 6: MODEL EVALUATION METRICS
==================================================
1. R-Squared (R2) Score : 0.9610 (96.10% variance explained)
2. Mean Absolute Error  : 2.04 marks
3. Root Mean Sq. Error  : 2.53 marks

==================================================
STEP 7: ACTUAL VS PREDICTED RESULTS (FIRST 10 TEST STUDENTS)
==================================================
   Actual Marks  Predicted Marks  Difference (Error)
0          66.7             66.1                 0.6
1          86.9             89.4                 2.5
2          76.9             77.5                 0.6
3          73.3             76.3                 3.0
4          81.4             80.2                 1.2
5          86.6             83.3                 3.3
6          58.2             60.9                 2.7
7          83.9             81.6                 2.3
8          65.3             63.4                 1.9
9          69.4             72.5                 3.1
```