python - 将数据规范化为 [-1 and 1] ,但需要保留 0 值

我有一个 r=data 范围,既有正值也有负值。我想将其标准化到 [-1,1] 范围。

我用过 应用(lambda x: -1 + (2*((x - x.min())/(x.max() - x.min())))) 将数据从 -1 归一化为 1,但在我的数据中 0 很重要,因此我希望需要保留它。

我怎样才能完成它?

最佳答案

使用 numpy 你可以做到这一点

输入:

r =(-5,-10,0,1,17)

import numpy as np
normalized = np.where(r>0,r/r.max(),np.where(r<0,-r/r.min(),r))

输出:

归一化 = (-0.5 ,-1 ,0 ,0.0588235,1 )

我想你是在期待这样的结果

https://stackoverflow.com/questions/66687877/

相关文章:

kotlin - 在 Kotlin 中定义数组类型

c - 将 foo(int *) 作为参数传递给 X 中的 foo(void*)

python - 从具有相同键的多个字典中获取值

python - 类型错误 : input expected at most 1 argument,

Flutter - 'showSnackBar' 已弃用 - 如何更新?

python - 在 pandas 的 to_markdown() 中抑制科学记数法

c# - 解析不同的HttpClient

javascript - ("[object Promise]") 无法序列化为 JSON

git - 有没有没有版本提交的 'bump the version' 的方法?

Angular 11 - RouterModule - 'router-outlet' 不是已知元素