Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 302106b

Browse files
authored
FIX RuntimeWarning by dividing by zero in test_iforest_with_uniform_data (#19622)
1 parent ac6dea5 commit 302106b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

sklearn/ensemble/_iforest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,14 @@ def _compute_score_samples(self, X, subsample_features):
450450
+ _average_path_length(n_samples_leaf)
451451
- 1.0
452452
)
453-
453+
denominator = (
454+
len(self.estimators_) * _average_path_length([self.max_samples_])
455+
)
454456
scores = 2 ** (
455-
-depths
456-
/ (len(self.estimators_)
457-
* _average_path_length([self.max_samples_]))
457+
# For a single training sample, denominator and depth are 0.
458+
# Therefore, we set the score manually to 1.
459+
-np.divide(depths, denominator, out=np.ones_like(depths),
460+
where=denominator != 0)
458461
)
459462
return scores
460463

0 commit comments

Comments
 (0)