Problem 1
I have a numpy array
data[:,0:5]
Out[98]:
array([[ 1.00200300e+09, 1.00000000e+00, 2.00000000e+00,
3.00000000e+00, 4.00000000e+00],
[ 1.00200400e+09, 1.00000000e+00, 2.00000000e+00,
4.00000000e+00, 5.00000000e+00],
[ 1.00200300e+09, 3.00000000e+00, 4.00000000e+00,
1.00000000e+00, 2.00000000e+00],
[ 1.00200400e+09, 4.00000000e+00, 5.00000000e+00,
1.00000000e+00, 2.00000000e+00],
[ 5.70580591e+10, 5.70000000e+01, 5.80000000e+01,
5.90000000e+01, 6.00000000e+01],
[ 5.70580601e+10, 5.70000000e+01, 5.80000000e+01,
6.00000000e+01, 6.10000000e+01],
[ 5.70580591e+10, 5.90000000e+01, 6.00000000e+01,
5.70000000e+01, 5.80000000e+01],
[ 5.70580601e+10, 6.00000000e+01, 6.10000000e+01,
5.80000000e+01, 5.70000000e+01]])
Each of data[:,1:5] refers to a position on a 2D grid, of which there are 64 positions in total. I'm trying to define a function that gives the x positions of each position.
What I want is to have a situation where
0 < i <= 8 returns 0.00
8 < i <= 16 returns 0.01
16 < i <= 24 returns 0.02
24 < i <= 32 returns 0.03
32 < i <= 40 returns 0.04
40 < i <= 48 returns 0.05
48 < i <= 56 returns 0.06
56 < i <= 64 returns 0.07
and I want those returned values to be put in an array that I can later np.hstack with data.
I'm testing it on
data[:,1]
Out[103]: array([ 1., 1., 3., 4., 57., 57., 59., 60.])
so for this array it should produce the array:
[[0.0, 0.0, 0.0, 0.0, 0.07, 0.07, 0.07, 0.07]]
I've tried:
pos = np.empty([len(data),])
def xpos(col):
for i in col:
if i <= 8:
np.append(pos, [0.0])
if 8 < i <= 16:
np.append(pos, [1.0e-02])
if 16 < i <= 24:
np.append(pos, [2.0e-02])
if 24 < i <= 32:
np.append(pos, [3.0e-02])
if 32 < i <= 40:
np.append(pos, [4.0e-02])
if 40 < i <= 48:
np.append(pos, [5.0e-02])
if 48 < i <= 56:
np.append(pos, [6.0e-02])
else:
np.append(pos, [7.0e-02])
return pos
xcol1 = xpos(data[:,1])
Which gives:
xcol1
Out[104]: array([ 0., 0., 0., 0., 0., 0., 0., 0.])
It doesn't produce the values that I'm after in the new array. Does anyone know what I'm doing wrong?
EDIT:
Problem 2
The secondary problem is the y-position problem, which doesn't fit nicely into sequential bins.
i == 1 or 9 or 17 or 25 or 33 or 41 or 49 or 57 returns 0.00
i == 2 or 10 or 18 or 26 or 34 or 42 or 50 or 58 returns 0.01
i == 3 or 11 or 19 or 27 or 35 or 43 or 51 or 59 returns 0.02
i == 4 or 12 or 20 or 28 or 36 or 44 or 52 or 60 returns 0.03
i == 5 or 13 or 21 or 29 or 37 or 45 or 53 or 61 returns 0.04
i == 6 or 14 or 22 or 30 or 38 or 46 or 54 or 62 returns 0.05
i == 7 or 15 or 23 or 31 or 39 or 47 or 55 or 63 returns 0.06
i == 8 or 16 or 24 or 32 or 40 or 48 or 56 or 64 returns 0.07
so
data[:,1]
Out[103]: array([ 1., 1., 3., 4., 57., 57., 59., 60.])
should in this case return
[[0.0, 0.0, 0.02, 0.03, 0.0, 0.0, 0.02, 0.03]]
I was hoping to do this with:
pos = np.empty([len(data),])
def ypos(col):
for i in col:
if i == 1 or i == 9 or i == 17 or i == 25 or i == 33 or i == 41 or i == 49 or i == 57:
np.append(pos, [0.0])
if i == 2 or i == 10 or i == 18 or i == 26 or i == 34 or i == 42 or i == 50 or i == 58:
np.append(pos, [1.0e-02])
if i == 3 or i == 11 or i == 19 or i == 27 or i == 35 or i == 43 or i == 51 or i == 59:
np.append(pos, [2.0e-02])
if i == 4 or i == 12 or i == 20 or i == 28 or i == 36 or i == 44 or i == 52 or i == 60:
np.append(pos, [3.0e-02])
if i == 5 or i == 13 or i == 21 or i == 29 or i == 37 or i == 45 or i == 53 or i == 61:
np.append(pos, [4.0e-02])
if i == 6 or i == 14 or i == 22 or i == 30 or i == 38 or i == 46 or i == 54 or i == 62:
np.append(pos, [5.0e-02])
if i == 7 or i == 15 or i == 23 or i == 31 or i == 39 or i == 47 or i == 55 or i == 63:
np.append(pos, [6.0e-02])
else:
np.append(pos, [7.0e-02])
return pos
ya = ypos(data[:,1])
but that returns an array of zeroes again
ya
Out[119]: array([ 0., 0., 0., 0., 0., 0., 0., 0.])
Approach #1: You can use
np.digitize
-Sample runs -
Case #1 :
Case #2 :
Approach #2: Alternatively, using
np.searchsorted
-