Skip to content
Advertisement

positive weights and negative weights [closed]

I have an array named as weights –

array([-0.11387602,  0.07744348, -0.25294052,  0.06635546,  0.11452402,
    0.02732373, -0.09838084, -0.06822771,  0.06360016,  0.36889096,
    0.0232971 , -0.2113415 ,  0.38389924, -0.21285646, -0.03606946,
    0.03405694, -0.16547289,  0.07334644,  0.057695  ,  0.05680166,
   -0.14985729, -0.1537674 ,  0.06515446,  0.13564594, -0.07660504,
   -0.22963317, -0.27253672, -0.14489202, -0.05152282, -0.0773859 ,
    0.01486651, -0.10037556, -0.0162965 , -0.01810175, -0.09474796,
    0.35913295, -0.290787  ])

I am trying for hours to get an array where the output is normalized for positives and for negative. LOGIC – positive number divide by all positive numbers in the array AND negative number divide by sum of all negative numbers in the array.

in the end the output of the operation should show positives weight (which adds to 1) and negative weights (which adds to -1).

Please help :)

PS: using python

Advertisement

Answer

you can try:

pos = np.where(x>=0,x,0).sum(0)
neg = np.where(x<0,x,0).sum(0)

y = np.where(x>=0, x/pos, -x/neg)

OR

y = np.where(x>=0, x/x[x>=0].sum(), -x/x[x<0].sum())

y:

array([-0.04015835,  0.04029246, -0.08919942,  0.03452356,  0.0595848 ,
        0.01421605, -0.03469398, -0.02406049,  0.03309003,  0.19192738,
        0.01212107, -0.07452953,  0.19973592, -0.07506378, -0.01271989,
        0.01771922, -0.05835398,  0.03816084,  0.03001768,  0.02955289,
       -0.05284714, -0.05422604,  0.0338987 ,  0.07057416, -0.02701475,
       -0.08098009, -0.09611001, -0.05109614, -0.01816951, -0.02729012,
        0.00773478, -0.03539742, -0.00574696, -0.00638358, -0.03341285,
        0.18685046, -0.10254597])

y[y>=0].sum() —> 0.9999999999999999

y[y<0].sum() —> -1.0000000000000002

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement