首页 > 解决方案 > 使用带有 sklearn 和 pickle 的随机森林循环预测

问题描述

我在 Python3.6 上使用 sci-kit learn,并且在从 Pickle 加载的随机森林循环中进行预测时遇到了一些运行时问题。

当我从具有 1 个估计器(树)的随机森林进行预测时,每个样本的运行时间约为 0.002 秒。但是,当我增加估计器的数量(>1)时,无论树的数量如何,运行时间都会增加到每个样本 0.1 秒。

这是我保存模型的代码:

clf = RandomForestClassifier(n_estimators=1, #or > 1 
            n_jobs=-1,
            random_state=2,
            max_depth=15,
            min_samples_leaf=1,
            verbose=0,
            max_features='auto'
            )

clf.fit(X_train, y_train)

with open('classifier.pkl', 'wb') as fid:
    cPickle.dump(clf, fid)  

这是我在循环中加载模型和预测的代码:

with open('classifier.pkl', 'rb') as fid:
    clf = cPickle.load(fid)

for s in samples:
    #my feature extraction method
    pred = clf.predict(feature) #feature is a 1D np array containing features computed for the sample s 

我不明白为什么运行时间与树的数量不成正比,以及为什么它增长得如此之快。这是一个错误还是我以错误的方式使用泡菜?

拜托,你能帮帮我吗?

CB

标签: pythonmachine-learningruntimepicklerandom-forest

解决方案


推荐阅读