Let’s suppose that we want to learn logistic regression from the ground up. Where shall we get started?

Logistic (Logit) Regression

Python Implementation

import numpy as np
def logistic(z):
	return 1 / (1 + np.exp(-z))
def predict(X, w):
	return logistic(X @ w)
def loss(X, y, w):
	p = predict(X, w)
	return np.mean((y - p) ** 2)
def gradient(X, y, w):
	p = predict(X, w)
	return -2 * (X.T @ ((y - p) * p * (1 - p))) / len(X)
def fit(X, y, alpha=0.1, iters=1000):
	w = np.zeros(X.shape[1])
	for _ in range(iters):
		w -= alpha * gradient(X, y, w)
	return w