Key Ideas
1The Big Idea. scikit-learn's entire API is built around one consistent pattern. Whether you're using a linear regression, a random forest, a scaler, or a text vectorizer — they all ...
2Estimators — Objects That Learn. An estimator is any object with a fit() method. During fit(), it looks at your training data and learns something — the weights of a regression, the split rules of a t...
3Transformers — Objects That Change Data. A transformer has two methods: fit() and transform(). The fit() step learns statistics from the training data. The transform() step applies those statistics to any dat...
4Pipelines — Chaining It All Together. A Pipeline chains one or more transformers followed by a final estimator. When you call pipeline.fit(X, y), it:
5The Three Methods You Must Know
MethodWhat it doesWhen to callOn what data
fit(X, y)
Learn parameters from data. Returns self.
Once, during training
Training set only
transform(X)
Apply learned transformation to data
After fit(). Call on train AND test.
Any data, using train stats
fit_transform(X)
fit() then transform() in one step
Shortcut for training data only
Training set only
predict(X)
Generate predictions from fitted model
After fit(). Inference time.
Any data
score(X, y)
Evaluate model performance
After fit(). Evaluation.
Test or validation set
What's stored after fit()
sklearn convention: parameters learned during fit() are stored with a trailing underscore. model.coef_, scaler.mean_, pca.components_. Parameters you set before fitting don't have trailing underscores: StandardScaler(with_mean=True). This is how you know what's learned vs configured.
Putting It Together: A Full Working Example. Here is a complete, real-world-style workflow using Pipeline with proper train/test splitting, cross-validation, and scoring: