忽略NaN归一化在0和1之间 [英] Normalise between 0 and 1 ignoring NaN
问题描述
对于从x
到y
的可能包含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屋!