Logistic Regression
Logistic Regression
Logistic regression is the standard model for binary classification. Instead of predicting a continuous value, it predicts the probability that an example belongs to class 1.
The Sigmoid Function
The sigmoid (logistic) function squashes any real number into the interval :
Notation note: In ML, conventionally denotes the sigmoid activation function. In statistics and finance, the same symbol represents standard deviation or volatility — an entirely different quantity.
Key properties:
- as
- as
Numerical Stability
The naive formula 1 / (1 + exp(-z)) overflows when z is a large negative number (e.g., -1000), because exp(1000) exceeds floating-point range.
The fix: when , use the formula directly. When , rewrite as:
This way the exponent is always non-positive, so exp() never overflows:
def sigmoid(z):
if z >= 0:
return 1 / (1 + math.exp(-z))
else:
ez = math.exp(z)
return ez / (1 + ez)
Logistic Prediction
Binary Cross-Entropy Loss
For binary classification with labels , we minimise:
Use inside the logarithms to avoid .
Your Task
Implement:
sigmoid(z)— the logistic functionlogistic_predict(x, w, b)— sigmoid of the linear combinationbinary_cross_entropy(y_pred, y_true)— average cross-entropy loss