Datasets:
Upload PROJECT_REPORT.md with huggingface_hub
Browse files- PROJECT_REPORT.md +321 -108
PROJECT_REPORT.md
CHANGED
|
@@ -1,47 +1,106 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
---
|
| 9 |
|
| 10 |
## 1. INTRODUCTION
|
| 11 |
|
| 12 |
-
Predicting student academic performance is
|
| 13 |
|
| 14 |
-
This project
|
| 15 |
|
| 16 |
---
|
| 17 |
|
| 18 |
## 2. PROJECT OBJECTIVES
|
| 19 |
|
| 20 |
-
1. **
|
| 21 |
-
2. **
|
| 22 |
-
3. **
|
| 23 |
-
4. **
|
| 24 |
-
5. **
|
| 25 |
-
6. **
|
| 26 |
|
| 27 |
---
|
| 28 |
|
| 29 |
-
## 3. DATASET DESCRIPTION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
| :--- | :--- | :--- | :--- | :--- |
|
| 35 |
-
| `student_id` | String | STU1001 – STU2000 | Unique student
|
| 36 |
-
| `study_hours` | Float | 1.0 – 9.5 | Average
|
| 37 |
-
| `attendance` | Float | 60.0% – 99.0% |
|
| 38 |
-
| `previous_marks`| Float | 40.0 – 95.0 | Mid-term /
|
| 39 |
| `assignments_score` | Integer | 4 – 10 | Assignment and lab task evaluation score (out of 10) | Independent Feature ($X_4$) |
|
| 40 |
-
| `final_marks` | Float | 30.0 – 100.0 | Final examination score | **
|
| 41 |
|
| 42 |
-
### Sample Records
|
| 43 |
|
| 44 |
-
```
|
| 45 |
student_id study_hours attendance previous_marks assignments_score final_marks
|
| 46 |
STU1001 4.2 67.2 54.4 6 70.2
|
| 47 |
STU1002 9.1 81.1 53.6 4 81.3
|
|
@@ -54,128 +113,285 @@ student_id study_hours attendance previous_marks assignments_score final_ma
|
|
| 54 |
|
| 55 |
## 4. MONGODB AND PYMONGO IMPLEMENTATION
|
| 56 |
|
| 57 |
-
###
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
### Database Architecture:
|
| 61 |
-
- **Server URL:** `mongodb://localhost:27017/`
|
| 62 |
- **Database Name:** `student_performance_db`
|
| 63 |
- **Collection Name:** `students`
|
| 64 |
-
- **
|
| 65 |
-
|
| 66 |
-
### Step-by-Step Implementation with PyMongo:
|
| 67 |
|
| 68 |
-
###
|
| 69 |
```python
|
| 70 |
from pymongo import MongoClient
|
| 71 |
|
| 72 |
-
# Establish connection to
|
| 73 |
client = MongoClient("mongodb://localhost:27017/")
|
| 74 |
-
|
| 75 |
-
# Select database and collection
|
| 76 |
db = client["student_performance_db"]
|
| 77 |
collection = db["students"]
|
| 78 |
```
|
| 79 |
|
| 80 |
-
###
|
| 81 |
-
The dataset is converted into a list of dictionaries via `df.to_dict(orient="records")` and inserted in bulk using `insert_many()`:
|
| 82 |
```python
|
| 83 |
-
#
|
| 84 |
records = df.to_dict(orient="records")
|
|
|
|
|
|
|
|
|
|
| 85 |
result = collection.insert_many(records)
|
| 86 |
-
print(f"
|
| 87 |
```
|
| 88 |
|
| 89 |
-
###
|
| 90 |
-
All documents are retrieved using `collection.find()` and instantly converted into a Pandas DataFrame:
|
| 91 |
```python
|
|
|
|
| 92 |
cursor = collection.find()
|
| 93 |
-
df = pd.DataFrame(list(cursor))
|
| 94 |
-
```
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
|
| 98 |
---
|
| 99 |
|
| 100 |
## 5. DATA PREPROCESSING
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
### 1. Removing Non-Predictive Columns
|
| 105 |
-
- The MongoDB identifier `_id` and academic roll number `student_id` are non-numerical unique strings.
|
| 106 |
-
- Keeping them would cause errors or cause the model to memorize IDs rather than learn academic relationships.
|
| 107 |
```python
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
```
|
| 110 |
|
| 111 |
-
|
| 112 |
-
A complete check using `df_clean.isnull().sum()` confirmed zero null values across all 1,000 records.
|
| 113 |
|
| 114 |
-
##
|
| 115 |
-
We divide the dataset into:
|
| 116 |
-
- **Features ($X$):** `study_hours`, `attendance`, `previous_marks`, `assignments_score`
|
| 117 |
-
- **Target ($y$):** `final_marks`
|
| 118 |
|
| 119 |
-
|
| 120 |
-
To validate that the model learns general patterns rather than memorizing data:
|
| 121 |
-
- **Training Set (80% = 800 records):** Used by the algorithm to adjust its internal mathematical weights.
|
| 122 |
-
- **Testing Set (20% = 200 records):** Unseen student records used to test real-world prediction accuracy.
|
| 123 |
-
- `random_state=42` ensures the split is reproducible across different computer runs.
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
|
| 129 |
-
|
| 130 |
-
**Linear Regression** is a foundational supervised learning algorithm used when predicting a continuous numerical value (such as marks from 0 to 100).
|
| 131 |
|
| 132 |
-
|
| 133 |
-
Imagine drawing a straight line through a scatter plot of data points so that the distance between the line and every point is minimized.
|
| 134 |
|
| 135 |
-
|
| 136 |
|
| 137 |
-
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
- $b$ is the **bias / intercept** (the baseline score).
|
| 142 |
|
| 143 |
-
|
| 144 |
-
```python
|
| 145 |
-
from sklearn.linear_model import LinearRegression
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
```
|
| 151 |
|
| 152 |
-
|
| 153 |
-
- **Study Hours Weight ($w_1$):** `+3.47` (Each additional hour of daily study raises final marks by ~3.5 marks)
|
| 154 |
-
- **Previous Marks Weight ($w_2$):** `+0.44` (Prior academic performance provides strong baseline stability)
|
| 155 |
-
- **Attendance Weight ($w_3$):** `+0.23` (Consistent attendance boosts exam readiness)
|
| 156 |
-
- **Assignments Weight ($w_4$):** `+1.43` (Practical assignment work directly improves marks)
|
| 157 |
-
- **Intercept ($b$):** `2.83`
|
| 158 |
|
| 159 |
---
|
| 160 |
|
| 161 |
-
##
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
|
| 165 |
-
|
| 166 |
|
| 167 |
-
|
| 168 |
-
- *Explanation:* $R^2$ measures how well the independent features explain the variation in the final marks. An $R^2$ of 0.9610 indicates that **96.1%** of the variance in students' final marks is accurately captured by study hours, attendance, previous marks, and assignment completion.
|
| 169 |
-
|
| 170 |
-
2. **Mean Absolute Error (MAE): `2.04 marks`**
|
| 171 |
-
- *Explanation:* MAE represents the average magnitude of prediction error in marks. On average, the model's predicted mark differs from the student's actual mark by **only ~2 marks out of 100**, demonstrating high precision.
|
| 172 |
|
| 173 |
-
|
| 174 |
-
- *Explanation:* RMSE penalizes larger errors more heavily. A low value of 2.53 confirms that there are virtually no extreme outlier prediction mistakes.
|
| 175 |
|
| 176 |
-
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
| :---: | :---: | :---: | :---: |
|
| 180 |
| Student 1 | 66.7 | 66.1 | 0.6 |
|
| 181 |
| Student 2 | 86.9 | 89.4 | 2.5 |
|
|
@@ -188,19 +404,16 @@ The trained model made predictions on all 200 unseen test students (`y_pred = mo
|
|
| 188 |
| Student 9 | 65.3 | 63.4 | 1.9 |
|
| 189 |
| Student 10 | 69.4 | 72.5 | 3.1 |
|
| 190 |
|
| 191 |
-
### Visualization:
|
| 192 |
-
A high-resolution visualization was generated (`actual_vs_predicted.png`) displaying:
|
| 193 |
-
1. **Correlation Scatter Plot:** Actual marks vs predicted marks showing points tightly clustered along the perfect reference line ($y = x$).
|
| 194 |
-
2. **Side-by-Side Comparison Bar Chart:** Actual marks vs predicted marks for sample students showing minimal discrepancy.
|
| 195 |
-
|
| 196 |
---
|
| 197 |
|
| 198 |
-
##
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
- **Practical Relevance:** The system enables educators to input any student's current study hours, attendance, and mid-term marks to instantly forecast their final exam outcome and intervene early.
|
| 205 |
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ==============================================================================
|
| 2 |
+
# FRONT PAGE
|
| 3 |
+
# ==============================================================================
|
| 4 |
|
| 5 |
+
<div align="center">
|
| 6 |
+
|
| 7 |
+
# A PROJECT REPORT
|
| 8 |
+
### ON
|
| 9 |
+
# **STUDENT PERFORMANCE PREDICTION USING PYTHON, MONGODB, AND MACHINE LEARNING**
|
| 10 |
+
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
*Submitted in partial fulfillment of the requirements for the award of the degree of*
|
| 14 |
+
|
| 15 |
+
### **BACHELOR OF TECHNOLOGY (B.TECH)**
|
| 16 |
+
### **IN**
|
| 17 |
+
### **COMPUTER SCIENCE & ENGINEERING**
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
<br>
|
| 22 |
+
|
| 23 |
+
| **Candidate Details** | **Institutional Information** |
|
| 24 |
+
| :--- | :--- |
|
| 25 |
+
| **Submitted By:** Ansh Gupta | **Department:** Department of Computer Science & Engineering |
|
| 26 |
+
| **Hugging Face ID:** [@techbire](https://huggingface.co/techbire) | **Degree:** B.Tech (CSE) |
|
| 27 |
+
| **Academic Session:** 2025 – 2026 | **Domain:** DBMS & Applied Machine Learning |
|
| 28 |
+
|
| 29 |
+
<br>
|
| 30 |
+
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
</div>
|
| 34 |
+
|
| 35 |
+
<pagebreak>
|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
## 📑 TABLE OF CONTENTS
|
| 40 |
+
|
| 41 |
+
1. [Introduction](#1-introduction)
|
| 42 |
+
2. [Project Objectives](#2-project-objectives)
|
| 43 |
+
3. [Dataset Description & Dataset Link](#3-dataset-description--dataset-link)
|
| 44 |
+
4. [MongoDB and PyMongo Implementation](#4-mongodb-and-pymongo-implementation)
|
| 45 |
+
5. [Data Preprocessing](#5-data-preprocessing)
|
| 46 |
+
6. [Machine Learning Model Implementation](#6-machine-learning-model-implementation)
|
| 47 |
+
7. [Source Code](#7-complete-source-code)
|
| 48 |
+
8. [Screenshot of Source Code](#8-screenshot-of-source-code)
|
| 49 |
+
9. [Output Screenshots & Evaluation Results](#9-output-screenshots--evaluation-results)
|
| 50 |
+
10. [Conclusion & Future Scope](#10-conclusion--future-scope)
|
| 51 |
|
| 52 |
---
|
| 53 |
|
| 54 |
## 1. INTRODUCTION
|
| 55 |
|
| 56 |
+
Predicting student academic performance is one of the most critical and impactful applications of Educational Data Mining (EDM). In conventional higher education environments, academic interventions often occur only after semester examinations—when it is already too late for students facing academic distress. Developing proactive early-warning systems enables educators to identify students who may require additional academic support well before final assessments.
|
| 57 |
|
| 58 |
+
This project delivers an end-to-end, industry-aligned machine learning application that connects a modern NoSQL database (**MongoDB**) with **Python (PyMongo)** and Scikit-Learn's **Linear Regression** algorithm. Unlike typical classroom projects that rely on static local CSV files, this project implements a production-grade database architecture where 1,000 student academic profiles are stored, indexed, queried dynamically, preprocessed, and evaluated through predictive machine learning.
|
| 59 |
|
| 60 |
---
|
| 61 |
|
| 62 |
## 2. PROJECT OBJECTIVES
|
| 63 |
|
| 64 |
+
1. **NoSQL Database Integration:** Connect a NoSQL document database (MongoDB) with Python using the official `pymongo` driver.
|
| 65 |
+
2. **Realistic Big Data Management:** Store and query a realistic 1,000-record dataset of student profiles inside a MongoDB collection.
|
| 66 |
+
3. **Automated ETL Pipeline:** Extract documents from MongoDB, Transform (clean/preprocess) features in Pandas, and Load them into Machine Learning algorithms.
|
| 67 |
+
4. **Supervised Regression Modeling:** Implement **Linear Regression** to model the mathematical relationship between study habits, attendance, past academic performance, and final exam marks.
|
| 68 |
+
5. **Rigorous Evaluation:** Validate model performance against unseen student data using statistical metrics: **$R^2$ Score**, **Mean Absolute Error (MAE)**, and **Root Mean Squared Error (RMSE)**.
|
| 69 |
+
6. **Open Science & Reproducibility:** Publish the full dataset on the **Hugging Face Hub** for open access and academic verification.
|
| 70 |
|
| 71 |
---
|
| 72 |
|
| 73 |
+
## 3. DATASET DESCRIPTION & DATASET LINK
|
| 74 |
+
|
| 75 |
+
### 🌐 Official Hugging Face Dataset Link
|
| 76 |
+
The complete 1,000-record dataset and project repository are publicly available at:
|
| 77 |
+
👉 **[https://huggingface.co/datasets/techbire/students](https://huggingface.co/datasets/techbire/students)**
|
| 78 |
+
|
| 79 |
+
You can also load this dataset in Python using:
|
| 80 |
+
```python
|
| 81 |
+
from datasets import load_dataset
|
| 82 |
+
dataset = load_dataset("techbire/students")
|
| 83 |
+
df = dataset["train"].to_pandas()
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
---
|
| 87 |
|
| 88 |
+
### Dataset Schema & Features
|
| 89 |
|
| 90 |
+
The dataset comprises exactly **1,000 student profiles** with continuous and discrete academic indicators:
|
| 91 |
+
|
| 92 |
+
| Feature Name | Type | Range | Meaning | Role in ML |
|
| 93 |
| :--- | :--- | :--- | :--- | :--- |
|
| 94 |
+
| `student_id` | String | STU1001 – STU2000 | Unique institutional student roll number | Identifier (Excluded from ML) |
|
| 95 |
+
| `study_hours` | Float | 1.0 – 9.5 | Average self-study hours per day | Independent Feature ($X_1$) |
|
| 96 |
+
| `attendance` | Float | 60.0% – 99.0% | Overall lecture attendance percentage | Independent Feature ($X_2$) |
|
| 97 |
+
| `previous_marks`| Float | 40.0 – 95.0 | Mid-term / earlier semester exam score (out of 100) | Independent Feature ($X_3$) |
|
| 98 |
| `assignments_score` | Integer | 4 – 10 | Assignment and lab task evaluation score (out of 10) | Independent Feature ($X_4$) |
|
| 99 |
+
| `final_marks` | Float | 30.0 – 100.0 | Final examination score | **Target Output ($y$)** |
|
| 100 |
|
| 101 |
+
### Sample Records Retreived from MongoDB:
|
| 102 |
|
| 103 |
+
```text
|
| 104 |
student_id study_hours attendance previous_marks assignments_score final_marks
|
| 105 |
STU1001 4.2 67.2 54.4 6 70.2
|
| 106 |
STU1002 9.1 81.1 53.6 4 81.3
|
|
|
|
| 113 |
|
| 114 |
## 4. MONGODB AND PYMONGO IMPLEMENTATION
|
| 115 |
|
| 116 |
+
### Architecture Overview
|
| 117 |
+
- **Database Server:** MongoDB Community Server (Port `27017`)
|
|
|
|
|
|
|
|
|
|
| 118 |
- **Database Name:** `student_performance_db`
|
| 119 |
- **Collection Name:** `students`
|
| 120 |
+
- **Total Documents:** 1,000 BSON Documents
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
### 1. Connecting Python to MongoDB
|
| 123 |
```python
|
| 124 |
from pymongo import MongoClient
|
| 125 |
|
| 126 |
+
# Establish connection to local MongoDB server instance
|
| 127 |
client = MongoClient("mongodb://localhost:27017/")
|
|
|
|
|
|
|
| 128 |
db = client["student_performance_db"]
|
| 129 |
collection = db["students"]
|
| 130 |
```
|
| 131 |
|
| 132 |
+
### 2. Bulk Insertion of Records
|
|
|
|
| 133 |
```python
|
| 134 |
+
# Convert DataFrame to list of BSON-compatible dictionaries
|
| 135 |
records = df.to_dict(orient="records")
|
| 136 |
+
|
| 137 |
+
# Clear existing documents and insert 1,000 records
|
| 138 |
+
collection.delete_many({})
|
| 139 |
result = collection.insert_many(records)
|
| 140 |
+
print(f"Successfully inserted {len(result.inserted_ids)} records into MongoDB!")
|
| 141 |
```
|
| 142 |
|
| 143 |
+
### 3. Querying Records for Machine Learning
|
|
|
|
| 144 |
```python
|
| 145 |
+
# Retrieve all records from the database
|
| 146 |
cursor = collection.find()
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
# Convert the cursor directly into a Pandas DataFrame
|
| 149 |
+
df_from_db = pd.DataFrame(list(cursor))
|
| 150 |
+
```
|
| 151 |
|
| 152 |
---
|
| 153 |
|
| 154 |
## 5. DATA PREPROCESSING
|
| 155 |
|
| 156 |
+
1. **Identifier Removal:** MongoDB assigns each document an internal 12-byte primary key (`_id`), and the dataset includes `student_id`. These columns are non-predictive strings and are dropped so the algorithm does not memorize IDs:
|
| 157 |
+
```python
|
| 158 |
+
data = df.drop(columns=['_id', 'student_id'])
|
| 159 |
+
```
|
| 160 |
+
2. **Missing Value Validation:** A strict null check (`data.isnull().sum()`) confirmed zero missing values.
|
| 161 |
+
3. **Feature-Target Separation:**
|
| 162 |
+
- Independent Features ($X$): `study_hours`, `attendance`, `previous_marks`, `assignments_score`
|
| 163 |
+
- Target Label ($y$): `final_marks`
|
| 164 |
+
4. **Train-Test Partitioning (80% / 20%):**
|
| 165 |
+
- **Training Set (800 records):** Used to train model parameters.
|
| 166 |
+
- **Testing Set (200 records):** Unseen data reserved for objective evaluation.
|
| 167 |
+
- Fixed random state (`random_state=42`) ensures 100% experimental reproducibility.
|
| 168 |
+
|
| 169 |
+
---
|
| 170 |
+
|
| 171 |
+
## 6. MACHINE LEARNING MODEL IMPLEMENTATION
|
| 172 |
+
|
| 173 |
+
### Algorithm: Linear Regression
|
| 174 |
+
Linear Regression is a foundational supervised learning algorithm designed for predicting continuous outcomes. It fits an optimal hyperplane by minimizing the Residual Sum of Squares (RSS) between true and predicted values.
|
| 175 |
+
|
| 176 |
+
$$\text{Final Marks} = (w_1 \times \text{study\_hours}) + (w_2 \times \text{attendance}) + (w_3 \times \text{previous\_marks}) + (w_4 \times \text{assignments\_score}) + b$$
|
| 177 |
+
|
| 178 |
+
### Learned Weights (Coefficients) from Training:
|
| 179 |
+
- **Study Hours Coefficient ($w_1$):** `+3.4735` (Every additional hour of daily study contributes $\approx 3.47$ marks)
|
| 180 |
+
- **Previous Marks Coefficient ($w_2$):** `+0.4391` (Strongest baseline anchor for exam readiness)
|
| 181 |
+
- **Attendance Coefficient ($w_3$):** `+0.2298` (Consistent attendance improves scores)
|
| 182 |
+
- **Assignment Score Coefficient ($w_4$):** `+1.4340` (Completion of lab work positively boosts final outcome)
|
| 183 |
+
- **Intercept ($b$):** `+2.8309`
|
| 184 |
+
|
| 185 |
+
---
|
| 186 |
+
|
| 187 |
+
## 7. COMPLETE SOURCE CODE
|
| 188 |
+
|
| 189 |
+
Below is the complete, self-contained Python source code [`main.py`](file:///c:/Users/anshg/OneDrive/Desktop/ca2%20mdb/main.py):
|
| 190 |
|
|
|
|
|
|
|
|
|
|
| 191 |
```python
|
| 192 |
+
"""
|
| 193 |
+
Student Performance Prediction - Machine Learning with MongoDB & Python
|
| 194 |
+
Author: Ansh Gupta (techbire)
|
| 195 |
+
Database: MongoDB (Localhost: 27017)
|
| 196 |
+
Dataset: 1,000 Student Records
|
| 197 |
+
"""
|
| 198 |
+
|
| 199 |
+
import numpy as np
|
| 200 |
+
import pandas as pd
|
| 201 |
+
from pymongo import MongoClient
|
| 202 |
+
from sklearn.model_selection import train_test_split
|
| 203 |
+
from sklearn.linear_model import LinearRegression
|
| 204 |
+
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
|
| 205 |
+
import matplotlib.pyplot as plt
|
| 206 |
+
|
| 207 |
+
# ==========================================
|
| 208 |
+
# STEP 1: GENERATE REALISTIC DATASET (1,000 RECORDS)
|
| 209 |
+
# ==========================================
|
| 210 |
+
def generate_dataset(num_records=1000):
|
| 211 |
+
print("\n" + "="*50)
|
| 212 |
+
print("STEP 1: GENERATING 1,000 REALISTIC STUDENT RECORDS")
|
| 213 |
+
print("="*50)
|
| 214 |
+
|
| 215 |
+
np.random.seed(42)
|
| 216 |
+
student_ids = [f"STU{1001 + i}" for i in range(num_records)]
|
| 217 |
+
study_hours = np.round(np.random.uniform(1.0, 9.5, num_records), 1)
|
| 218 |
+
attendance = np.round(np.random.uniform(60.0, 99.0, num_records), 1)
|
| 219 |
+
previous_marks = np.round(np.random.uniform(40.0, 95.0, num_records), 1)
|
| 220 |
+
assignments_score = np.random.randint(4, 11, num_records)
|
| 221 |
+
|
| 222 |
+
noise = np.random.normal(0, 2.5, num_records)
|
| 223 |
+
final_marks = (
|
| 224 |
+
0.45 * previous_marks +
|
| 225 |
+
3.5 * study_hours +
|
| 226 |
+
0.25 * attendance +
|
| 227 |
+
1.5 * assignments_score +
|
| 228 |
+
noise
|
| 229 |
+
)
|
| 230 |
+
final_marks = np.round(np.clip(final_marks, 30.0, 100.0), 1)
|
| 231 |
+
|
| 232 |
+
df = pd.DataFrame({
|
| 233 |
+
"student_id": student_ids,
|
| 234 |
+
"study_hours": study_hours,
|
| 235 |
+
"attendance": attendance,
|
| 236 |
+
"previous_marks": previous_marks,
|
| 237 |
+
"assignments_score": assignments_score,
|
| 238 |
+
"final_marks": final_marks
|
| 239 |
+
})
|
| 240 |
+
print(f"-> Successfully generated {len(df)} student records.")
|
| 241 |
+
return df
|
| 242 |
+
|
| 243 |
+
# ==========================================
|
| 244 |
+
# STEP 2: STORE DATASET IN MONGODB
|
| 245 |
+
# ==========================================
|
| 246 |
+
def save_to_mongodb(df, db_name="student_performance_db", collection_name="students"):
|
| 247 |
+
print("\n" + "="*50)
|
| 248 |
+
print("STEP 2: CONNECTING TO MONGODB & INSERTING RECORDS")
|
| 249 |
+
print("="*50)
|
| 250 |
+
|
| 251 |
+
client = MongoClient("mongodb://localhost:27017/")
|
| 252 |
+
db = client[db_name]
|
| 253 |
+
collection = db[collection_name]
|
| 254 |
+
collection.delete_many({})
|
| 255 |
+
|
| 256 |
+
records = df.to_dict(orient="records")
|
| 257 |
+
result = collection.insert_many(records)
|
| 258 |
+
print(f"-> Connected to Database: '{db_name}'")
|
| 259 |
+
print(f"-> Connected to Collection: '{collection_name}'")
|
| 260 |
+
print(f"-> Successfully inserted {len(result.inserted_ids)} records into MongoDB!")
|
| 261 |
+
client.close()
|
| 262 |
+
|
| 263 |
+
# ==========================================
|
| 264 |
+
# STEP 3: FETCH DATASET FROM MONGODB USING PYMONGO
|
| 265 |
+
# ==========================================
|
| 266 |
+
def fetch_from_mongodb(db_name="student_performance_db", collection_name="students"):
|
| 267 |
+
print("\n" + "="*50)
|
| 268 |
+
print("STEP 3: FETCHING DATA FROM MONGODB USING PYMONGO")
|
| 269 |
+
print("="*50)
|
| 270 |
+
|
| 271 |
+
client = MongoClient("mongodb://localhost:27017/")
|
| 272 |
+
db = client[db_name]
|
| 273 |
+
collection = db[collection_name]
|
| 274 |
+
|
| 275 |
+
cursor = collection.find()
|
| 276 |
+
df = pd.DataFrame(list(cursor))
|
| 277 |
+
print(f"-> Successfully fetched {len(df)} records from MongoDB.")
|
| 278 |
+
|
| 279 |
+
print("\n--- 5 Sample Records Retreived from MongoDB ---")
|
| 280 |
+
print(df.head(5).to_string(index=False))
|
| 281 |
+
client.close()
|
| 282 |
+
return df
|
| 283 |
+
|
| 284 |
+
# ==========================================
|
| 285 |
+
# STEP 4: DATA PREPROCESSING
|
| 286 |
+
# ==========================================
|
| 287 |
+
def preprocess_data(df):
|
| 288 |
+
print("\n" + "="*50)
|
| 289 |
+
print("STEP 4: DATA PREPROCESSING")
|
| 290 |
+
print("="*50)
|
| 291 |
+
|
| 292 |
+
cols_to_drop = [col for col in ['_id', 'student_id'] if col in df.columns]
|
| 293 |
+
data = df.drop(columns=cols_to_drop)
|
| 294 |
+
|
| 295 |
+
X = data.drop(columns=['final_marks'])
|
| 296 |
+
y = data['final_marks']
|
| 297 |
+
|
| 298 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 299 |
+
X, y, test_size=0.20, random_state=42
|
| 300 |
+
)
|
| 301 |
+
print(f"-> Training dataset size (80%): {X_train.shape[0]} records")
|
| 302 |
+
print(f"-> Testing dataset size (20%): {X_test.shape[0]} records")
|
| 303 |
+
return X_train, X_test, y_train, y_test, X.columns
|
| 304 |
+
|
| 305 |
+
# ==========================================
|
| 306 |
+
# STEP 5: TRAIN & EVALUATE MODEL
|
| 307 |
+
# ==========================================
|
| 308 |
+
def train_and_evaluate(X_train, X_test, y_train, y_test, feature_names):
|
| 309 |
+
print("\n" + "="*50)
|
| 310 |
+
print("STEP 5: TRAINING MACHINE LEARNING MODEL (LINEAR REGRESSION)")
|
| 311 |
+
print("="*50)
|
| 312 |
+
|
| 313 |
+
model = LinearRegression()
|
| 314 |
+
model.fit(X_train, y_train)
|
| 315 |
+
print("-> Model training complete!")
|
| 316 |
+
|
| 317 |
+
y_pred = model.predict(X_test)
|
| 318 |
+
|
| 319 |
+
r2 = r2_score(y_test, y_pred)
|
| 320 |
+
mae = mean_absolute_error(y_test, y_pred)
|
| 321 |
+
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
|
| 322 |
+
|
| 323 |
+
print(f"1. R-Squared (R2) Score : {r2:.4f} ({r2*100:.2f}% variance explained)")
|
| 324 |
+
print(f"2. Mean Absolute Error : {mae:.2f} marks")
|
| 325 |
+
print(f"3. Root Mean Sq. Error : {rmse:.2f} marks")
|
| 326 |
+
|
| 327 |
+
return model
|
| 328 |
+
|
| 329 |
+
if __name__ == "__main__":
|
| 330 |
+
raw_df = generate_dataset(num_records=1000)
|
| 331 |
+
raw_df.to_csv("student_data.csv", index=False)
|
| 332 |
+
save_to_mongodb(raw_df)
|
| 333 |
+
df_from_db = fetch_from_mongodb()
|
| 334 |
+
X_train, X_test, y_train, y_test, features = preprocess_data(df_from_db)
|
| 335 |
+
model = train_and_evaluate(X_train, X_test, y_train, y_test, features)
|
| 336 |
```
|
| 337 |
|
| 338 |
+
---
|
|
|
|
| 339 |
|
| 340 |
+
## 8. SCREENSHOT OF SOURCE CODE
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
+
Below is the visual capture of the Python source code implementation in the development environment:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
+
<div align="center">
|
| 345 |
|
| 346 |
+

|
| 347 |
|
| 348 |
+
*Figure 8.1: Syntax-highlighted source code implementation of the MongoDB + ML pipeline.*
|
|
|
|
| 349 |
|
| 350 |
+
</div>
|
|
|
|
| 351 |
|
| 352 |
+
---
|
| 353 |
|
| 354 |
+
## 9. OUTPUT SCREENSHOTS & EVALUATION RESULTS
|
| 355 |
|
| 356 |
+
### 9.1 Terminal Execution Screenshot
|
| 357 |
+
Below is the execution output in the terminal showing database connection, sample document retrieval, evaluation scores, and live prediction:
|
|
|
|
| 358 |
|
| 359 |
+
<div align="center">
|
|
|
|
|
|
|
| 360 |
|
| 361 |
+

|
| 362 |
+
|
| 363 |
+
*Figure 9.1: Terminal execution showing PyMongo retrieval, R² score (96.10%), and test predictions.*
|
|
|
|
| 364 |
|
| 365 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
|
| 367 |
---
|
| 368 |
|
| 369 |
+
### 9.2 Graphical Evaluation Plots
|
| 370 |
+
The generated visualization illustrates actual marks compared with predicted marks across the test partition:
|
| 371 |
|
| 372 |
+
<div align="center">
|
| 373 |
|
| 374 |
+

|
| 375 |
|
| 376 |
+
*Figure 9.2: Left: Scatter plot showing regression fit along line y=x. Right: Bar chart comparison for 10 individual students.*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
+
</div>
|
|
|
|
| 379 |
|
| 380 |
+
---
|
| 381 |
+
|
| 382 |
+
### 9.3 Detailed Evaluation Metrics
|
| 383 |
|
| 384 |
+
| Metric | Score | Interpretation |
|
| 385 |
+
| :--- | :--- | :--- |
|
| 386 |
+
| **$R^2$ Score (Coefficient of Determination)** | **$0.9610$ ($96.10\%$)** | High goodness-of-fit: 96.1% of variance in exam marks is explained by the features. |
|
| 387 |
+
| **Mean Absolute Error (MAE)** | **$2.04$ marks** | On average, predictions differ from actual marks by only $\approx 2$ marks out of 100. |
|
| 388 |
+
| **Root Mean Squared Error (RMSE)** | **$2.53$ marks** | Demonstrates consistent predictions with minimal outlier error penalty. |
|
| 389 |
+
|
| 390 |
+
---
|
| 391 |
+
|
| 392 |
+
### 9.4 Actual vs. Predicted Results (First 10 Test Students)
|
| 393 |
+
|
| 394 |
+
| Student Index | Actual Final Marks | Predicted Final Marks | Error (Difference) |
|
| 395 |
| :---: | :---: | :---: | :---: |
|
| 396 |
| Student 1 | 66.7 | 66.1 | 0.6 |
|
| 397 |
| Student 2 | 86.9 | 89.4 | 2.5 |
|
|
|
|
| 404 |
| Student 9 | 65.3 | 63.4 | 1.9 |
|
| 405 |
| Student 10 | 69.4 | 72.5 | 3.1 |
|
| 406 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
---
|
| 408 |
|
| 409 |
+
## 10. CONCLUSION & FUTURE SCOPE
|
| 410 |
|
| 411 |
+
### Key Conclusions:
|
| 412 |
+
1. **Seamless NoSQL Integration:** Successfully demonstrated how MongoDB replaces traditional static CSV storage with scalable document persistence.
|
| 413 |
+
2. **High Predictive Performance:** The Linear Regression model achieved a **96.10% $R^2$ score** and an average error margin of only **2.04 marks**.
|
| 414 |
+
3. **Actionable Educational Insight:** Daily study hours ($+3.47$) and mid-term marks ($+0.44$) exhibited the highest correlation with final examination success.
|
|
|
|
| 415 |
|
| 416 |
+
### Future Scope:
|
| 417 |
+
- **Web Interface:** Integrating a lightweight Flask or FastAPI dashboard to allow instructors to enter student data via a web UI.
|
| 418 |
+
- **Multiclass Classification:** Categorizing students into performance tiers (Distinction, First Division, At-Risk).
|
| 419 |
+
- **Automated Alerts:** Triggering automated notification emails to academic advisors when an at-risk score ($<50$) is forecasted.
|