Search code examples
pythonscikit-learnsklearn-pandas

Increase performance of Random Forest Regressor in sklearn


There is an optimization problem where I have to call the predict function of a Random Forest Regressor several thousand times.

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

Is there a way to increase the speed of the predict function in this case? Possibly by using Cython?


Solution

  • You can convert it to C or C++ Code with SKompiler (https://github.com/konstantint/SKompiler) and then run it there.

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