Search code examples
pythonarraysnumpygoogle-colaboratory

Numpy different elements between two arrays


I need help. I have this array from a table:

lycop = np.array([[14, 15, 16, 17, 18, 19, 20, 21, 22, 23], 
                 [1, 1, 8, 24, 48, 58, 35, 16, 8, 1]])
lycop

array([[14, 15, 16, 17, 18, 19, 20, 21, 22, 23],

       [ 1,  1,  8, 24, 48, 58, 35, 16,  8,  1]])

I had to calculate media, var and std.

lycop_media = (lycop[0]*lycop[1]).sum()/lycop[1].sum() 
lycop_media

18.83

lycop_std = lycop_var**1/2 
lycop_std

1.09555

so far so good... Then I need 3 different arrays:

a1 : values under the media - std 𝑑<πœ‡π‘‘βˆ’πœŽπ‘‘

a1 = lycop[0][(lycop[0] < (lycop_media - lycop_std))]
a1
array([14, 15, 16, 17])

a2 : values over media + std 𝑑>πœ‡π‘‘+πœŽπ‘‘

a2 = lycop[0][(lycop[0] > (lycop_media + lycop_std))]
a2
array([20, 21, 22, 23])

a3 : values between both a1 and a2 so πœ‡π‘‘βˆ’πœŽπ‘‘<𝑑<πœ‡π‘‘+πœŽπ‘‘

and here I just lost it. My attempts:

a3 = [np.where((lycop[0] < lycop_media - lycop_std) & (lycop[0] < lycop_media + lycop_std))]
a3

[(array([0, 1, 2, 3]),)]

which is obviously wrong.

a3= [((lycop_media - lycop_std) < 18 & 19 < (lycop_media + lycop_std))] 
a3

[True]

I know is true but I need to get and array with the numbers, something like

array([18, 19])

Some ideas?


Solution

  • It's just an extension of what you did for a1 and a2, but you have the < and > signs confused for one half.

    a3 = lycop[0](lycop[0] >= lycop_media - lycop_std) & (lycop[0] <= lycop_media + lycop_std)]