【问题标题】:Why sklearn RandomForest model take a lot of disk space after save?为什么 sklearn 随机森林模型保存后会占用大量磁盘空间?
【发布时间】:2016-07-31 03:45:52
【问题描述】:

我正在使用下面的代码从 sklearn 库中保存 RandomForestClassifier 模型

with open('/tmp/rf.model', 'wb') as f:
    cPickle.dump(RF_model, f)

它在我的硬盘驱动器上占用了很多空间。模型中只有 50 棵树,但它占用了超过 50 MB 的磁盘空间(分析数据集约为 20 MB,有 21 个特征)。有人知道为什么吗?我观察到 ExtraTreesClassifier 的类似行为。

编辑: 射频参数:

"n_estimators": 50,
"max_features": 0.2,
"min_samples_split": 20,
"criterion": "gini",
"min_samples_leaf": 11

根据@dooms 的建议,我检查了 sys.getsizeof 并返回 64 - 我假设这只是指针大小。

我尝试了其他方法来保存模型:

from sklearn.externals import joblib
joblib.dump(RF_model, 'filename.pkl') 

通过使用这种方式,我得到 1 个 *.pkl 文件和 201 个 *.npy 文件,总大小为 14.9 MB,比之前的 53 MB 小。在这 201 个 npy 文件中有一个模式 - Forest 中每棵树有 4 个文件:

第一个文件(231 KB)内容:

array([(1, 1062, 20, 0.2557438611984253, 0.4997574055554296, 29168, 46216.0),
       (2, 581, 12, 0.5557271242141724, 0.49938159451291675, 7506, 11971.0),
       (3, 6, 14, 0.006186043843626976, 0.4953095968671224, 4060, 6422.0),
       ...,
       (4123, 4124, 15, 0.6142271757125854, 0.4152249134948097, 31, 51.0),
       (-1, -1, -2, -2.0, 0.495, 11, 20.0),
       (-1, -1, -2, -2.0, 0.3121748178980229, 20, 31.0)], 
      dtype=[('left_child', '<i8'), ('right_child', '<i8'), ('feature', '<i8'), ('threshold', '<f8'), ('impurity', '<f8'), ('n_node_samples', '<i8'), ('weighted_n_node_samples', '<f8')])

第二个文件(66 kB)内容:

array([[[  2.25990000e+04,   2.36170000e+04]],

       [[  6.19600000e+03,   5.77500000e+03]],

       [[  3.52200000e+03,   2.90000000e+03]],

       ..., 
       [[  3.60000000e+01,   1.50000000e+01]],

       [[  1.10000000e+01,   9.00000000e+00]],

       [[  2.50000000e+01,   6.00000000e+00]]])

第三个文件(88B):

array([2])

组中的最后一个文件 (96B):

array([ 0.,  1.])

有什么想法吗?我试图查看 sklearn 中的 Tree 代码,但这很难。任何想法如何保存它存储更少磁盘的sklearn树? (只是指出类似大小的 xgboost 集合占用了大约 200KB 的总大小)

【问题讨论】:

  • 分类器的参数是什么?树的数量和最大深度 / min_samples_{split,leaf} 是相关的。

标签: python scikit-learn random-forest


【解决方案1】:

您无法将RandomForestXGBoost 模型进行比较,因为XGBoost 中的树非常浅,而RandomForest 中的树要深得多。这意味着随机森林中的每棵树也更大。如果你想比较尺寸,或者如果你想要一个占用更少空间的模型,你应该改用HistGradientBoostingClassifiersklearn 中的HistGradientBoostingRegressor

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 2018-11-24
    • 2017-10-19
    • 2017-05-14
    • 2020-01-16
    • 2016-07-23
    • 2017-05-14
    • 2017-08-29
    • 2018-10-09
    相关资源
    最近更新 更多