How to derive regression prediction from each tree in Random Forest in Python scikit-learn?

I am new to scikit-learn and random forest regression and wondered if there is an easy way to get predictions from each tree in a random forest in addition to combined prediction. I would like to display all the predictions in the list and not look at the whole tree. I know that I can get sheet indexes using the apply method, but I'm not sure how to use this to get the value from the sheet. Any help is appreciated.

Edit: this is what I have left from the comments below. It was not clear to me that the trees in the valuators_ attribute could be called, but it seems that the forecasting method can be used on each tree using this attribute. Is this the best way to do this?

numberTrees = 100 clf = RandomForestRegressor(n_estimators=numberTrees) clf.fit(X,Y) for tree in range(numberTrees): print(clf.estimators_[tree].predict(val.irow(1))) 
+8
python scikit-learn regression random-forest
source share
2 answers

Short, I use sklearn in a battle at Kaggle, and I'm sure you have the best you can do. As you noted, pred () returns a forecast for the entire RF, but not for its component trees. It can return a matrix, but this is only for the case when several goals are studied together. In this case, it returns one prediction per target; it does not return predictions for each tree. You can get individual tree predictions in an R random forest using pred.all = True, but sklearn doesn't. If you try to use apply (), you get a matrix of leaf indices, and you still have to iterate over the trees to find out what the prediction is for this tree / leaf combination. Therefore, I think that you have as good as it turns out.

+4
source share

I am not 100% sure what you definitely want, but there are other methods in the Scikit-learns Random Forest Regressor that will most likely return what you want, in particular the predict method! This method returns an array of predicted values. What you mean by getting the average is the score method, which simply uses the predict method to return the coefficient R squared determinant.

0
source share

All Articles