Skip to content

Commit f719d09

Browse files
committed
Merge pull request scikit-learn#2576 from ankit-maverick/issue2560
Fixing issue 2560 : Imputer bug with median and dense input
2 parents 613cf8e + 067ace1 commit f719d09

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

sklearn/preprocessing/imputation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def _dense_fit(self, X, strategy, missing_values, axis):
304304
median_masked = np.ma.median(masked_X, axis=axis)
305305
# Avoid the warning "Warning: converting a masked element to nan."
306306
median = np.ma.getdata(median_masked)
307-
median[np.ma.getmask(median_masked)] = np.nan
307+
median[np.ma.getmaskarray(median_masked)] = np.nan
308308

309309
return median
310310

sklearn/preprocessing/tests/test_imputation.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
from scipy import sparse
33

4+
from sklearn.utils.testing import assert_equal
45
from sklearn.utils.testing import assert_array_equal
56
from sklearn.utils.testing import assert_raises
67
from sklearn.utils.testing import assert_false
@@ -76,6 +77,19 @@ def _check_statistics(X, X_true,
7677
err_msg.format(1, True))
7778

7879

80+
def test_imputation_shape():
81+
"""Verify the shapes of the imputed matrix for different strategies."""
82+
X = np.random.randn(10, 2)
83+
X[::2] = np.nan
84+
85+
for strategy in ['mean', 'median', 'most_frequent']:
86+
imputer = Imputer(strategy=strategy)
87+
X_imputed = imputer.fit_transform(X)
88+
assert_equal(X_imputed.shape, (10, 2))
89+
X_imputed = imputer.fit_transform(sparse.csr_matrix(X))
90+
assert_equal(X_imputed.shape, (10, 2))
91+
92+
7993
def test_imputation_mean_median_only_zero():
8094
"""Test imputation using the mean and median strategies, when
8195
missing_values == 0."""

0 commit comments

Comments
 (0)