python - 在 0 和 1 之间进行归一化,忽略 NaN

标签 python pandas numpy scikit-learn

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

通常我会使用 sklearn.preprocessing 中的 MinMaxScaler (ref page),但这不能处理 NaN 并建议根据在平均值或中位数等方面,它不提供忽略所有 NaN 值的选项。

最佳答案

考虑 pd.Series s

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

enter image description here


选项 1
最小最大缩放

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

enter image description here


不是 OP 要求的内容
我把这些放进去是因为我想

选项 2
乙状结肠

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

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

enter image description here


选项 3
tanh(双曲正切)

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

enter image description here

关于python - 在 0 和 1 之间进行归一化,忽略 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39758449/

相关文章:

python - Pandas to_csv : suppress scientific notation in csv file when writing pandas to csv

python - 当表无法返回值时,如何抓取表? (美汤)

python - 将列表转换为数据框/调整一行循环

python - 将 NumPy np.uint8 数组列表转换为 np.unicode_ 数组

python - 如何在 if 语句中收集元组值?

python - C相当于Python的struct.pack?

python - 如何复制 Django 模型实例和所有相关数据

python - 正交化矩阵 numpy

python - 迭代列并在达到特定数字时返回列名称

Python ndarray 具有不同类型的元素