Square Feet (x)Price (y)

Predicting the Future

Linear Regression is the "Hello World" of Machine Learning. It's a statistical method to model the relationship between a dependent variable (like house price) and one or more independent variables (like size).
Continue

1. The Data

Imagine we have data on House Prices. • X-axis: Size (sq ft) • Y-axis: Price ($) We can see a pattern: bigger houses generally cost more.
Continue

2. The Line of Best Fit

We want to draw a straight line through this data that best represents the trend. The equation for a line is: y = mx + b (m = slope, b = intercept)
Continue

3. Measuring Error (Residuals)

How good is our line? We measure the vertical distance from each data point to the line. These are called 'Residuals' or 'Errors'. Our goal: Minimize the sum of these squared errors.
Continue

4. Finding the Best Fit

Using an algorithm like 'Ordinary Least Squares' or 'Gradient Descent', we adjust the Slope (m) and Intercept (b) until the Error is as small as possible.
Continue

5. Making Predictions

Now that we have a trained model, we can predict the price for ANY house size! Drag the slider below to use the model.
Continue

The Math

In Machine Learning terms, we write the linear equation as:

model.py
# Simple Linear Regression

# y = mx + b
# y_pred = weight * input + bias

def predict(x, weight, bias):
    return weight * x + bias
Prediction Function

Cost Function (MSE)

To find the best line, we minimize the Mean Squared Error (MSE).

cost.py
def compute_cost(X, y, weight, bias):
    n = len(y)
    total_error = 0
    
    for i in range(n):
        y_pred = weight * X[i] + bias
        error = (y_pred - y[i]) ** 2
        total_error += error
        
    return total_error / n
Calculating MSE

Gradient Descent

We use the derivatives of the cost function to update our weight and bias iteratively.

optimizer.py
learning_rate = 0.001

# Update rules
weight_gradient = (-2/n) * sum(X * (y - y_pred))
bias_gradient = (-2/n) * sum(y - y_pred)

weight = weight - (learning_rate * weight_gradient)
bias = bias - (learning_rate * bias_gradient)
Updating Parameters
AlgoAnimator: Interactive Data Structures