<h3 dir="auto">Description</h3>
<p dir="auto">The <code class="notranslate">fit_transfrom</code> function from the <code class="notranslate">sklearn.impute.KNNImputer</code> returns array that with <code class="notranslate">NaN</code> values removed instead of imputed.</p>
<p dir="auto">For example if an array X with shape (1,100) and 4 missing values is passed to fit_transform. It returns an array with size shape (1,96); The <code class="notranslate">NaN</code> values removed.</p>
<h4 dir="auto">Example :</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
import numpy as np
from sklearn.impute import KNNImputer

X = np.array([[1,2,np.nan,3,4,np.nan,5,6,np.nan]])
imp = KNNImputer()
X_imp = imp.fit_transform(X)

print(X.shape)
print(X_imp.shape)

print(X)
print(X_imp)
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">impute</span> <span class="pl-k">import</span> <span class="pl-v">KNNImputer</span>

<span class="pl-v">X</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([[<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">3</span>,<span class="pl-c1">4</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>,<span class="pl-c1">5</span>,<span class="pl-c1">6</span>,<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>]])
<span class="pl-s1">imp</span> <span class="pl-c1">=</span> <span class="pl-v">KNNImputer</span>()
<span class="pl-v">X_imp</span> <span class="pl-c1">=</span> <span class="pl-s1">imp</span>.<span class="pl-en">fit_transform</span>(<span class="pl-v">X</span>)

<span class="pl-en">print</span>(<span class="pl-v">X</span>.<span class="pl-s1">shape</span>)
<span class="pl-en">print</span>(<span class="pl-v">X_imp</span>.<span class="pl-s1">shape</span>)

<span class="pl-en">print</span>(<span class="pl-v">X</span>)
<span class="pl-en">print</span>(<span class="pl-v">X_imp</span>)</pre></div>
<h4 dir="auto">Expected Results</h4>
<p dir="auto">(1, 9)<br>
(1, 9)<br>
[[ 1.  2. nan  3.  4. nan  5.  6. nan]]<br>
[[1. 2 <em>some_value_here</em> 3. 4. <em>some_value_here</em> 5. 6. <em>some_value_here</em>]]</p>
<h4 dir="auto">Actual Results</h4>
<p dir="auto">(1, 9)<br>
(1, 6)<br>
[[ 1.  2. nan  3.  4. nan  5.  6. nan]]<br>
[[1. 2. 3. 4. 5. 6.]]</p>
<h4 dir="auto">Versions</h4>
<p dir="auto">System:<br>
python: 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]<br>
executable: C:\Users\my_name\Anaconda3\pythonw.exe<br>
machine: Windows-10-10.0.18362-SP0</p>
<p dir="auto">Python dependencies:<br>
pip: 20.0.2<br>
setuptools: 45.2.0.post20200210<br>
sklearn: 0.22.1<br>
numpy: 1.18.1<br>
scipy: 1.4.1<br>
Cython: 0.29.15<br>
pandas: 1.0.1<br>
matplotlib: 3.1.3<br>
joblib: 0.14.1</p>
<p dir="auto">Built with OpenMP: True</p>
<h3 dir="auto">Code sample</h3>
<p dir="auto">In the sample code below, a column is removed from the dataset during the pipeline</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; from sklearn.impute import SimpleImputer
&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; imp = SimpleImputer()
&gt;&gt;&gt; imp.fit([[0, np.nan], [1, np.nan]])
&gt;&gt;&gt; imp.transform([[0, np.nan], [1, 1]])
array([[0.],
       [1.]])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">impute</span> <span class="pl-k">import</span> <span class="pl-v">SimpleImputer</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">imp</span> <span class="pl-c1">=</span> <span class="pl-v">SimpleImputer</span>()
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">imp</span>.<span class="pl-en">fit</span>([[<span class="pl-c1">0</span>, <span class="pl-s1">np</span>.<span class="pl-s1">nan</span>], [<span class="pl-c1">1</span>, <span class="pl-s1">np</span>.<span class="pl-s1">nan</span>]])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">imp</span>.<span class="pl-en">transform</span>([[<span class="pl-c1">0</span>, <span class="pl-s1">np</span>.<span class="pl-s1">nan</span>], [<span class="pl-c1">1</span>, <span class="pl-c1">1</span>]])
<span class="pl-en">array</span>([[<span class="pl-c1">0.</span>],
       [<span class="pl-c1">1.</span>]])</pre></div>
<h3 dir="auto">Problem description</h3>
<p dir="auto">Currently <code class="notranslate">sklearn.impute.SimpleImputer</code> silently removes features that are <code class="notranslate">np.nan</code> on every training sample.</p>
<p dir="auto">This may cause further issues on pipelines because the dataset's <code class="notranslate">shape</code> has changed, e.g.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="dataset[:, columns_to_impute_with_median] = imp.fit_transform(dataset[:, columns_to_impute_with_median])"><pre class="notranslate"><span class="pl-s1">dataset</span>[:, <span class="pl-s1">columns_to_impute_with_median</span>] <span class="pl-c1">=</span> <span class="pl-s1">imp</span>.<span class="pl-en">fit_transform</span>(<span class="pl-s1">dataset</span>[:, <span class="pl-s1">columns_to_impute_with_median</span>])</pre></div>
<h3 dir="auto">Possible solutions</h3>
<p dir="auto">For the problematic features, either keep their values if valid or impute the <code class="notranslate">fill_value</code> during <code class="notranslate">transform</code>. I suggest adding a new parameter to trigger this behaviour with a warning highlighting the referred features.</p>
<p dir="auto">As I'm willing to implement this feature, I look forward advices.</p>