Page 08 of 14 · Core techniques
Classification: from a score to a yes or no
Regression predicts a number. Classification predicts a category: spam or not, malicious or benign, compromised or safe. Logistic regression, despite its name, is the classic classification model, and it is built from a linear model plus one S-shaped function.
The sigmoid squashes any number into a probability
A linear model can output any value from minus infinity to plus infinity. The sigmoid function maps that score, called z, into the range 0 to 1, which we can read as a probability.
Draw the decision boundary
Each point is a login. The model combines two features into a score, z = w1x1 + w2x2 + b, then applies the sigmoid. Wherever the probability is exactly 0.5 lies the decision boundary, a straight line. Adjust the weights by hand, then let training find them.
The loss for classification: log loss
Mean squared error works poorly for probabilities. Classification uses log loss (also called binary cross-entropy), which punishes confident wrong answers very harshly.
If the true label is y = 1 and the model says p = 0.99, the loss is about 0.01. If it says p = 0.01, the loss is about 4.6. Averaged over every example, this is the number gradient descent minimizes.
More than two classes
To sort malware into five families instead of two, logistic regression generalizes with the softmax function: one score per class, turned into probabilities that add up to 1. The same idea appears again at the output of neural networks.
In Python
from sklearn.linear_model import LogisticRegression model = LogisticRegression().fit(X_train, y_train) print(model.coef_, model.intercept_) # w1, w2 and b model.predict_proba(X_test)[:, 1] # probability of class 1 (compromised) model.predict(X_test) # 0 or 1, using a 0.5 threshold
- Logistic regression is a linear score passed through the sigmoid, giving a probability between 0 and 1.
- The decision boundary is where the probability is 0.5; for logistic regression it is a straight line (or flat plane).
- Training minimizes log loss. Page 09 shows why the 0.5 threshold is not always the right one.