Classification with Logistic Regression
Classification guesses a category instead of a number. Meet logistic regression, which, despite its name, sorts examples into classes by turning a score into a probability. You'll read its probabilities, choose where to draw the line between yes and no, and split data so both classes show up fairly in training and testing. The running example is an inbox's spam filter.
Updated
What Classification Is
- What Classification Is (Apprentice): Classification guesses a category instead of a number, such as spam or not spam, with two classes (binary) or more (multiclass) and often one class you're looking for (the positive class), and a scikit-learn classifier lists its classes in classes_ and gives back one of them for each row from predict.
Logistic Regression
- Logistic Regression (Journeyman): Logistic regression adds up a weighted score for each example, as a linear regression does, then bends it with an S-shaped curve into a probability between 0 and 1, which makes it a classifier despite its name, with C setting how simple scikit-learn keeps it and max_iter how long it may search.
- Probabilities and the 0.5 Line (Journeyman): predict_proba gives each example one probability per class, in the order of classes_, predict picks the class with the highest one, which for two classes is a line at 0.5, and the probability is the model's confidence, not a guarantee, so you can draw the line somewhere else when the job needs it.
Fair Splits for Categories
- Stratified Splits (Journeyman): A random split can leave more or fewer of one class in the test part than in the data, and stratify=y makes train_test_split keep each class's share the same in both parts, which you check with value_counts(normalize=True).
Exercise: Spam or Not
- Exercise: Spam or Not (Journeyman): Write three functions that split Inbox Guard's 1,000 emails into training and test emails with the same share of spam in both, train a LogisticRegression on the training emails, and turn its chances of spam into labels at a threshold, and let pytest check the split, the model and the labels.