Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

The Scikit-learn API: `fit`, `predict`, and `transform`

Recommended resources

Hello! Welcome to the fourth lesson in our "Introduction to Data Science" course.

Introduction

In our previous lessons, we assembled a powerful toolkit for data wrangling and exploration. You've mastered manipulating arrays with NumPy, managing tabular data with Pandas, and creating insightful visualizations with Matplotlib. Now, we'll add the final, crucial component to this toolkit: Scikit-learn, the cornerstone of machine learning in Python.

This lesson focuses on the elegant design philosophy that makes Scikit-learn so effective: its unified Application Programming Interface (API). We will dissect the three fundamental methods that govern almost every object in the library:

  • fit(): for learning from data.
  • predict(): for making predictions.
  • transform(): for preprocessing data.

Understanding this API is not just about memorizing commands; it's about grasping a consistent pattern that allows you to seamlessly combine data preprocessing steps and modeling algorithms. This knowledge is the key that unlocks the entire library and will be essential as we move on to our first project.


1. The Scikit-learn Design Philosophy

Scikit-learn is more than just a collection of algorithms. It's a well-designed ecosystem of building blocks for creating, evaluating, and deploying machine learning models. The power of this ecosystem comes from its consistent and simple API.

The creators of Scikit-learn made deliberate design choices to ensure that all objects in the library, whether they are for data scaling or for complex classification, behave in a predictable way. This consistency makes the library both easy to learn and incredibly powerful for composing complex workflows.

To understand these design principles from the source, let's look at the paper that introduced the API.

API design for machine learning software

This paper, written by the developers of Scikit-learn, details the core design principles of the library. Reading this will give you a formal understanding of the 'why' behind the API's structure.

Please read the 'Core API' and 'General principles' sections. Focus on the principles of Consistency, Inspection, and Composition. This will provide the theoretical foundation for the practical methods we're about to cover.

The key idea is that every object is an Estimator. This base object is then extended by two other interfaces: Predictor and Transformer.

Scikit-learn API Core Classes: Estimator, Transformer, and Pipeline
This diagram provides a high-level overview of Scikit-learn's core API. It shows that `Estimator` is the base class, with its fundamental `fit()` method. `Transformer` inherits from `Estimator` and adds `transform()`, while `Pipeline` chains these components together. The `Predictor` interface (not shown as a class here, but as a concept) adds the `predict()` method to an estimator. Source: B. D. Rou, et al. (2019).

2. Estimators vs. Transformers

Within Scikit-learn, objects generally fall into one of two categories, each with its own primary purpose and set of methods.

  1. Transformers: These objects are for data preprocessing. Their job is to change the dataset in some way, such as scaling features, imputing missing values, or reducing dimensionality.
    • Core methods: fit(), transform(), and the convenient fit_transform().
  2. Models (or Predictors): These objects are for making predictions. They learn patterns from the data to predict a target variable.
    • Core methods: fit() and predict().

Let's watch a video that clearly explains this distinction.

Difference Between fit(), transform(), fit_transform() and predict() methods in Scikit-Learn

Krish Naik provides an excellent conceptual breakdown of the difference between transformer objects and model objects in Scikit-learn.

Watch the segment from 01:46 to 04:39. Pay attention to how he categorizes objects like StandardScaler and PCA as transformers, and objects like LinearRegression and DecisionTree as models.

Now let's dive into the methods themselves.

The fit() Method: Learning from Data

The fit(X, y) method is the learning step. It is present in every estimator.

  • When used with a transformer (e.g., StandardScaler), fit() calculates the parameters needed for the transformation. For StandardScaler, this means calculating the mean and standard deviation of the training data X. It does not change the data itself.
  • When used with a model (e.g., LogisticRegression), fit() trains the model on the training data X and target labels y. It learns the internal model parameters (like regression coefficients) needed to make predictions.

The transform() and predict() Methods: Applying Knowledge

After an estimator is fitted, it can be used to perform an action.

  • transform(X): This method, unique to transformers, applies the learned transformation to the data X. For StandardScaler, it would subtract the learned mean and divide by the learned standard deviation.
  • predict(X): This method, unique to models, uses the trained model to make predictions on new data X.

The video below demonstrates this process in action.

Difference Between fit(), transform(), fit_transform() and predict() methods in Scikit-Learn

Let's return to Krish Naik's video, where he demonstrates the fit/transform pattern for a transformer and the fit/predict pattern for a model.

Watch two segments: 04:39 to 09:55: Focus on how fit() on StandardScaler learns the mean/std dev and transform() applies the scaling. 11:53 to 14:38: See how fit() on a model trains it, and predict() uses it to generate outputs for new data.

The fit_transform() Convenience Method

For transformers, it's very common to fit on a dataset and then immediately transform that same dataset. Scikit-learn provides a convenience method fit_transform() that combines these two steps. While you could call fit() then transform() separately, fit_transform() is often more computationally efficient.


3. The Golden Rule: Preventing Data Leakage

Here we arrive at the most critical best practice when using transformers. You must learn the transformation parameters from the training data only.

If you fit your transformer on the entire dataset (including the test set), information from the test set "leaks" into your training process. For example, the mean and standard deviation calculated by StandardScaler would be influenced by the test data. This leads to overly optimistic performance metrics and a model that will perform worse in the real world.

The correct workflow is:

  1. Split your data into a training set and a testing set.
  2. Call fit_transform() on the training data to learn the scaling parameters and apply them.
  3. Call transform() (and only transform()) on the testing data to apply the same scaling that was learned from the training data.

This flowchart perfectly illustrates the correct data flow.

Scikit-learn Fit and Transform Workflow
This flowchart shows the proper procedure for preprocessing data. The transformer is `fit` only on the training data. That same fitted transformer is then used to `transform` both the training and test data, ensuring no information from the test set influences the preprocessing step.

Let's watch a final video clip that emphasizes this crucial point.

Difference Between fit(), transform(), fit_transform() and predict() methods in Scikit-Learn

This is the most important concept in this lesson. Krish Naik explains precisely how to apply transformations correctly to training and test sets to avoid data leakage and overfitting.

Watch carefully from 14:38 to 18:37. This segment summarizes the entire workflow and explains the reasoning behind using fit_transform on the training set but only transform on the test set.


4. Practice and Self-Test

Let's solidify this with a quick thought experiment. Imagine you have a dataset with features [age, salary] that you need to prepare for a model. You've split it into X_train and X_test. You decide to use StandardScaler.

Which of the following is the correct sequence of operations?

Option A:

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.fit_transform(X_test)

Option B:

scaler = StandardScaler()



# Fit on the full dataset to get the 'true' parameters
scaler.fit(np.vstack((X_train, X_test))) 
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)

Option C:

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
Click to reveal the answer

Option C is the correct answer.

  • Option A is incorrect because it calls fit_transform on the test data. This refits the scaler using the test data's mean and standard deviation, which is a form of data leakage.
  • Option B is incorrect because it explicitly fits the scaler on the combined train and test data, which is the definition of data leakage.
  • Option C is correct because it learns the transformation parameters (fit) from the training data only and then applies (transform) that same transformation to both the training and test data. This ensures your model evaluation on the test set is a true reflection of its performance on unseen data.

Conclusion

In this lesson, you've learned the fundamental design pattern that makes Scikit-learn a cohesive and powerful library. By understanding the distinct roles of estimators and transformers, and the correct application of fit, predict, and transform, you are now prepared to build robust machine learning workflows.

Key Takeaways:

  • Scikit-learn's API is built on a consistent interface with three key methods: fit, predict, and transform.
  • Transformers (like StandardScaler) preprocess data. They use fit() to learn transformation parameters and transform() to apply them.
  • Models (like LogisticRegression) make predictions. They use fit() to learn from data and predict() to generate outputs.
  • The Golden Rule: To prevent data leakage, you must fit() your transformers only on the training data. You then use the fitted transformer to transform() both the training and the test data.

This lesson completes our tour of the essential Python data science toolkit. You now have the skills to load, clean, visualize, and preprocess data.

In the next lesson, we will begin our first project. You will integrate all the tools from this module—Pandas, Matplotlib, and Scikit-learn—to conduct a complete exploratory data analysis and prepare a dataset for modeling.