Tim*_*ams 6 python random-forest scikit-learn quantile-regression
我已经开始使用该scikit-garden软件包中的分位数随机森林(QRF)。以前,我是使用RandomForestRegresserfrom 创建常规随机森林的sklearn.ensemble。
看起来,QRF的速度与具有较小数据集大小的常规RF相当,但是随着数据大小的增加,QRF的预测速度要比RF慢得多。
这是预期的吗?如果是这样,有人可以解释一下为什么要花这么长时间才能做出这些预测和/或就如何更及时地获得分位数预测给出任何建议。
请参见下面的玩具示例,在该示例中,我测试了各种数据集大小的训练时间和预测时间。
import matplotlib as mpl
mpl.use('Agg')
from sklearn.ensemble import RandomForestRegressor
from skgarden import RandomForestQuantileRegressor
from sklearn.model_selection import train_test_split
import numpy as np
import time
import matplotlib.pyplot as plt
log_ns = np.arange(0.5, 5, 0.5) # number of observations (log10)
ns = (10 ** (log_ns)).astype(int)
print(ns)
m = 14 # number of covariates
train_rf = []
train_qrf = []
pred_rf = []
pred_qrf = []
for n in ns:
# create dataset
print('n = {}'.format(n))
print('m = {}'.format(m))
rndms = np.random.normal(size=n)
X = np.random.uniform(size=[n,m])
betas = np.random.uniform(size=m)
y = 3 + np.sum(betas[None,:] * X, axis=1) + rndms
# split test/train
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# random forest
rf = RandomForestRegressor(n_estimators=1000, random_state=0)
st = time.time()
rf.fit(X_train, y_train)
en = time.time()
print('Fit time RF = {} secs'.format(en - st))
train_rf.append(en - st)
# quantile random forest
qrf = RandomForestQuantileRegressor(random_state=0, min_samples_split=10, n_estimators=1000)
qrf.set_params(max_features = X.shape[1] // 3)
st = time.time()
qrf.fit(X_train, y_train)
en = time.time()
print('Fit time QRF = {} secs'.format(en - st))
train_qrf.append(en - st)
# predictions
st = time.time()
preds_rf = rf.predict(X_test)
en = time.time()
print('Prediction time RF = {}'.format(en - st))
pred_rf.append(en - st)
st = time.time()
preds_qrf = qrf.predict(X_test, quantile=50)
en = time.time()
print('Prediction time QRF = {}'.format(en - st))
pred_qrf.append(en - st)
fig, ax = plt.subplots()
ax.plot(np.log10(ns), train_rf, label='RF train', color='blue')
ax.plot(np.log10(ns), train_qrf, label='QRF train', color='red')
ax.plot(np.log10(ns), pred_rf, label='RF predict', color='blue', linestyle=':')
ax.plot(np.log10(ns), pred_qrf, label='QRF predict', color='red', linestyle =':')
ax.legend()
ax.set_xlabel('log(n)')
ax.set_ylabel('time (s)')
fig.savefig('time_comparison.png')
Run Code Online (Sandbox Code Playgroud)
输出为: RF和QRF训练和预测的时间比较
我不是这个或任何分位数回归包的开发人员,但我已经查看了 scikit-garden 和 quantRegForest/ranger 的源代码,我对为什么 R 版本要快得多有一些想法:
编辑:在一个相关的 github 问题上,lmssdd 提到这种方法的性能比论文中的“标准程序”要差得多。我还没有详细阅读这篇论文,所以对这个答案持怀疑态度。
skgardenpredict函数的基本思想是保存所有y_train叶子对应的所有值。然后,在预测新样本时,您收集相关的叶子和相应的y_train值,并计算该数组的(加权)分位数。R 版本有一个捷径:它们只为y_train每个叶节点保存一个随机选择的值。这有两个优点:它使相关y_train值的收集更加简单,因为每个叶节点中总是只有一个值。其次,它使分位数计算更加简单,因为每个叶子都具有完全相同的权重。
由于每个叶子只使用一个(随机)值而不是所有值,因此这是一种近似方法。根据我的经验,如果你有足够多的树(至少 50-100 左右),这对结果的影响很小。但是,我对数学的了解还不足以说明近似值到底有多好。
下面是一个更简单的 R 分位数预测方法的实现,用于 RandomForestQuantileRegressor 模型。请注意,该函数的前半部分是为每个叶子选择随机 y_train 值的(一次性)过程。如果作者要在 skgarden 中实现这个方法,他们会合乎逻辑地将这部分移动到fit方法中,只留下最后 6 行左右,这使得predict方法更快。同样在我的示例中,我使用的是从 0 到 1 的分位数,而不是从 0 到 100。
def predict_approx(model, X_test, quantiles=[0.05, 0.5, 0.95]):
"""
Function to predict quantiles much faster than the default skgarden method
This is the same method that the ranger and quantRegForest packages in R use
Output is (n_samples, n_quantiles) or (n_samples, ) if a scalar is given as quantiles
"""
# Begin one-time calculation of random_values. This only depends on model, so could be saved.
n_leaves = np.max(model.y_train_leaves_) + 1 # leaves run from 0 to max(leaf_number)
random_values = np.zeros((model.n_estimators, n_leaves))
for tree in range(model.n_estimators):
for leaf in range(n_leaves):
train_samples = np.argwhere(model.y_train_leaves_[tree, :] == leaf).reshape(-1)
if len(train_samples) == 0:
random_values[tree, leaf] = np.nan
else:
train_values = model.y_train_[train_samples]
random_values[tree, leaf] = np.random.choice(train_values)
# Optionally, save random_values as a model attribute for reuse later
# For each sample, get the random leaf values from all the leaves they land in
X_leaves = model.apply(X_test)
leaf_values = np.zeros((X_test.shape[0], model.n_estimators))
for i in range(model.n_estimators):
leaf_values[:, i] = random_values[i, X_leaves[:, i]]
# For each sample, calculate the quantiles of the leaf_values
return np.quantile(leaf_values, np.array(quantiles), axis=1).transpose()
Run Code Online (Sandbox Code Playgroud)