Machine Learning Mini Project
1) Linear Regression: Predict student exam scores based on study hours
and attendance
Concept of Linear Regression
Linear Regression is a supervised algorithm used to predict continuous values. It fits a
straight line (or plane in multiple dimensions) to minimize the difference between actual
and predicted values.
Equation:
y = b0 + b1*x1 + b2*x2 + ... + bn*xn
Here:
y = Exam score (target)
x1 = Study hours
x2 = Attendance
Dataset Generation
- Study Hours: Random integers (1–10)
- Attendance: Random integers (50–100)
- Scores: Linear combination (7 × hours + 0.5 × attendance) + Gaussian noise
Code Snippet
import numpy as np
import [Link] as plt
from sklearn.linear_model import LinearRegression
from mpl_toolkits.mplot3d import Axes3D
# Step 1: Dataset
[Link](42)
study_hours = [Link](1, 10, 50)
attendance = [Link](50, 101, 50)
scores = study_hours * 7 + attendance * 0.5 + [Link](0, 5, 50)
X = np.column_stack((study_hours, attendance))
y = scores
# Step 2: Train Linear Regression model
model = LinearRegression()
[Link](X, y)
# Step 3: Predictions
y_pred = [Link](X)
# Step 4: Results
print('Coefficients:', model.coef_)
print('Intercept:', model.intercept_)
print('R² Score:', [Link](X, y))
# Step 5: Visualization (3D plot)
fig = [Link](figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
[Link](study_hours, attendance, scores, c='b', label='Actual Scores')
[Link](study_hours, attendance, y_pred, c='r', label='Predicted Scores')
# Regression plane
x_surf, y_surf = [Link](
[Link](study_hours.min(), study_hours.max(), 10),
[Link]([Link](), [Link](), 10)
)
z_surf = [Link](np.column_stack((x_surf.ravel(),
y_surf.ravel()))).reshape(x_surf.shape)
ax.plot_surface(x_surf, y_surf, z_surf, color='green', alpha=0.5, edgecolor='k')
ax.set_xlabel('Study Hours')
ax.set_ylabel('Attendance')
ax.set_zlabel('Exam Score')
ax.set_title('Linear Regression: Exam Scores Prediction (3D)')
[Link]()
[Link]()
Results
- Coefficients: Contribution of study hours & attendance
- Intercept: Baseline exam score
- R² Score: Goodness of fit (closer to 1 = better)
Result Plots
Insert 3D plot of actual vs predicted scores with regression plane here
Observations
- The regression plane fits the dataset well.
- Both study hours and attendance positively affect exam scores.
- Random noise introduces small prediction errors, but overall fit is strong.