arrays - 将 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 数组数组的最快方法是什么?
解决方案
最初的解决方案速度惊人,但可以更快地完成。您可以将字符串连接到一个大缓冲区并通过一次调用来处理它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
。
推荐阅读
- request - 将图像从发布请求上传到云功能
- javascript - 打印时如何使用 JavaScript 或 CSS 在页脚中添加页码?
- apache - Websocket 连接失败:在收到握手响应之前连接已关闭
- ios - 着色器中的优化
- database - sqlite:导入文件不能存入内存
- angular - 在 Angular 中,如何使自定义组件具有应用了简单标志的 [(ngModel)]?
- javascript - 如何禁用按钮 - HTML/Javascript
- docker - 通过 docker 镜像运行时,Webpack 站点不会显示
- python - 有什么方法可以使用 python 将 Pdf 文件转换为 Docx
- ios - 从 Mac 读取 iPhone 日志文件内容