← Back to Portfolio

Linear Regression from Scratch

Click to add points, then train the model using gradient descent

-
MSE
-
Weight (slope)
-
Bias (intercept)
-
Iterations

The Algorithm

class LinearRegression:
    def fit(self, X, y):
        # Initialize weights
        self.weight = 0
        self.bias = 0
        
        for _ in range(self.n_iters):
            # Predict
            y_pred = X * self.weight + self.bias
            
            # Calculate gradients
            dw = (1/n) * sum(X * (y_pred - y))
            db = (1/n) * sum(y_pred - y)
            
            # Update weights
            self.weight -= learning_rate * dw
            self.bias -= learning_rate * db