在数组中找到上下最接近的值?

时间:2013-11-14 10:14:39

标签: python arrays numpy

我有一个由[元素数,x坐标,y坐标,z坐标,半径(极坐标),θ(极坐标)]组成的数组 在这个数组中,我需要找到指定数字的两个最接近的值,上面的一个和下面的一个。 这需要在保持θ值的数组的最后一列中找到。

值的范围为0 - 1.5707弧度(0 - 90度),在我们的例子中,我们希望能够选择我们想要的指定数量

number=9 
Anglestep = math.pi/2 / number 
Anglerange = np.arange(0,math.pi/2+anglestep,anglestep) #math.pi/2+anglestep so that we get math.pi/2 in   the array

例如,我需要在指定值之上和之下找到两个值:“0.17”

[...['4549', '4.2158604', '49.4799309', '0.0833661', 49.65920902290997, 0.0849981532744405],
 ['4535', '4.2867651', '49.4913025', '0.0813997', 49.67660795755971, 0.08640089283783374],
 ['4537', '5.6042995', '49.4534569', '0.0811241', 49.7699967073121, 0.11284330708918186],
 ['4538', '6.2840257', '49.4676971', '0.0809942', 49.86523874780516, 0.12635612935285648],
 ['4539', '6.9654546', '49.4909363', '0.0814121', 49.97869879894153, 0.13982362821749783],
 ['4540', '7.6476088', '49.5210190', '0.0813955', 50.10805567128103, 0.1532211602749019],
 ['4541', '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243],
 ['4542', '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624],
 ['4529', '9.3985014', '49.6320610', '0.0812080', 50.51409018950577, 0.18714756393388338],
 ['4531', '10.3884563', '49.7157669', '0.0812043', 50.78954127329902, 0.2059930152826599]..]

所以我想要的输出在这种情况下是两个值:(0.16651831290560243,0.17975113416156624)

2 个答案:

答案 0 :(得分:1)

In [30]: np.max(arr[arr < .17])
Out[30]: 0.16651831290560243

In [31]: np.min(arr[arr > .17])
Out[31]: 0.17975113416156624

答案 1 :(得分:1)

@NPE's answer对于1d数组是正确的,但您必须首先访问数组的Angle列。这取决于数组的dtype(数据类型)(您的数组似乎包括字符串和浮点数,这对于numpy数组是不允许的)。有两种方法可以解决,一种是通过使用所有浮点数,另一种是使用结构化的dtype:

所有花车

arr = np.array([
    ['4549',  '4.2158604', '49.4799309', '0.0833661', 49.65920902290997, 0.0849981532744405 ],
    ['4535',  '4.2867651', '49.4913025', '0.0813997', 49.67660795755971, 0.08640089283783374],
    ['4537',  '5.6042995', '49.4534569', '0.0811241', 49.7699967073121 , 0.11284330708918186],
    ['4538',  '6.2840257', '49.4676971', '0.0809942', 49.86523874780516, 0.12635612935285648],
    ['4539',  '6.9654546', '49.4909363', '0.0814121', 49.97869879894153, 0.13982362821749783],
    ['4540',  '7.6476088', '49.5210190', '0.0813955', 50.10805567128103, 0.1532211602749019 ],
    ['4541',  '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243],
    ['4542',  '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624],
    ['4529',  '9.3985014', '49.6320610', '0.0812080', 50.51409018950577, 0.18714756393388338],
    ['4531', '10.3884563', '49.7157669', '0.0812043', 50.78954127329902, 0.2059930152826599 ]], dtype=float)

然后,要应用@Jaime's method,请使用

i = np.searchsorted(arr[:, -1], 0.17)
below = arr[i-1]
above = arr[i]

below
# array([  4.54100000e+03,   8.32986550e+00,   4.95605049e+01,   8.12513000e-02,   5.02556495e+01,   1.66518313e-01])
above
# array([  4.54200000e+03,   9.01412110e+00,   4.96065178e+01,   8.11457000e-02,   5.04188555e+01,   1.79751134e-01])

如果您只想要角度,那么也可以逐列切割:

below_ang = arr[i-1, -1]
above_ang = arr[i, -1]

below_ang, above_ang
#(0.166518313, 0.179751134)

请注意,这假定arr按角度排序。

结构化数组:

arr = array([ ('4549',  '4.2158604', '49.4799309', '0.0833661', 49.65920902290997, 0.0849981532744405 ),
              ('4535',  '4.2867651', '49.4913025', '0.0813997', 49.67660795755971, 0.08640089283783374),
              ('4537',  '5.6042995', '49.4534569', '0.0811241', 49.7699967073121 , 0.11284330708918186),
              ('4538',  '6.2840257', '49.4676971', '0.0809942', 49.86523874780516, 0.12635612935285648),
              ('4539',  '6.9654546', '49.4909363', '0.0814121', 49.97869879894153, 0.13982362821749783),
              ('4540',  '7.6476088', '49.5210190', '0.0813955', 50.10805567128103, 0.1532211602749019 ),
              ('4541',  '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243),
              ('4542',  '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624),
              ('4529',  '9.3985014', '49.6320610', '0.0812080', 50.51409018950577, 0.18714756393388338),
              ('4531', '10.3884563', '49.7157669', '0.0812043', 50.78954127329902, 0.2059930152826599)], 
             dtype=[('id', 'S4'), ('x', 'S10'), ('y', 'S10'), ('z', 'S9'), ('rad', '<f8'), ('ang', '<f8')])

i = np.searchsorted(arr['ang'], 0.17)
below = arr[i-1]
above = arr[i]

below
# ('4541', '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243)
above
# ('4542', '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624)

为多个值执行

首先,设置范围的更简单方法是linspace,它自动包含开始和结束,并由数组长度指定,而不是步长。而不是:

number=9 
anglestep = math.pi/2 / number 
anglerange = np.arange(0,math.pi/2+anglestep,anglestep) #math.pi/2+anglestep so that we get math.pi/2 in the array

使用

number = 9
anglerange = np.linspace(0, math.pi/2, number) # start, end, number

现在,searchsorted实际上会为您轻松找到几个要点:

locs = np.searchsorted(arr['ang'], anglerange)
belows = arr['ang'][locs-1]
aboves = arr['ang'][locs]

例如,我会设置anglerange = [0.1, 0.17, 0.2],因为整个范围不在您的示例数据中:

belows
# array([ 0.08640089,  0.16651831,  0.18714756])
aboves
# array([ 0.11284331,  0.17975113,  0.20599302])