Linear Regression
medium· Machine Learningruns: 0Implement linear regression trained with batch gradient descent. Fit weights and bias to minimize mean squared error, then predict targets for new inputs.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 642
import numpy as np
class LinearRegression:
def __init__(self, lr: float = 0.01, epochs: int = 1000):
self.lr = lr
self.epochs = epochs
self.weights = None
self.bias = 0.0
def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
for _ in range(self.epochs):
y_pred = X @ self.weights + self.bias
error = y_pred - y
self.weights -= self.lr * (2 / n_samples) * (X.T @ error)
self.bias -= self.lr * (2 / n_samples) * error.sum()
def predict(self, X):
return X @ self.weights + self.bias
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.