Ann*_*off 4 python scikit-learn sklearn-pandas
有一个优化问题,我必须调用随机森林回归器的预测函数数千次。
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
Run Code Online (Sandbox Code Playgroud)
在这种情况下,有没有办法提高预测功能的速度?可能通过使用Cython?
小智 5
您可以使用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)
Run Code Online (Sandbox Code Playgroud)