Scikit-learn's Universal API: Fit, Predict, Transform
The scikit-learn Estimator API is a universal contract: `.fit()` to learn, `.predict()` to guess, and `.transform()` to change data. It's used for everything from `StandardScaler` to `RandomForestClassifier`.
WHY IT EXISTS To provide a simple, consistent, and composable interface for a vast library of machine learning algorithms. Without a standard API, every algorithm would have its own unique method names and usage patterns, making it impossible to build reusable workflows like Pipelines.
THE MENTAL MODEL Think of the Estimator API as a universal remote for machine learning models. Every object, whether it's a data cleaner (a Transformer) or a predictive model (a Predictor), responds to the same basic commands. You instantiate an object, .fit() it to your training data, and then use it to .predict() outcomes or .transform() new data.
HOW IT WORKS All estimator objects share a common interface based on a few key methods. There are two main types of objects:
Predictors are models used for classification or regression. They have two main methods: first, .fit(X, y) to learn parameters from features X and targets y; and second, .predict(X) to generate predictions for new data.
Transformers are used for data preprocessing, like scaling or encoding features. They have a .fit(X) method to learn the transformation parameters (e.g., the mean and standard deviation for scaling) and a .transform(X) method to apply the transformation. Many also have a convenient .fit_transform(X) method that does both steps at once.
WHEN TO USE IT This API is the foundation of the entire scikit-learn library. Its power shines when you use a Pipeline to chain multiple steps together. A Pipeline bundles transformers and a final predictor into a single object that behaves just like any other estimator, with its own .fit() and .predict() methods. This simplifies your code and, crucially, prevents data leakage by ensuring that transformers are only fitted on the training data.
WHEN NOT TO USE IT The API itself is not optional within scikit-learn. The mistake is using the wrong method for the job. You don't call .transform() on a final classification model, and you don't call .predict() on a data scaler. Each object type has its designated methods.
ONE CANONICAL EXAMPLE A common workflow is to scale features and then train a logistic regression model. A Pipeline makes this robust and simple. from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression pipe = make_pipeline(StandardScaler(), LogisticRegression()) pipe.fit(X_train, y_train) predictions = pipe.predict(X_test) This single pipe object handles fitting the scaler on training data, transforming it, and then training the model. When .predict() is called, it automatically transforms the test data using the scaler that was fitted only on the training data.
Read the original → scikit-learn.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.