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

Solution
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 = iris.data
target = iris.target

#Create decision tree classifer object
classifer = RandomForestClassifier()

#Train model
model = classifer.fit(features, target)

#Save model as pickle file
joblib.dump(model, "model.pkl")
['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
classifer.predict(new_observation)
array([0])

Discussion
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))
['model_0.11.pkl']