Reputation: 2478
I am using Python 3.8.5 and numpy 1.18.5. I have, an example of two 5 x 3 np arrays as follows:
x
'''
array([[ 0.61671909, -3.28907688, 1.47162381],
[-0.52380408, -0.87605603, 2.09490614],
[-1.12907555, 0.00810023, 0.41001445],
[-1.12316108, -0.93468271, -0.41353168],
[-1.14203546, 0.0319224 , 1.04925758]])
'''
y
'''
array([[-0.19258918, 0.24449053, -1.16664036],
[-0.00495552, -1.12518086, 0.30846267],
[-1.09738445, 1.96309078, -0.67126134],
[-0.93575175, -0.17778996, 3.15546178],
[ 0.73158413, 1.09391779, 0.73405069]])
'''
I want to keep track of how many of the individual (element-wise) numbers change sign from 'x' to 'y'. For example, x[0][0] changes from positive to negative in the corresponding y[0][0].
I want to compute some statistics such as: "how many of the positive numbers changed to negative from x to y", "how many of the negative numbers changed to positive from x to y", etc.
How can I achieve this?
Upvotes: 0
Views: 173
Reputation: 150735
Use sign
function:
np.sign(x) != np.sign(y)
If you want the coordinates, you can wrap that around np.where
or np.nonzero
np.where(np.sign(x)!=np.sign(y))
If you want the total number, just sum
:
np.sum(np.sign(x) != np.sign(y))
# out 6
Update for the updated question:
signs = np.sign(x) - np.sign(y)
neg2pos = np.sum( signs == -2 )
pos2neg = np.sum( signs == 2 )
Upvotes: 1