10000 Fix issue #11839 · albertcthomas/scikit-learn@8fddcb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8fddcb8

Browse files
joshuakennethjonesalbertcthomas
authored andcommitted
Fix Issue scikit-learn#11839 : sklearn.ensemble.IsolationForest._average_path_length returns incorrect values for input < 3.
1 parent 7bfed17 commit 8fddcb8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

sklearn/ensemble/iforest.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ def _average_path_length(n_samples_leaf):
439439
"""
440440
if isinstance(n_samples_leaf, INTEGER_TYPES):
441441
if n_samples_leaf <= 1:
442+
return 0.
443+
if n_samples_leaf <= 2:
442444
return 1.
443445
else:
444446
return 2. * (np.log(n_samples_leaf - 1.) + np.euler_gamma) - 2. * (
@@ -450,10 +452,12 @@ def _average_path_length(n_samples_leaf):
450452
n_samples_leaf = n_samples_leaf.reshape((1, -1))
451453
average_path_length = np.zeros(n_samples_leaf.shape)
452454

453-
mask = (n_samples_leaf <= 1)
454-
not_mask = np.logical_not(mask)
455+
mask_1 = (n_samples_leaf <= 1)
456+
mask_2 = (n_samples_leaf == 2)
457+
not_mask = np.logical_not(np.logical_or(mask_1, mask_2))
455458

456-
average_path_length[mask] = 1.
459+
average_path_length[mask_1] = 0.
460+
average_path_length[mask_2] = 1.
457461
average_path_length[not_mask] = 2. * (
458462
np.log(n_samples_leaf[not_mask] - 1.) + np.euler_gamma) - 2. * (
459463
n_samples_leaf[not_mask] - 1.) / n_samples_leaf[not_mask]

0 commit comments

Comments
 (0)
0