Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy矢量化小于/大于比较_Python_Numpy - Fatal编程技术网

Python Numpy矢量化小于/大于比较

Python Numpy矢量化小于/大于比较,python,numpy,Python,Numpy,我有一些代码来匹配角度和它们所在圆的象限。它目前给了我想要的结果,但我试图失去for循环,以充分利用numpy的速度 import numpy as np angle = np.array([350, 10, 80, 100, 170, 190, 260, 280]) # Center of each quadrant spawn_angles = np.array([0, 90, 180, 270]) segment_degrees = np.diff(spawn_angles)[0] l

我有一些代码来匹配角度和它们所在圆的象限。它目前给了我想要的结果,但我试图失去for循环,以充分利用numpy的速度

import numpy as np

angle = np.array([350, 10, 80, 100, 170, 190, 260, 280])
# Center of each quadrant
spawn_angles = np.array([0, 90, 180, 270])

segment_degrees = np.diff(spawn_angles)[0]
lower_bounds = spawn_angles - (segment_degrees / 2)
upper_bounds = spawn_angles + (segment_degrees / 2)
max_upper = upper_bounds.max()
# Wrap angles larger than the upper bound of the last segment
# back to a negative angle
angle[angle > max_upper] -= 360
quadrant = np.zeros_like(angle, dtype=np.float64)
# Want to make sure that quadrants that don't get calculated
# properly get assigned an invalid number, i.e. -1
quadrant.fill(-1)
for segment_num in range(len(spawn_angles)):
    in_segment = ((angle > lower_bounds[segment_num]) & 
                  (angle < upper_bounds[segment_num]))
    quadrant[in_segment] = segment_num

# Expected/current output
quadrant
Out[16]: array([ 0.,  0.,  1.,  1.,  2.,  2.,  3.,  3.])

基本上,我不知道如何在numpy中实现的部分是>/,您需要将所有内容提升到一个维度。您需要一个二维数组,每个角度作为一行,每个线段作为一列。或者你想要转置,但是如果是的话,你应该可以从这里找到答案

如果只做a>b,其中a和b都是1D数组,则要求进行1对1的元素比较

但如果a是2D数组,则需要笛卡尔乘积比较

换言之:

>>> array.reshape((8,1)) > lower_bounds
array([[ True,  True,  True,  True],
       [ True, False, False, False],
       [ True,  True, False, False],
       [ True,  True, False, False],
       [ True,  True,  True, False],
       [ True,  True,  True, False],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)

你应该能够从那里找到答案。

感谢abarnert提供了这里的关键见解。我的返工矢量化代码:

import numpy as np

angle = np.array([350, 10, 80, 100, 170, 190, 260, 280])
# Center of each quadrant
spawn_angles = np.array([0, 90, 180, 270])

segment_degrees = np.diff(spawn_angles)[0]
lower_bounds = spawn_angles - (segment_degrees / 2)
upper_bounds = spawn_angles + (segment_degrees / 2)
max_upper = upper_bounds.max()
# Wrap angles larger than the upper bound of the last segment
# back to a negative angle
angle[angle > max_upper] -= 360
angle_2d = angle.reshape((len(angle), 1))
cmp_array = ((angle_2d > lower_bounds) & 
             (angle_2d < upper_bounds))
quadrant = np.argwhere(cmp_array)[:, 1]
quadrant
Out[29]: array([0, 0, 1, 1, 2, 2, 3, 3], dtype=int64)

很好,明白了,谢谢你朝着正确的方向轻推。我将很快发布我的重新编写的代码作为解决方案,如果您注意到任何明显的错误,请告诉我。仅供将来参考,您可以将angle_2d=angle.ReformElenangle,1重写为angle_2d=angle.Reformate-1,1