Python Engineer

Free Python and Machine Learning Tutorials

Become A Patron and get exclusive content! Get access to ML From Scratch notebooks, join a private Slack channel, get priority response, and more! I really appreciate the support!

back to course overview

Linear and Logistic Regression Refactoring- ML From Scratch 04

22 Sep 2019

In this Machine Learning from Scratch Tutorial, we are going to refactor the code from the previous two videos. We will implement Linear and Logistic Regression in only 60 lines of Python, with the help of a Base Regression class.

All algorithms from this course can be found on GitHub together with example tests.

IMPORTANT: In the video I forgot to call the `_approximation() in the Base class at the end. Please check the correct full code below or in the repo.

Implementation

import numpy as np class BaseRegression: def __init__(self, learning_rate=0.001, n_iters=1000): self.lr = learning_rate self.n_iters = n_iters self.weights = None self.bias = None def fit(self, X, y): n_samples, n_features = X.shape # init parameters self.weights = np.zeros(n_features) self.bias = 0 # gradient descent for _ in range(self.n_iters): y_predicted = self._approximation(X, self.weights, self.bias) # compute gradients dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y)) db = (1 / n_samples) * np.sum(y_predicted - y) # update parameters self.weights -= self.lr * dw self.bias -= self.lr * db def predict(self, X): return self._predict(X, self.weights, self.bias) def _predict(self, X, w, b): raise NotImplementedError() def _approximation(self, X, w, b): raise NotImplementedError() class LinearRegression(BaseRegression): def _approximation(self, X, w, b): return np.dot(X, w) + b def _predict(self, X, w, b): return np.dot(X, w) + b class LogisticRegression(BaseRegression): def _approximation(self, X, w, b): linear_model = np.dot(X, w) + b return self._sigmoid(linear_model) def _predict(self, X, w, b): linear_model = np.dot(X, w) + b y_predicted = self._sigmoid(linear_model) y_predicted_cls = [1 if i > 0.5 else 0 for i in y_predicted] return np.array(y_predicted_cls) def _sigmoid(self, x): return 1 / (np.exp(-x) + 1)