2

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.])

1 Answer 1

3

Approach #1: You 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]: a = 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]: a
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
Sign up to request clarification or add additional context in comments.

6 Comments

That works great, thanks Divakar. One issue I have now is that I can't see how I can get it to work for the y-positions, which are a little more complex than the binned solution - I'll edit the question to show the y position problem
Case #2 doesn't quite work. I've edited the question again to be more explicit
@georussell I am sort of confused. Your Problem 2 doesn't seem like could be solved by what you had tried with your IF-ELSE code. So, this is a new problem, right?
Essentially it is - it's all part of getting an x and a y co-ordinate for each point in a 8*8 2D grid, but they look like they'll need quite different approaches as the way I was hoping to get it done doesn't work. I've changed it again to show what I was trying to code.
@georussell For that second problem, you can simply use modulus : ((a%8)-1)*0.01, with a being the input array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.