How to Save and Load Fit Scikit-Learn Scalers

How to Save and Load Fit Scikit-Learn Scalers

Scikit-Learn's scalers are the backbone of practically all regressors and classifiers built on top of them, scaling the data to a workable range and preparing a latent representation to learn from.

If you'd like to read more about feature scaling, read our "Feature Scaling Data with Scikit-Learn for Machine Learning in Python"!

When you want to push your model to production, you'll want to scale the data in the same way it was scaled during training for your model to work. A fresh scaler that wasn't fit on your training data will never reproduce the same latent representations!

Thankfully, it's easy to save an already fit scaler and load it in a different environment alongside the model, to scale the data in the same way as during training:

import joblib

scaler = sklearn.preprocessing.StandardScaler()
joblib.dump(scaler, 'scaler.save') 
scaler = joblib.load('scaler.save') 

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

Putting it into practice:

import joblib

scaler = sklearn.preprocessing.MinMaxScaler()
scaler.fit(X_train)
print('Scaler results:', scaler.transform(X_train)[:1])

joblib.dump(scaler, 'scaler.save') 
scaler = joblib.load('scaler.save') 
print('Loaded scaler results:', scaler.transform(X_train)[:1])

This results in:

Scaler results: [[0.16060468 0.52941176 0.02742132 0.02532079 0.02561875 0.00184402
  0.4293305  0.47310757]]
  
Loaded scaler results: [[0.16060468 0.52941176 0.02742132 0.02532079 0.02561875 0.00184402
  0.4293305  0.47310757]]

The data was scaled in the exact same way across both scaler objects!

Last Updated: August 22nd, 2022
Was this helpful?
David LandupAuthor

Entrepreneur, Software and Machine Learning Engineer, with a deep fascination towards the application of Computation and Deep Learning in Life Sciences (Bioinformatics, Drug Discovery, Genomics), Neuroscience (Computational Neuroscience), robotics and BCIs.

Great passion for accessible education and promotion of reason, science, humanism, and progress.

Project

Hands-On House Price Prediction - Machine Learning in Python

# deep learning# tensorflow# machine learning# python

If you've gone through the experience of moving to a new house or apartment - you probably remember the stressful experience of choosing a property,...

David Landup
Ammar Alyousfi
Jovana Ninkovic
Details
Project

Building Your First Convolutional Neural Network With Keras

# artificial intelligence# machine learning# keras# deep learning

Most resources start with pristine datasets, start at importing and finish at validation. There's much more to know. Why was a class predicted? Where was...

David Landup
David Landup
Details

© 2013-2022 Stack Abuse. All rights reserved.

DisclosurePrivacyTerms