A simple implementation of linear regression from scratch using only:
- NumPy for numerical computation.
- Matplotlib for visualization.
The project does not use pandas, scikit-learn, or any other machine-learning library. The model learns the relationship between years of experience and salary using gradient descent.
The model predicts salary using the following equation:
where:
- (x) is the number of years of experience.
- (\hat{y}) is the predicted salary.
- (w) is the learned weight or slope.
- (b) is the learned bias or intercept.
The parameters are learned by minimizing the mean squared error using gradient descent.
The project expects a file named train.csv with the following structure:
YearsExperience,Salary
1.1,39343.00
1.3,46205.00
1.5,37731.00
2.0,43525.00
2.2,39891.00The dataset contains two columns:
YearsExperience: The input feature.Salary: The target value to predict.
- Python 3.8 or newer.
- NumPy.
- Matplotlib.
Install the dependencies with:
python -m pip install numpy matplotlibThe model uses the following cost function:
The gradients are:
The parameters are updated after every iteration:
After training, predictions can be generated using:
experience = 7.5
predicted_salary = final_w * experience + final_b
print(f"Predicted salary: {predicted_salary:.2f}")The prediction follows:
- The feature name must be exactly
YearsExperience. - The target column must be exactly
Salary. - The learning rate is small because salary values are relatively large.
- A learning rate that is too large can make the cost diverge.
- A learning rate that is too small can make training slow.
- This implementation uses batch gradient descent.
- This project is intended for learning and does not include regularization, train/test splitting, or production-level data validation.
This project demonstrates:
- Reading structured data without pandas.
- Representing a linear model mathematically.
- Implementing batch gradient descent.
- Using NumPy arrays for numerical computation.
- Visualizing model predictions with Matplotlib.
This project is available for educational and personal use.