How to save/pickle IsolationForest Model in Python?

  isolation-forest, outliers, pickle, python, scikit-learn

I’m using IsolationForest to predict outliers. And I want to save all my steps so I can use it directly on other data next time.

My code for building isolation forest is:

import sklearn.neighbors._base
import sys
sys.modules['sklearn.neighbors.base'] = sklearn.neighbors._base
from sklearn.ensemble import IsolationForest

iforest = IsolationForest(n_estimators=50, max_samples='auto', contamination=float(0.00005), max_features=1.0,random_state = None)
y_pred = iforest.predict(data)

data['anomaly_score'] = iforest.decision_function(data)


I want to pickle the iforest model and the steps after it. Basically i want to save all the steps I wrote as a whole. How should I do this in python?

Source: Python Questions