比循环创建numpy数组更快的替代方法

时间:2018-08-16 13:22:45

标签: numpy

我有一个包含点云的数组(大约100个ladar点)。我需要尽快创建一组numpy数组。

sweep = np.empty(
    shape=(len(sweep.points),),
    dtype=[
        ('point', np.float64, 3),
        ('intensity', np.float32),
        ## ..... more fields ....
    ]
)

for index, point in enumerate(sweep.points):
        sweep[index]['point'] = (point.x, point.y, point.z)
        sweep[index]['intensity'] = point.intensity
        ## ....more fields...

编写显式循环非常低效且缓慢。有没有更好的方法可以解决这个问题?

1 个答案:

答案 0 :(得分:2)

使用列表推导来格式化数据并将其直接传递给numpy数组要快一些:

np.array([((point.x, point.y, point.z), point.intensity)
          for point in points],
         dtype=[('point', np.float64, 3),
                ('intensity', np.float32)])
相关问题