scikit-learnrandom-forestisolation-forest

Calculate memory usage of RandomForestClassifier and IsolationForest


I'd like to evaluate how many memory is used up by both

sklearn.ensemble.IsolationForest
sklearn.ensemble.RandomForestClassifier

But

sys.sizeof(my_isolation_forest_model)
sys.sizeof(my_random_forest_classifier_model)

always returns a value of 48, no matter how the model is fit.

Can you help me find out how much memory space are my models using?


Solution

  • Scikit-Learn trees are represented using Cython data structures, which do not interact with Python's sizeof function in a meaningful way - you're just seeing the size of pointer(s).

    As a workaround, you can dump these Cython data structures into a Pickle file (or any alternative serialization protocol/data format), and then measure the size of this file. The data from Cython data structures will all be there.

    When using Pickle, be sure to disable any data compression!