You have a trained scikit-learn model and want to save it and load it elsewhere.

Save the model as a pickle file:

#Load libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
from sklearn.externals import joblib
#Load data
iris = datasets.load_iris()
features =
target =

#Create decision tree classifer object
classifer = RandomForestClassifier()

#Train model
model =, target)

#Save model as pickle file
joblib.dump(model, "model.pkl")

Once the model is saved we can use scikit-learn in our destination application (e.g.,
web application) to load the model:

# Load model from file
classifer = joblib.load("model.pkl")

And use it make predictions:

# Create new observation
new_observation = [[ 5.2, 3.2, 1.1, 0.1]]
# Predict observation's class

The first step in using a model in production is to save that model as a file that can be
loaded by another application or workflow. We can accomplish this by saving the
model as a pickle file, a Python-specific data format. Specifically, to save the model we
use joblib, which is a library extending pickle for cases when we have large NumPy
arrays—a common occurrence for trained models in scikit-learn.
When saving scikit-learn models, be aware that saved models might not be compati‐
ble between versions of scikit-learn; therefore, it can be helpful to include the version
of scikit-learn used in the model in the filename:

# Import library
import sklearn
# Get scikit-learn version
scikit_version = joblib.__version__
# Save model as pickle file
joblib.dump(model, "model_{version}.pkl".format(version=scikit_version))