Skip to content

FIX Adds check_array to inverse_transform of StandardScaler #19356

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

Merged
merged 11 commits into from
Mar 23, 2021
Merged
3 changes: 3 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ Changelog
positioning strategy ``knots``.
:pr:`18368` by :user:`Christian Lorentzen <lorentzenchr>`.

- |Fix| :meth:`preprocessing.StandardScaler.inverse_transform` now
correctly handles integer dtypes. :pr:`19356` by :user:`makoeppel`.

:mod:`sklearn.tree`
...................

Expand Down
11 changes: 3 additions & 8 deletions sklearn/preprocessing/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,22 +911,17 @@ def inverse_transform(self, X, copy=None):
check_is_fitted(self)

copy = copy if copy is not None else self.copy
X = check_array(X, accept_sparse='csr', copy=copy, ensure_2d=False,
dtype=FLOAT_DTYPES, force_all_finite="allow-nan")

if sparse.issparse(X):
if self.with_mean:
raise ValueError(
"Cannot uncenter sparse matrices: pass `with_mean=False` "
"instead See docstring for motivation and alternatives.")
if not sparse.isspmatrix_csr(X):
X = X.tocsr()
copy = False
if copy:
X = X.copy()
if self.scale_ is not None:
inplace_column_scale(X, self.scale_)
else:
X = np.asarray(X)
if copy:
X = X.copy()
if self.with_std:
X *= self.scale_
if self.with_mean:
Expand Down
20 changes: 20 additions & 0 deletions sklearn/preprocessing/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,26 @@ def test_standard_scaler_trasform_with_partial_fit(sample_weight):
)


def test_standard_check_array_of_inverse_transform():
# Check if StandardScaler inverse_transform is
# converting the integer array to float
x = np.array([
[1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 0],
[0, 8, 0, 1, 0, 0],
[1, 4, 1, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 4, 0, 1, 0, 1]], dtype=np.int32)

scaler = StandardScaler()
scaler.fit(x)

# The of inverse_transform should be converted
# to a float array.
# If not X *= self.scale_ will fail.
scaler.inverse_transform(x)


def test_min_max_scaler_iris():
X = iris.data
scaler = MinMaxScaler()
Expand Down