{% extends "layout.html" %} {% block content %}
Understand the fundamentals of Linear Regression, its computational flow, and how it makes predictions.
Linear Regression is a fundamental supervised learning algorithm used for predicting a continuous outcome variable (dependent variable) based on one or more input features (independent variables). It models the relationship between the variables by fitting a linear equation to the observed data.
For a simple linear regression with one input feature, the equation used by our model is:
Predicted Score = (20 x Hours Studied) + 15
The slope of 20 means each hour of studying contributes 20 points to your exam score. For example, if you study one more hour, your predicted score increases by 20 points.
The intercept of 15 represents points earned regardless of study time. This could account for:
The following steps illustrate how our Linear Regression model makes predictions:
Predicted Score = (20 * Input Hours) + 15
The model was trained on the following data points to learn the relationship between 'Hours Studied' and 'Score' using the equation `Score = 20 * Hours + 15`:
| Hours Studied X | Score Y |
|---|---|
| 1 | 35 |
| 2 | 55 |
| 3 | 75 |
| 4 | 95 |
| 5 | 115 |
When a linear regression model is being trained, it doesn't just randomly draw a line. It evaluates how good its current line is by using a Cost Function. The goal of training is to find the line i.e. the specific m and b values that minimizes this cost.
A common cost function for linear regression is the Mean Squared Error MSE. It calculates the average of the squared differences between the actual observed values $y_i$ and the values predicted by the model $\hat{y_i}$.
Mean Squared Error (MSE) Formula:
\[ \text{MSE} = \frac{1}{N} \sum_{i=1}^{N} (y_i - \hat{y}_i)^2 \]
Squaring the differences ensures that all errors are positive and penalizes larger errors more heavily. The model continuously adjusts its m and b to make this MSE value as small as possible.
Gradient Descent is an optimization algorithm used by linear regression and many other machine learning models to find the values of m and b that minimize the cost function like MSE. Imagine the cost function as a landscape with hills and valleys, and the goal is to find the lowest point (the minimum cost).
So, when `model.fit(X, y)` is called, behind the scenes, an optimization algorithm like Gradient Descent is tirelessly working to find the m and b that best fit your data by minimizing the prediction errors.
Slope (m): , Intercept (b):
{% if prediction is not none %} {{ prediction | round(2) }} {% else %} --.-- {% endif %}
This is the score predicted by the linear regression model for the hours you entered.