Persisting models with joblib or pickle
In this recipe, we're going to show how you can keep your model around for later use. For example, you might want to actually use a model to predict an outcome and automatically make a decision.
Getting ready
Create a dataset and train a classifier:
from sklearn.datasets import make_classification from sklearn.tree import DecisionTreeClassifier X, y = make_classification() dt = DecisionTreeClassifier() dt.fit(X, y)
How to do it...
- Save the training work the classifier has done with joblib:
from sklearn.externals import joblib joblib.dump(dt, "dtree.clf") ['dtree.clf']
Opening the saved model
- Load the model with joblib. Make a prediction with a set of inputs:
from sklearn.externals import joblib pulled_model = joblib.load("dtree.clf") y_pred = pulled_model.predict(X)
We did not have to train the model again, and have saved a lot of training time. We simply reloaded it with joblib and made a prediction.
There's more...
You can also use the cPickle
module in Python...