python-基于布尔逻辑创建数组

问题1

我有一个numpy数组

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

每个data [:,1:5]都指2D网格上的一个位置,总共有64个位置.我正在尝试定义一个给出每个位置的x位置的函数.

我想要的是有一种情况

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

我希望将那些返回的值放入一个数组中,以便以后可以在np.hstack中使用数据.

我正在测试

data[:,1]
Out[103]: array([  1.,   1.,   3.,   4.,  57.,  57.,  59.,  60.])

因此,对于此数组,它应生成数组:

[[0.0, 0.0, 0.0, 0.0, 0.07, 0.07, 0.07, 0.07]]

我试过了:

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

这使:

xcol1
Out[104]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

它不会产生我在新数组中需要的值.有人知道我在做什么错吗?

编辑:

问题2

第二个问题是y位置问题,它不能很好地适合顺序分档.

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

所以

data[:,1]
Out[103]: array([  1.,   1.,   3.,   4.,  57.,  57.,  59.,  60.])

在这种情况下应该返回

[[0.0, 0.0, 0.02, 0.03, 0.0, 0.0, 0.02, 0.03]]

我希望这样做:

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

但这又返回零数组

ya
Out[119]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
最佳答案
方法1:您可以使用np.digitize

bins = np.arange(8,64,8)
out = np.digitize(a, bins, right=True)*0.01

样品运行-

情况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])

案例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])

方法2:或者,使用np.searchsorted

np.searchsorted(bins, a)*0.01

转载注明原文:python-基于布尔逻辑创建数组 - 代码日志