python - Create array based on boolean logic -
problem 1
i have 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 position on 2d grid, of there 64 positions in total. i'm trying define function gives x positions of each position.
what want have situation where
0 < <= 8 returns 0.00 8 < <= 16  returns 0.01 16 < <= 24 returns 0.02 24 < <= 32 returns 0.03 32 < <= 40 returns 0.04 40 < <= 48 returns 0.05 48 < <= 56 returns 0.06 56 < <= 64 returns 0.07   and want returned values put in array can later np.hstack data.
i'm testing on
data[:,1] out[103]: array([  1.,   1.,   3.,   4.,  57.,  57.,  59.,  60.])   so array should produce 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):     in col:         if <= 8:             np.append(pos, [0.0])         if 8 < <= 16:             np.append(pos, [1.0e-02])         if 16 < <= 24:             np.append(pos, [2.0e-02])         if 24 < <= 32:             np.append(pos, [3.0e-02])         if 32 < <= 40:             np.append(pos, [4.0e-02])         if 40 < <= 48:             np.append(pos, [5.0e-02])         if 48 < <= 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 values i'm after in new array. know i'm doing wrong?
edit:
problem 2
the secondary problem y-position problem, doesn't fit nicely sequential bins.
i == 1 or 9 or 17 or 25 or 33 or 41 or 49 or 57 returns 0.00 == 2 or 10 or 18 or 26 or 34 or 42 or 50 or 58 returns 0.01 == 3 or 11 or 19 or 27 or 35 or 43 or 51 or 59 returns 0.02 == 4 or 12 or 20 or 28 or 36 or 44 or 52 or 60 returns 0.03 == 5 or 13 or 21 or 29 or 37 or 45 or 53 or 61 returns 0.04 == 6 or 14 or 22 or 30 or 38 or 46 or 54 or 62 returns 0.05 == 7 or 15 or 23 or 31 or 39 or 47 or 55 or 63 returns 0.06 == 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 case return
[[0.0, 0.0, 0.02, 0.03, 0.0, 0.0, 0.02, 0.03]]   i hoping with:
pos = np.empty([len(data),]) def ypos(col):     in col:         if == 1 or == 9 or == 17 or == 25 or == 33 or == 41 or == 49 or == 57:             np.append(pos, [0.0])         if == 2 or == 10 or == 18 or == 26 or == 34 or == 42 or == 50 or == 58:             np.append(pos, [1.0e-02])         if == 3 or == 11 or == 19 or == 27 or == 35 or == 43 or == 51 or == 59:             np.append(pos, [2.0e-02])         if == 4 or == 12 or == 20 or == 28 or == 36 or == 44 or == 52 or == 60:             np.append(pos, [3.0e-02])         if == 5 or == 13 or == 21 or == 29 or == 37 or == 45 or == 53 or == 61:             np.append(pos, [4.0e-02])         if == 6 or == 14 or == 22 or == 30 or == 38 or == 46 or == 54 or == 62:             np.append(pos, [5.0e-02])         if == 7 or == 15 or == 23 or == 31 or == 39 or == 47 or == 55 or == 63:             np.append(pos, [6.0e-02])         else:             np.append(pos, [7.0e-02])         return pos  ya = ypos(data[:,1])   but returns array of zeroes again
ya out[119]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])      
approach #1: can use np.digitize -
bins = np.arange(8,64,8) out = np.digitize(a, bins, right=true)*0.01   sample runs -
case #1 :
in [150]: = np.array([  1.,   1.,   3.,   4.,  57.,  57.,  59.,  60.])  in [151]: bins = np.arange(8,64,8)  in [152]: np.digitize(a, bins, right=true)*0.01 out[152]: array([ 0.  ,  0.  ,  0.  ,  0.  ,  0.07,  0.07,  0.07,  0.07])   case #2 :
in [156]: out[156]: array([ -5.,   1.,   3.,   4.,  56.,  57.,  59.,  65.])  in [157]: np.digitize(a, bins, right=true)*0.01 out[157]: array([ 0.  ,  0.  ,  0.  ,  0.  ,  0.06,  0.07,  0.07,  0.07])   approach #2: alternatively, using np.searchsorted -
np.searchsorted(bins, a)*0.01      wiki
Comments
Post a Comment