FIX RuntimeWarning by dividing by zero in test_iforest_with_uniform_data
@@ -450,11 +450,14 @@ class IsolationForest(OutlierMixin, BaseBagging):
+ _average_path_length(n_samples_leaf)
- 1.0
)
-
+ denominator = (
+ len(self.estimators_) * _average_path_length([self.max_samples_])
+ )
scores = 2 ** (
- -depths
- / (len(self.estimators_)
- * _average_path_length([self.max_samples_]))
+ # For a single training sample, denominator and depth are 0.
+ # Therefore, we set the score manually to 1.
+ -np.divide(depths, denominator, out=np.ones_like(depths),
+ where=denominator != 0)
)
return scores
