# Export weights (formula) from Random Forest Regressor in Scikit-Learn

I trained a prediction model with Scikit Learn in Python (Random Forest Regressor) and I want to extract somehow the weights of each feature to create an excel tool for manual prediction.

The only thing that I found is the model.feature_importances_ but it doesn't help.

Is there any way to achieve it?

def performRandomForest(X_train, y_train, X_test, y_test):

'''Perform Random Forest Regression'''

from sklearn.ensemble  import  RandomForestRegressor

model  =  RandomForestRegressor()
model.fit( X_train , y_train )

#make predictions
expected  = y_test
predicted  = model.predict( X_test )

#summarize the fit of the model
mse  = np.mean(( predicted - expected )** 2)
accuracy = ( model.score ( X_train , y_train ))

return model, mse, accuracy


At the moment, I use the model.predict([features]) to do it, but I need it in an excel file.

• A random forest regressor is a random forest of decision trees, so you won't get one equation like you do with linear regression. Instead you will get a bunch of if, then, else logic and many final equations to turn the final leaves into numerical values. Even if you can visualize the tree and pull out all of the logic, this all seems like a big mess. If you are working in excel, maybe think about just training your model in excel using Azure. However, I would probably just call the python from within excel. – AN6U5 Jan 8 '16 at 17:08
• Taking the average of each leaf will not work? I tried also a linear regression model and the difference is inside the limits. So, if there isn't a reasonable and efficient way to export the random forest, I may need to step back to linear regression. – Tasos Jan 8 '16 at 17:11
• stackoverflow.com/questions/33732875/… – AN6U5 Jan 8 '16 at 17:13
• Thank you but I was aware of this way in LR. Can you please join your comments on an answer so I could mark it as answered? – Tasos Jan 8 '16 at 17:15
• Its probably worth leaving up/unanswered for a couple days to see if someone else has useful insight. Data science stack exchange is much much smaller than stack overflow, so it takes 2-3 days at times to get good insightful answers. – AN6U5 Jan 8 '16 at 17:22

The SKompiler library might help:

from skompiler import skompile
skompile(rf.predict_proba).to('excel')


Check out this video.

Instead of exporting the weights, you can export the model to a pickle file and use a xlwings to read the data from the spreadsheet, load the pickled model and run a prediction Here's a similar questions.

I guess you want to extract all the logic followed by the different trees to end up on the final regressor. For that, you need to extract first the logic of each tree and then extract how those paths are followed. Scikit learn can provide that through .decision_path(X), with X some dataset to predict. From here you'll get an idea on how the random forest predicts and what logic is followed at each step.

Once you extracted the decision_path, you can use Tree Interpreter to obtain the "formula" of the Random Forest you trained. I am not familiar with this Tree Interpreter, but it seems to work directly on the modeler you have trained, i.e.,

from treeinterpreter import treeinterpreter as ti
# fit a scikit-learn's regressor model

rf = RandomForestRegressor()

rf.fit(trainX, trainY)

prediction, bias, contributions = ti.predict(rf, testX)