Learning with fit() and predict()

A small, repeatable training interface

With a clean feature table in hand, it's time to actually train something. Scikit-learn is the standard Python library for classical machine learning - a family that includes logistic regression, decision trees, and nearest-neighbor methods. "Classical" here just means: not a large language model, not a deep neural network with millions of learned weights. These models are small, fast, and can train on a few thousand rows in well under a second on an ordinary laptop CPU, which is exactly why this track starts with them before touching anything resembling an LLM.

Almost every scikit-learn model shares the same two-step interface, which is worth memorizing because it never changes no matter which algorithm you pick:

from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
model.fit(X_train, y_train)      # learn from features + known answers
predictions = model.predict(X_test)  # apply what it learned to new rows
print(predictions[:5])
[0 0 1 0 1]

model.fit(X_train, y_train) is the studying step: show the model past features (X_train) next to their real outcomes (y_train) so it can adjust its internal numbers. model.predict(X_test) is the exam step: given only features it has never labeled before, produce a guess. Keeping this same two-method shape across every algorithm is what makes it easy to swap one model for another and compare them fairly.

Logistic regression is a classification algorithm despite the confusing name - it estimates the probability that a row belongs to a given class (e.g. "73% likely to cancel") and then picks the more likely class. It's usually the very first model worth trying: simple, fast, and easy to explain to a non-technical stakeholder.

Before trusting any real model, always compare it against a baseline - the simplest possible thing that could pass for a prediction. Scikit-learn's DummyClassifier deliberately does something naive, like always predicting whichever class is most common in the training data:

from sklearn.dummy import DummyClassifier

baseline = DummyClassifier(strategy="most_frequent")
baseline.fit(X_train, y_train)
baseline.score(X_test, y_test)
0.82

If 82% of customers in this dataset never cancel, a baseline that always predicts "will not cancel" gets 82% accuracy for free, without learning anything useful. A real model only earns its keep if it clearly beats that number - which is why the next module spends real time on metrics beyond plain accuracy.

Features measured on very different scales (age in single digits, income in tens of thousands) can confuse some algorithms. StandardScaler rescales each numeric column to have a similar spread. A scikit-learn Pipeline chains preprocessing steps and the model together into one object, so both are fitted only on training data and then applied identically - never re-fit - when scoring the test set:

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

pipe = Pipeline([
    ("scale", StandardScaler()),
    ("model", LogisticRegression()),
])
pipe.fit(X_train, y_train)
Analogy: fit() is studying worked examples with the answer key in hand. predict() is sitting the closed-book exam. Reading the answer sheet during study, or grading the exam using the same questions you studied from, proves nothing about how the model handles a genuinely new case - which is exactly what the next section is about.