Skip to content

ENH Use scipy.stats.yeojohnson in PowerTransformer #27818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions sklearn/preprocessing/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from numbers import Integral, Real

import numpy as np
import scipy
from scipy import optimize, sparse, stats
from scipy.special import boxcox

Expand All @@ -26,6 +27,7 @@
from ..utils._array_api import get_namespace
from ..utils._param_validation import Interval, Options, StrOptions, validate_params
from ..utils.extmath import _incremental_mean_and_var, row_norms
from ..utils.fixes import parse_version
from ..utils.sparsefuncs import (
incr_mean_variance_axis,
inplace_column_scale,
Expand Down Expand Up @@ -3387,8 +3389,16 @@
# the computation of lambda is influenced by NaNs so we need to
# get rid of them
x = x[~np.isnan(x)]
# choosing bracket -2, 2 like for boxcox
return optimize.brent(_neg_log_likelihood, brack=(-2, 2))

Check warning on line 3392 in sklearn/preprocessing/_data.py

View check run for this annotation

Codecov / codecov/patch

sklearn/preprocessing/_data.py#L3392

Added line #L3392 was not covered by tests
# TODO: remove this when minimum version of scipy >= 1.9.0
scipy_version = parse_version(scipy.__version__)
min_scipy_version = "1.9.0"
if scipy_version < parse_version(min_scipy_version):
# choosing bracket -2, 2 like for boxcox
return optimize.brent(_neg_log_likelihood, brack=(-2, 2))

_, lmbda = stats.yeojohnson(x, lmbda=None)

Check warning on line 3400 in sklearn/preprocessing/_data.py

View check run for this annotation

Codecov / codecov/patch

sklearn/preprocessing/_data.py#L3400

Added line #L3400 was not covered by tests
return lmbda

def _check_input(self, X, in_fit, check_positive=False, check_shape=False):
"""Validate the input before fit and transform.
Expand Down
7 changes: 7 additions & 0 deletions sklearn/preprocessing/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2583,3 +2583,10 @@ def test_power_transformer_constant_feature(standardize):
assert_allclose(Xt_, np.zeros_like(X))
else:
assert_allclose(Xt_, X)


def test_yeojohnson_for_different_scipy_version():
"""Check the result are the same for different scipy version"""
# https://github.com/scikit-learn/scikit-learn/pull/27818
pt = PowerTransformer(method="yeo-johnson").fit(X_1col)
assert_almost_equal(pt.lambdas_[0], 0.99999353)
Loading