首页 > 解决方案 > 将 3D 坐标从字符串转换为 numpy 数组中的浮点数的最快方法

问题描述

我在一个列表中有 3D 坐标(字符串),我想将其转换为浮点数组。

# current list
iPoints = ['-50.0651394154927,-5.3133315588409,0', '-48.7824404616692,3.1894817418136,0', '-46.2317402190515,11.3986203175639,0']

# ideal output
array([[-50.0651394154927,-5.3133315588409,0], [-48.7824404616692,3.1894817418136,0], [-46.2317402190515,11.3986203175639,0]])

一个天真的实现:

iPoints = np.array([[float(c) for c in v.split(',')] for v in iPoints])

将此字符串列表转换为 numpy 数组数组的最快方法是什么?

标签: arrayspython-2.7numpytype-conversion

解决方案


最初的解决方案速度惊人,但可以更快地完成。您可以将字符串连接到一个大缓冲区并通过一次调用来处理它np.fromstring

试试下面的代码:

# put everthing to a buffer as a large 1D-array separated with commas
buf = ','.join(iPoints)
# parse the buffer
iPoints = np.fromstring(buf, sep=',', dtype=float, count=3*len(iPoints))
# make it 3d again
iPoints = iPoints.reshape(-1,3)

我做了一些基准测试。

iPoints=['-50.0651394154927,-5.3133315588409,0', '-48.7824404616692,3.1894817418136,0', '-46.2317402190515,11.3986203175639,0']
# lets make it a little large
iMorePoints = iPoints * 10000

method1 = lambda: np.array([[float(c) for c in v.split(',')] for v in iMorePoints])
method2 = lambda: np.fromstring(','.join(iMorePoints), sep=',', dtype=float, count=3*len(iMorePoints)).reshape(-1,3)

我的机器上的结果是:

>>> timeit(method1, number=100)
3.6391940720000093
>>> timeit(method2, number=100)
1.0472392480000963

所以提出的解决方案要快 3.5 倍。一个小缺点是必须事先知道向量是 3 维的。但可以通过 call 进行检查iPoints[0].count(',')+1


推荐阅读