Skip to content

Commit da1df33

Browse files
author
Sebastian Pölsterl
committed
BUG: Check if standard deviation is small before dividing by it
This avoids infinit and NaN feature importances
1 parent 97754df commit da1df33

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

sklearn/ensemble/forest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ def _set_oob_feature_importances(self, X, y):
401401
scores_sd = (scores_sd / self.n_estimators -
402402
scores**2) / (self.n_estimators - 1)
403403
np.sqrt(scores_sd, scores_sd)
404+
scores_sd = np.where(scores_sd < np.finfo(np.double).eps,
405+
np.finfo(np.double).eps, scores_sd, )
404406
scores /= scores_sd
405407
self.oob_feature_importances_ = scores / np.sum(scores)
406408

@@ -678,6 +680,8 @@ def _set_oob_feature_importances(self, X, y):
678680
scores_sd = (scores_sd / self.n_estimators -
679681
scores**2) / (self.n_estimators - 1)
680682
np.sqrt(scores_sd, scores_sd)
683+
scores_sd = np.where(scores_sd < np.finfo(np.double).eps,
684+
np.finfo(np.double).eps, scores_sd)
681685
scores /= scores_sd
682686
self.oob_feature_importances_ = scores / np.sum(scores)
683687

0 commit comments

Comments
 (0)