Linear Regression from Ground Zero
Train a mathematical model without polyfit, scikit-learn or any ready-made regression function—then use its prediction to classify a student's result.
We are implementing model training ourselves
Each observation is a coordinate (x, y). Here, x is a student's marks and y is a numerical result code.
| Result code | Meaning |
|---|---|
| 1 | 1st Division |
| 2 | 2nd Division |
| 3 | 3rd Division |
| 4 | Fail |
The model: a straight line
The observations do not lie on one perfect straight line. Linear regression finds the line that represents their overall trend. The model has only two learned values: the slope m and intercept c.
m = [nΣxy − (Σx)(Σy)] ÷ [nΣx² − (Σx)²]
c = [Σy − mΣx] ÷ n
For our training data, the calculations produce:
From regression to classification
1. Regression predicts a number
After the user enters marks, the program substitutes them into y = mx + c. The answer is normally a decimal, such as 2.18.
2. Classification names the group
The decimal is rounded with int(y + 0.5) in Python or Math.floor(y + 0.5) in JavaScript. Code 2 then becomes “2nd Division.”
The model therefore performs two connected tasks: first it predicts a continuous value, and then the program converts that prediction into a class.
Run the Python version
This version calculates every sum, develops the regression equation, accepts marks, classifies the prediction, and draws the regression line and scatter points with Matplotlib.
Open Python program in a new tabRun the HTML and JavaScript version
The browser version uses no external graphing library. JavaScript trains the model, while the HTML Canvas API draws the straight line, original coordinates and predicted point.
Open browser version in a new tabWhat the graph teaches us
- Blue points are the original training examples.
- The red line is the learned model, y = mx + c.
- The green point shows the new prediction.
- The negative slope occurs because higher marks correspond to smaller—and better—result codes.
A straight line approximates the overall pattern; it does not necessarily pass through every training point. The distance between an observed point and the line represents prediction error.
Try these experiments
- Enter several marks and observe how the predicted point moves.
- Change one training result and see how the learned line changes.
- Add another coordinate pair to both arrays.
- Print the values of Σx, Σy, Σxy and Σx².
- Compare the same input in the Python and JavaScript programs.
Study AI, Machine Learning and Python with Champak Roy of Varanasi
Learn the mathematics, understand the code, run the experiment, and build the model yourself.
Enquire on WhatsApp
0 Comments
Please comment