All intermediate steps should be transformers and implement fit and transform
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In the world of machine learning and data science, data preprocessing is a crucial step that involves transforming raw data into a suitable format for modeling. This often involves a sequence of steps applied to the data, such as scaling, encoding, and feature extraction. In modern machine learning workflows, a popular methodology for structuring these sequential steps is by using transformers. Transformers are components that implement the fit()
and transform()
methods, providing a standardized interface to preprocess data consistently and reliably.
Understanding Transformers
Definition
A transformer is an object or class that implements two essential methods:
fit(X, y=None): This method is used to learn from the dataXand, in supervised scenarios, a targety. The fit process modifies the internal parameters of the transformer based on the data properties. For instance, for scaling data,fit()would compute the mean and standard deviation from the dataset.transform(X): After fitting the transformer, thetransform()method applies the learned transformation to the dataX. For the scaling transformer,transform()would standardize the dataset using the precomputed mean and standard deviation.
Why Use Transformers?
Transformers help encapsulate the data preprocessing logic in a structured and reusable manner. They ensure that all intermediate steps are consistently applied across different datasets and experiment iterations. By adopting a transformer structure, data scientists and engineers can more seamlessly integrate and experiment with varied preprocessing techniques.
Practical Example
Let's consider a practical example where we use transformers for a simple data preprocessing pipeline:
Problem Statement
Imagine that we have a dataset with numerical and categorical features, and we need to preprocess the data to feed it into a machine learning model.
Steps
- Scaling Numerical Features: Use a transformer to standardize numerical features, ensuring that they have a mean of 0 and a variance of 1.
- Encoding Categorical Features: Use a transformer to encode categorical features into numerical formats, such as one-hot encoding.
- Feature Selection: Use a transformer to select a subset of features based on a specific criterion.
Implementing Transformers
Here's a simplified implementation of these transformers:

