提高 sklearn 中随机森林回归器的性能

Increase performance of Random Forest Regressor in sklearn

有一个优化问题,我必须调用随机森林回归器的预测函数数千次。

from sklearn.ensemble import RandomForestRegressor
rfr = RandomForestRegressor(n_estimators=10)
rfr = rfr.fit(X, Y)
for iteration in range(0, 100000):
    # code that adapts the input data according to fitness of the last output
    output_data = rfr.predict(input_data)
    # code that evaluates the fitness of output data

在这种情况下,有没有办法提高预测功能的速度?可能通过使用 Cython?

您可以使用 SKompiler (https://github.com/konstantint/SKompiler) 将它转换为 C 或 C++ 代码,然后 运行 在那里。

from skompiler import skompile
expr = skompile(rfr.predict)
with open("output.cpp", "w") as text_file: print(expr.to('sympy/cxx'), file=text_file)