Normalise between 0 and 1 ignoring NaN
<p>For a list of numbers ranging from <code>x</code> to <code>y</code> that may contain <code>NaN</code>, how can I normalise between 0 and 1, ignoring the <code>NaN</code> values (they stay as <code>NaN</code>).</p>

<p>Typically I would use <code>MinMaxScaler</code> (<a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html#sklearn.preprocessing.MinMaxScaler" rel="nofollow">ref page</a>) from <code>sklearn.preprocessing</code>, but this cannot handle <code>NaN</code> and recommends imputing the values based on mean or median etc. it doesn't offer the option to ignore all the <code>NaN</code> values.</p>

<p>consider <code>pd.Series</code> <code>s</code></p>

<pre><code>s = pd.Series(np.random.choice([3, 4, 5, 6, np.nan], 100))
s.hist()
</code></pre>

<p><a href="http://i.stack.imgur.com/hzlrF.png" rel="nofollow"><img src="http://i.stack.imgur.com/hzlrF.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>Option 1</em></strong><br>
Min Max Scaling</p>

<pre><code>new = s.sub(s.min()).div((s.max() - s.min()))
new.hist()
</code></pre>

<p><a href="http://i.stack.imgur.com/ThfOU.png" rel="nofollow"><img src="http://i.stack.imgur.com/ThfOU.png" alt="enter image description here"></a></p>

<hr>

<p><strong>NOT WHAT OP ASKED FOR</strong><br>
I put these in because I wanted to</p>

<p><strong><em>Option 2</em></strong><br>
sigmoid</p>

<pre><code>sigmoid = lambda x: 1 / (1 + np.exp(-x))

new = sigmoid(s.sub(s.mean()))
new.hist()
</code></pre>

<p><a href="http://i.stack.imgur.com/W4nZv.png" rel="nofollow"><img src="http://i.stack.imgur.com/W4nZv.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>Option 3</em></strong><br>
tanh (hyperbolic tangent)</p>

<pre><code>new = np.tanh(s.sub(s.mean())).add(1).div(2)
new.hist()
</code></pre>

<p><a href="http://i.stack.imgur.com/7Q0WT.png" rel="nofollow"><img src="http://i.stack.imgur.com/7Q0WT.png" alt="enter image description here"></a></p>

python pandas numpy scikit-learn