Reputation: 846
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?
Upvotes: 4
Views: 572
Reputation: 231
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)
Upvotes: 5