【发布时间】:2018-11-13 13:27:45
【问题描述】:
我在 Python 3.6 上使用 sklearn,我注意到将单个样本预测为 1D numpy 数组所需的运行时间与将 n 个样本预测为具有随机森林的 2D numpy 数组所需的运行时间相同(两者都约为 0.1 秒)。看起来 sklearn 在每个预测步骤首先需要一定的时间来设置树,然后立即进行预测。这可以解释为什么预测大型二维数组的运行时间与一维数组相同?
这是我训练模型的代码:
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
是不是因为我用错了方法?还是 sklearn 没有针对一一预测进行优化?
【问题讨论】:
-
你确定你测量的运行时间不是由分类器的解压控制的吗?
-
是的,我在不使用pickle的情况下在训练步骤之后进行预测时发现了同样的问题。
-
max_depth设置为 15。这可能是它变慢的原因之一。其次,您的特征提取方法也可能很耗时。我的作品中的两个之一。
标签: python machine-learning scikit-learn runtime random-forest