Skip to main content

Streamlining Machine Learning Workflows: The Power of Preprocessing Pipelines

In the world of machine learning, the journey from raw data to a deployed model can be complex and time-consuming. One of the most critical yet often overlooked aspects of this process is data preprocessing. Today, we'll explore how to streamline your ML workflows using preprocessing pipelines, a powerful technique that can save you time, reduce errors, and improve the maintainability of your projects.

The Challenge: Repetitive Preprocessing

 

After building a machine learning model, one of the best ways to validate its performance is to deploy and use it on new data. Traditionally, this involves using tools like pickle or joblib to save the computational state for reuse with new datasets.

 

However, this approach presents a significant challenge: how do we handle the cleaning, manipulation, and computations required for new datasets, especially when dealing with large volumes of data? Repeating these preprocessing steps manually can quickly become a nightmare.

 

Enter Preprocessing Pipelines

 

To address this challenge, data scientists and ML engineers have developed the concept of preprocessing pipelines. These pipelines allow you to chain together multiple data preprocessing steps, including:

 

1. Data cleaning (e.g., handling missing values)

2. Feature scaling

3. Encoding categorical variables

 

By creating a pipeline, you can apply all these steps to new datasets with a single function call, drastically simplifying your workflow.

 

Benefits of Preprocessing Pipelines

 

1. Consistency: Ensure that all data (training, testing, and new inputs) undergoes the same preprocessing steps.

2. Efficiency: Eliminate the need to manually repeat preprocessing steps for each new dataset.

3. Reduced Risk of Data Leakage: By fitting the pipeline on training data and applying it to test data, you minimize the risk of information from the test set influencing the preprocessing.

4. Improved Maintainability: Pipelines make your code more modular and easier to update or modify.

 

Implementing a Preprocessing Pipeline: A Practical Example

 

Let me walk you through an example of how to create and use a preprocessing pipeline using scikit-learn. We'll use a salary prediction dataset to demonstrate the process.

Python Code:

import pandas as pd

import numpy as np

from sklearn.compose import ColumnTransformer

from sklearn.preprocessing import StandardScaler, OneHotEncoder

from sklearn.impute import SimpleImputer

from sklearn.pipeline import Pipeline

from sklearn.model_selection import train_test_split

 

# Load the data

df = pd.read_csv('Salary_Prediction_Dataset.csv')

 

# Split features and target

X = df.drop('SALARY', axis=1)

y = df['SALARY']

 

# Split into train and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

 

# Define feature groups

numeric_features = ['AGE', 'LEAVES_USED', 'LEAVES_REMAINING', 'RATINGS', 'PAST_EXP', 'YEAR', 'MONTH', 'DAYOFWEEK']

categorical_features = ['GENDER', 'UNIT']

onehot_feature = ['DESIGNATION']

 

# Create transformers for each feature group

numeric_transformer = Pipeline(steps=[

    ('imputer', SimpleImputer(strategy='median')),

    ('scaler', StandardScaler())

])

 

categorical_transformer = Pipeline(steps=[

    ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),

    ('onehot', OneHotEncoder(sparse=False, handle_unknown='ignore'))

])

 

onehot_transformer = Pipeline(steps=[

    ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),

    ('onehot', OneHotEncoder(sparse=False, handle_unknown='ignore'))

])

 

# Combine transformers using ColumnTransformer

preprocessor = ColumnTransformer(

    transformers=[

        ('num', numeric_transformer, numeric_features),

        ('cat', categorical_transformer, categorical_features),

        ('onehot', onehot_transformer, onehot_feature)

    ])

 

# Fit the preprocessor on training data

preprocessor.fit(X_train)

 

# Transform both training and test data

X_train_preprocessed = preprocessor.transform(X_train)

X_test_preprocessed = preprocessor.transform(X_test)

 

# Get feature names

feature_names = preprocessor.get_feature_names_out()

 

# Create DataFrames with preprocessed data

X_train_preprocessed_df = pd.DataFrame(X_train_preprocessed, columns=feature_names)

X_test_preprocessed_df = pd.DataFrame(X_test_preprocessed, columns=feature_names)

 

print("Shape of preprocessed training data:", X_train_preprocessed.shape)

print("Shape of preprocessed test data:", X_test_preprocessed.shape)

print("\nFirst few rows of preprocessed training data:")

print(X_train_preprocessed_df.head())
--------------------------------------------------------------------------------------------


Key Takeaways

 

1. Fit on Training, Transform on Test: Always fit your preprocessing pipeline on the training data and then use it to transform both training and test sets. This prevents data leakage and ensures your model's performance estimates are reliable.

 

2. Handle Unknown Categories: Use handle_unknown='ignore' in OneHotEncoder to gracefully handle new categories in test or production data.

 

3. Verify Your Pipeline: Always check the output of your pipeline to ensure its producing the expected results. Look for unexpected NaN values or outliers.

 

4. Feature Names: Keep track of your feature names after preprocessing. This is crucial for interpretability and debugging.


By implementing preprocessing pipelines, you can significantly streamline your machine learning workflows, making them more efficient, consistent, and maintainable. 

Happy modelling!


- RMS

Comments

Popular posts from this blog

How to Open Jupyter Lab in your favourite browser other than system default browser in Mac OS: A Step-by-Step Guide

Are you tired of Jupyter Lab opening in your default browser? Would you prefer to use Google Chrome or another browser of your choice? This guide will walk you through the process of configuring Jupyter Lab to open in your preferred browser, with a focus on using Google Chrome. The Challenge   Many tutorials suggest using the command prompt to modify Jupyter's configuration. However, this method often results in zsh errors and permission issues, even when the necessary permissions seem to be in place. This guide offers a more reliable solution that has proven successful for many users.   Step-by-Step Solution   1. Locate the Configuration File - Open Finder and navigate to your user folder (typically named after your username). - Use the keyboard shortcut Command + Shift + . (full stop) to reveal hidden folders. - Look for a hidden folder named .jupyter . - Within this folder, you'll find the jupyter_notebook_config.py file.   2. Edit the Configuration File - Open ...

Astype vs pd.to_datetime

Astype and pandas date time Ever wondered that we could be using date time conversion in python could lead us to two different methods that perform same job but little do we know that their real working principle. As we can see below that astype and pd.to_datetime are used for converting a column of dtype say from string or object to Datetime format. By doing so we can separate them for days, week, no of days or day of week by using .dt.dayofweek as an example.  astype Purpose:  General type conversion. Usage:  Converts a pandas object (like a DataFrame column) to a specified dtype. Example:  If you have a column of strings representing dates and you want to convert them to datetime objects, you can use  astype . df[ 'date_column' ] = df[ 'date_column' ].astype( 'datetime64[ns]' ) pd.to_datetime Purpose:  Specialized function for parsing date and time strings to datetime objects. Usage:  Converts argument to datetime, optionally with more control over ...

Understanding Large Language Models (LLMs): A Simple Guide – Part – 1

What is a Large Language Model (LLM)? Imagine you have a robot friend who knows a lot about everything—like books, stories, facts, and even jokes. You can ask this robot questions or give it commands, and it will answer you in a way that sounds like a real person talking! This robot is powered by something called a Large Language Model (LLM). Large Language Models (LLMs) are a type of artificial intelligence (AI) that can understand, generate, and respond to human language. They are powered by deep learning, which is a branch of machine learning that mimics how the human brain works. Essentially, LLMs "learn" from large datasets, like books, articles, and websites, to generate text that makes sense. LLMs are super-smart computers that learn from tons of books, websites, and information to understand language (like Kannada, English, Hindi, and more) and help people with tasks. They can write essays, answer questions, translate languages, or even have fun conversations! Y...