@@ -2537,7 +2537,7 @@ class PowerTransformer(BaseEstimator, TransformerMixin):
2537
2537
>>> print(pt.fit(data))
2538
2538
PowerTransformer(copy=True, method='yeo-johnson', standardize=True)
2539
2539
>>> print(pt.lambdas_)
2540
- [1.38668178e+00 5.93926346e-09 ]
2540
+ [ 1.38668178 -3.10053309 ]
2541
2541
>>> print(pt.transform(data))
2542
2542
[[-1.31616039 -0.70710678]
2543
2543
[ 0.20998268 -0.70710678]
@@ -2718,23 +2718,18 @@ def _box_cox_inverse_tranform(self, x, lmbda):
2718
2718
def _yeo_johnson_inverse_transform (self , x , lmbda ):
2719
2719
"""Return inverse-transformed input x following Yeo-Johnson inverse
2720
2720
transform with parameter lambda.
2721
-
2722
- Notes
2723
- -----
2724
- We're comparing lmbda to 1e-19 instead of strict equality to 0. See
2725
- scipy/special/_boxcox.pxd for a rationale behind this
2726
2721
"""
2727
- x_inv = np .zeros ( x . shape , dtype = x . dtype )
2722
+ x_inv = np .zeros_like ( x )
2728
2723
pos = x >= 0
2729
2724
2730
2725
# when x >= 0
2731
- if lmbda < 1e-19 :
2726
+ if abs ( lmbda ) < np . spacing ( 1. ) :
2732
2727
x_inv [pos ] = np .exp (x [pos ]) - 1
2733
2728
else : # lmbda != 0
2734
2729
x_inv [pos ] = np .power (x [pos ] * lmbda + 1 , 1 / lmbda ) - 1
2735
2730
2736
2731
# when x < 0
2737
- if lmbda < 2 - 1e-19 :
2732
+ if abs ( lmbda - 2 ) > np . spacing ( 1. ) :
2738
2733
x_inv [~ pos ] = 1 - np .power (- (2 - lmbda ) * x [~ pos ] + 1 ,
2739
2734
1 / (2 - lmbda ))
2740
2735
else : # lmbda == 2
8000
@@ -2745,27 +2740,22 @@ def _yeo_johnson_inverse_transform(self, x, lmbda):
2745
2740
def _yeo_johnson_transform (self , x , lmbda ):
2746
2741
"""Return transformed input x following Yeo-Johnson transform with
2747
2742
parameter lambda.
2748
-
2749
- Notes
2750
- -----
2751
- We're comparing lmbda to 1e-19 instead of strict equality to 0. See
2752
- scipy/special/_boxcox.pxd for a rationale behind this
2753
2743
"""
2754
2744
2755
- out = np .zeros ( shape = x . shape , dtype = x . dtype )
2745
+ out = np .zeros_like ( x )
2756
2746
pos = x >= 0 # binary mask
2757
2747
2758
2748
# when x >= 0
2759
- if lmbda < 1e-19 :
2760
- out [pos ] = np .log (x [pos ] + 1 )
2749
+ if abs ( lmbda ) < np . spacing ( 1. ) :
2750
+ out [pos ] = np .log1p (x [pos ])
2761
2751
else : # lmbda != 0
2762
2752
out [pos ] = (np .power (x [pos ] + 1 , lmbda ) - 1 ) / lmbda
2763
2753
2764
2754
# when x < 0
2765
- if lmbda < 2 - 1e-19 :
2755
+ if abs ( lmbda - 2 ) > np . spacing ( 1. ) :
2766
2756
out [~ pos ] = - (np .power (- x [~ pos ] + 1 , 2 - lmbda ) - 1 ) / (2 - lmbda )
2767
2757
else : # lmbda == 2
2768
- out [~ pos ] = - np .log (- x [~ pos ] + 1 )
2758
+ out [~ pos ] = - np .log1p (- x [~ pos ])
2769
2759
2770
2760
return out
2771
2761
@@ -2794,12 +2784,8 @@ def _neg_log_likelihood(lmbda):
2794
2784
x_trans = self ._yeo_johnson_transform (x , lmbda )
2795
2785
n_samples = x .shape [0 ]
2796
2786
2797
- # Estimated mean and variance of the normal distribution
2798
- est_mean = x_trans .sum () / n_samples
2799
- est_var = np .power (x_trans - est_mean , 2 ).sum () / n_samples
2800
-
2801
- loglike = - n_samples / 2 * np .log (est_var )
2802
- loglike += (lmbda - 1 ) * (np .sign (x ) * np .log (np .abs (x ) + 1 )).sum ()
2787
+ loglike = - n_samples / 2 * np .log (x_trans .var ())
2788
+ loglike += (lmbda - 1 ) * (np .sign (x ) * np .log1p (np .abs (x ))).sum ()
2803
2789
2804
2790
return - loglike
2805
2791
0 commit comments