简体   繁体   English

分别归一化正负值

[英]Normalize positive and negative values separately

I have a matrix where I want the positive values to be normalized by dividing by the max positive value such that the maximum value is 1, and the negative values to be normalized by the min negative value such that the most negative value is -1.我有一个矩阵,我希望正值通过除以最大正值来归一化,使得最大值为 1,负值被最小负值归一化,使得最大负值为 -1。 For example,例如,

[[   1.    4. -100.]
 [   3.   10.   -8.]]

becomes变成

[[ 0.1 0.4 -1. [[ 0.1 0.4 -1。 ] [ 0.3 1. -0.08]]. ] [ 0.3 1. -0.08]]。

I tried我试过了

def sym_min_max_norm(mat):
    res = np.divide(mat, np.max(mat), where = mat > 0)
    res = np.divide(res, -np.min(res), where = res < 0)
    return res

but this doesn't seem to work.但这似乎不起作用。 Maybe I'm using the where condition wrong in np.divide?也许我在 np.divide 中使用了错误的 where 条件?

Assuming that the source array is a , you can get the result running:假设源数组是a ,您可以运行结果:

result = np.where(a >= 0, a/np.max(a), -a/np.min(a))

Just a single-liner.只是一个单线。

The following code gives me the correct result, but I'm not sure why the original solution didn't work.下面的代码给了我正确的结果,但我不确定为什么原来的解决方案不起作用。

def sym_min_max_norm(mt):
    pos = mt > 0
    neg = mt < 0
    res = mt * (pos/np.max(mt) - neg/np.min(mt))
    return res

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM