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)
iforest.fit(imputed_data_initial)
y_pred = iforest.predict(data)

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

data['anomaly_label']=y_pred
data[data.anomaly_label==-1].head()

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

LEAVE A COMMENT