Classification with Logistic Regression
Logistic regression is for categories, not lines. Despite the name, it predicts a probability between 0 and 1 and then picks a class. It is the baseline for spam detection, churn prediction, and medical screening.
You will see it output probabilities. A threshold, usually 0.5, turns that into a label. Moving the threshold trades false positives for false negatives, which matters more than accuracy in many products.
The confusion matrix is how you read its performance. Rows are true labels, columns are predicted labels. From it you get precision and recall.
Probability to class
The model outputs a probability. Use predict for labels and predict_proba for scores. Calibrate the threshold based on cost of errors.
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
proba = model.predict_proba(X_test)[:,1]
labels = (proba > 0.5).astype(int)
Confusion matrix
Four cells: true positive, true negative, false positive, false negative. From these you compute precision and recall.
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_test, labels))
Syntax
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
model.predict([[0.5, 1.2]])
Accuracy lies on imbalanced data
If 95% of emails are ham, a model that always predicts ham gets 95% accuracy but is useless. Use precision, recall, and F1. This Reddit r/MachineLearning top post explains why accuracy is a vanity metric.
from sklearn.metrics import classification_report
print(classification_report(y_test, labels))
Good to know
- Logistic regression predicts categories via probabilities.
- Threshold 0.5 is default but not always best.
- Read confusion matrix to understand errors.