@@ -335,7 +335,7 @@ def predict(self, X):
335
335
X = self ._validate_data (X , accept_sparse = "csr" , reset = False )
336
336
is_inlier = np .ones (X .shape [0 ], dtype = int )
337
337
# is_inlier[self.decision_function(X) < 0] = -1
338
- is_inlier [self .decision_function (X ) < - 1.0e-15 ] = - 1
338
+ is_inlier [self .decision_function (X ) < - 2 * np . finfo ( float ). eps ] = - 1
339
339
return is_inlier
340
340
341
341
def decision_function (self , X ):
@@ -486,6 +486,7 @@ def _more_tags(self):
486
486
}
487
487
488
488
489
+ # Lookup table used below in _average_path_length() for small samples
489
490
_average_path_length_small = np .array (
490
491
(
491
492
0.0 ,
@@ -558,6 +559,21 @@ def _average_path_length(n_samples_leaf):
558
559
Returns
559
560
-------
560
561
average_path_length : ndarray of shape (n_samples,)
562
+
563
+ Notes
564
+ -----
565
+ Average path length equals :math:`2*(H(n)-1)`, with :math:`H(n)`
566
+ the :math:`n`th harmonic number. Calculation adapted from the
567
+ harmonic number asymptotic expansion, see Wikipedia
568
+ (https://en.wikipedia.org/wiki/Harmonic_number#Calculation) or
569
+ M.B. Villarino in [MBV]_.
570
+
571
+ References
572
+ ----------
573
+ .. [MBV] Villarino, M.B. Ramanujan’s Harmonic Number Expansion Into Negative
574
+ Powers Of A Triangular Number. JIPAM. J. Inequal. Pure Appl. Math. 9(3),
575
+ 89 (2008). https://www.emis.de/journals/JIPAM/article1026.html.
576
+ Preprint at https://arxiv.org/abs/0707.3950.
561
577
"""
562
578
563
579
n_samples_leaf = check_array (n_samples_leaf , ensure_2d = False )
@@ -566,7 +582,7 @@ def _average_path_length(n_samples_leaf):
566
582
n_samples_leaf = n_samples_leaf .reshape ((1 , - 1 ))
567
583
average_path_length = np .zeros (n_samples_leaf .shape )
568
584
569
- mask_small = n_samples_leaf < 52
585
+ mask_small = n_samples_leaf < len ( _average_path_length_small )
570
586
not_mask = ~ mask_small
571
587
572
588
average_path_length [mask_small ] = _average_path_length_small [
@@ -575,20 +591,21 @@ def _average_path_length(n_samples_leaf):
575
591
576
592
# Average path length equals 2*(H(n)-1), with H(n) the nth harmonic number.
577
593
# For the harmonic number calculation,
578
- # see the following publications and references therein
594
+ # see Wikipedia (https://en.wikipedia.org/wiki/Harmonic_number#Calculation)
595
+ # or the following publications and references therein
579
596
# Villarino, M.B. Ramanujan’s Harmonic Number Expansion Into Negative
580
597
# Powers Of A Triangular Number. JIPAM. J. Inequal. Pure Appl. Math. 9(3),
581
- # 89 (2008). https://www.emis.de/journals/JIPAM/article1026.html?sid=1026 .
598
+ # 89 (2008). https://www.emis.de/journals/JIPAM/article1026.html.
582
599
# Preprint at https://arxiv.org/abs/0707.3950.
583
600
# or
584
601
# Wang, W. Harmonic Number Expansions of the Ramanujan Type.
585
602
# Results Math 73, 161 (2018). https://doi.org/10.1007/s00025-018-0920-8
586
603
587
- tmp = 1.0 / np .square (n_samples_leaf [not_mask ])
604
+ n2_inv = 1.0 / np .square (n_samples_leaf [not_mask ])
588
605
average_path_length [not_mask ] = (
589
606
2.0 * (np .log (n_samples_leaf [not_mask ]) - 1.0 + np .euler_gamma )
590
607
+ 1.0 / n_samples_leaf [not_mask ]
591
- - tmp * (1.0 / 6.0 - tmp * (1.0 / 60.0 - tmp / 126.0 ))
608
+ - n2_inv * (1.0 / 6.0 - n2_inv * (1.0 / 60.0 - n2_inv / 126.0 ))
592
609
)
593
610
594
611
return average_path_length .reshape (n_samples_leaf_shape )
0 commit comments