Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Sklearn Model Saving and Loading

Sklearn Model Saving and Loading

In machine learning, the model training process is usually time-consuming. To avoid retraining the model each time, we can save the trained model for later loading and prediction.

scikit-learn Two common ways to save and load models are provided:joblib and pickle.

1. Use joblib to save and load the model

joblib is an efficient Python serialization tool, especially suitable for saving objects containing large numerical arrays (such as numpy arrays, scikit-learn models, etc.). Compared to pickle, joblib , it is more efficient when processing large-scale data.

joblib is an external Python library that can be installed with the following command:



pip install joblib

Save the model

joblib provides a simple API to save and load objects.

We can use the joblib.dump() method to save the model to a file.

Example

import

joblib

from

sklearn.

datasets

import

load_iris

from

sklearn.

model_selection

import

train_test_split

from

sklearn.

svm

import

SVC

Load data

data

=

load_iris

(

)

X

,

y

=

data.

data

,

data.

target

Split into training and test sets

X_train

,

X_test

,

y_train

,

y_test

=

train_test_split

(

X

,

y

,

test_size

=

0.2

,

random_state

=

42

)

Create and train the model

model

=

SVC

(

kernel

=

'linear'

)

model.

fit

(

X_train

,

y_train

)

Save the model to a file

joblib.

dump

(

model

,

'svm_model.joblib'

)

Load model

Use the joblib.load() method to load the saved model object.

Example

Load the saved model

loaded_model

=

joblib.

load

(

'svm_model.joblib'

)

Use the loaded model for prediction

y_pred

=

loaded_model.

predict

(

X_test

)

Print the prediction results

print

(

"Predictions:"

,

y_pred

)

Through the above steps, we have successfully saved the trained model to a file and can load the model and make predictions at any later time.


2. Use pickle to save and load the model

pickle is a built-in Python module that allows serialization and deserialization of Python objects.

Although joblib is more suitable for handling large amounts of data, pickle is also a commonly used tool for saving and loading models and is suitable for general cases.

Save the model

Similar to joblib, pickle also has a simple API for saving and loading objects.

The code to save the model is as follows:

Example

import

pickle

from

sklearn.

datasets

import

load_iris

from

sklearn.

model_selection

import

train_test_split

from

sklearn.

svm

import

SVC

Load data

data

=

load_iris

(

)

X

,

y

=

data.

data

,

data.

target

Split into training and test sets

X_train

,

X_test

,

y_train

,

y_test

=

train_test_split

(

X

,

y

,

test_size

=

0.2

,

random_state

=

42

)

Create and train the model

model

=

SVC

(

kernel

=

'linear'

)

model.

fit

(

X_train

,

y_train

)

Save the model using pickle

with

open

(

'svm_model.pkl'

,

'wb'

)

as

f:

   

pickle

.

dump

(

model

,

f

)

Load model

Load the model using pickle.load():

Example

Load the saved model using pickle

with

open

(

'svm_model.pkl'

,

'rb'

)

as

f:

    loaded_model

=

pickle

.

load

(

f

)

Use the loaded model for prediction

y_pred

=

loaded_model.

predict

(

X_test

)

Print the prediction results

print

(

"Predictions:"

,

y_pred

)


3. joblib vs pickle

joblib and pickle are two commonly used methods for saving and loading models.

joblib is more suitable for saving large data objects, while pickle is Python's standard serialization tool, suitable for general cases.

If the model contains a large number of numerical arrays or matrices (such as SVM, random forest, etc.), joblib is recommended as it is more efficient than pickle. For smaller models or models without large amounts of numerical data, pickle is sufficient.


4. Save and load a Pipeline

In practical applications, a model is not just a single model; sometimes multiple processing steps (such as data preprocessing, feature selection, model training, etc.) are combined. These processing steps can be accomplished using scikit-learn's Pipeline, which can also be saved and loaded via joblib or pickle.

Save the pipeline:

Example

from

sklearn.

pipeline

import

Pipeline

from

sklearn.

preprocessing

import

StandardScaler

from

sklearn.

svm

import

SVC

import

joblib

Create a pipeline

pipeline

=

Pipeline

(

[

   

(

'scaler'

,

StandardScaler

(

)

)

,

   

(

'svc'

,

SVC

(

kernel

=

'linear'

)

)

]

)

Train the pipeline

pipeline.

fit

(

X_train

,

y_train

)

Save the pipeline to a file

joblib.

dump

(

pipeline

,

'pipeline_model.joblib'

)

Load the pipeline:

Example

Load the pipeline

loaded_pipeline

=

joblib.

load

(

'pipeline_model.joblib'

)

Use the loaded pipeline for prediction

y_pred

=

loaded_pipeline.

predict

(

X_test

)

Print the prediction results

print

(

"Predictions:"

,

y_pred

)

The process of saving and loading a pipeline is the same as for a single model; just ensure the entire pipeline object is saved and loaded.


5. Model version management

In practical machine learning applications, model updates and version management are crucial. Each time a model is trained and saved, it is best to add a timestamp or version number to the model file name to distinguish different versions of the model. For example:

Example

import

time

Create a timestamp

timestamp

=

time

.

strftime

(

"%Y%m%d-%H%M%S"

)

Save the model with a timestamp

joblib.

dump

(

model

,

f

'svm_model_{timestamp}.joblib'

)

In this way, we can manage different versions of the model based on timestamps, facilitating model rollback and updates.


6. Use the model for persistence

Once the model is trained and saved, we can load it in subsequent practical applications to make predictions without retraining.

For example, we can integrate the saved model with web services, batch jobs, or other applications so that the model can be reused without retraining.

Use the loaded model in a web service

For example, suppose we are using Flask to create a simple web service that provides model prediction services through an API interface. In this case, we can load the saved model for real-time prediction.

Example

from

flask

import

Flask

,

request

,

jsonify

import

joblib

import

numpy

as

np

app

=

Flask

(

name

)

Load model

model

=

joblib.

load

(

'svm_model.joblib'

)

@

app.

route

(

'/predict'

,

methods

=

[

'POST'

]

)

def

predict

(

)

:

    data

=

request.

get_json

(

)

 

Get the input data

    features

=

np.

array

(

data

[

'features'

]

)

.

reshape

(

1

,

1

)

 

Convert to a format suitable for prediction

    prediction

=

model.

predict

(

features

)

 

Use the loaded model for prediction

   

return

jsonify

(

{

'prediction'

: prediction.

tolist

(

)

}

)

 

Return the prediction result

if

name

==

'main'

:

    app.

run

(

debug

=

True

)