bhylu.com

Advertisements

Logistic Regression

Logistic regression is commonly used for classification despite its name. It models a class probability through a linear score and a link function.

Understand the idea

predict chooses class labels, while predict_proba reports estimated class probabilities in classes_ order. Those numbers require calibration and validation before being treated as reliable confidence. The tiny example only separates two synthetic clusters. Production decisions also need a deliberate threshold and a review of false-positive and false-negative costs.

Where to try it

Run locally with scikit-learn and NumPy installed in your virtual environment (py -m pip install scikit-learn numpy). These examples are executed during course review, but the online runner does not currently preload scikit-learn. The small data is for learning the API, not evidence of production model quality.

A worked example

Python

from sklearn.linear_model import LogisticRegression
model = LogisticRegression().fit([[1], [2], [8], [9]], [0, 0, 1, 1])
print(model.predict([[1], [9]]).tolist())
print(model.classes_.tolist())

Read the result

Both lists are [0, 1]. The class ordering explains the columns of a probability result.

Try one change

Inspect predict_proba for a middle value and explain how a threshold affects the final label.

For package options, see the scikit-learn documentation.