← Lesson
BitWithBite
AI & Machine Learning · Quick Reference

1.1 Estimators, Transformers & Pipelines Cheat Sheet

AI & Machine Learning
In one line: 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 follow the...

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:

Code Examples

from sklearn.linear_model import LinearRegression import numpy as np # Training data X_train = np.array([[1], [2], [3], [4]]) y_train = np.array([2, 4, 6, 8]) # Step 1: create the estimator (nothing learned yet) model = LinearRegression() # Ste...
from sklearn.preprocessing import StandardScaler import numpy as np X_train = np.array([[10, 200], [20, 400], [30, 600]]) X_test = np.array([[15, 300], [25, 500]]) scaler = StandardSc...
from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split X, y = load_iris(re...