忽略NaN归一化在0和1之间 [英] Normalise between 0 and 1 ignoring NaN

查看:118
本文介绍了忽略NaN归一化在0和1之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于从xy的可能包含NaN的数字​​列表,我如何在0和1之间进行归一化,而忽略NaN值(它们保持为NaN).

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

通常,我会使用MinMaxScaler((a >参考页),但无法处理NaN,建议根据均值或中位数等推算值.它不提供忽略所有NaN值的选项. /p>

Typically I would use MinMaxScaler (ref page) from sklearn.preprocessing, but this cannot handle NaN and recommends imputing the values based on mean or median etc. it doesn't offer the option to ignore all the NaN values.

推荐答案

考虑pd.Series s

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

选项1
最小最大缩放比例

Option 1
Min Max Scaling

new = s.sub(s.min()).div((s.max() - s.min()))
new.hist()

没什么要求
我把它们放进去是因为我想

NOT WHAT OP ASKED FOR
I put these in because I wanted to

选项2
乙状结肠

Option 2
sigmoid

sigmoid = lambda x: 1 / (1 + np.exp(-x))

new = sigmoid(s.sub(s.mean()))
new.hist()

选项3
tanh(双曲正切)

Option 3
tanh (hyperbolic tangent)

new = np.tanh(s.sub(s.mean())).add(1).div(2)
new.hist()

这篇关于忽略NaN归一化在0和1之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆