[英]Python How to improve numpy array performance?
I have a global numpy.array data which is a 200*200*3 3d-array containing 40000 points in the 3d-space. 我有一个全局numpy.array 数据 ,它是一个200 * 200 * 3 3d数组,在3d空间中包含40000个点。
My goal is to calculate the distance from each point to the four corners of the unit cube ((0, 0, 0),(1, 0, 0),(0, 1, 0),(0, 0, 1)),so I can determine which corner is the nearest from the it . 我的目标是计算每个点到单位立方体四个角的距离((0,0,0),(1,0,0),(0,1,0),(0,0,1) ),因此我可以确定哪个角离它最近。
def dist(*point):
return np.linalg.norm(data - np.array(rgb), axis=2)
buffer = np.stack([dist(0, 0, 0), dist(1, 0, 0), dist(0, 1, 0), dist(0, 0, 1)]).argmin(axis=0)
I wrote this piece of code and tested it, it costs me about 10ms each run . 我编写了这段代码并对其进行了测试,每次运行大约花费10毫秒。 My problem is how can I improve the performance of this piece of code ,better ran in less than 1ms . 我的问题是如何改善这段代码的性能,最好在不到1ms的时间内运行。
You could use Scipy cdist
- 您可以使用Scipy cdist
# unit cube coordinates as array
uc = np.array([[0, 0, 0],[1, 0, 0], [0, 1, 0], [0, 0, 1]])
# buffer output
buf = cdist(data.reshape(-1,3), uc).argmin(1).reshape(data.shape[0],-1)
Runtime test 运行时测试
# Original approach
def org_app():
return np.stack([dist(0, 0, 0), dist(1, 0, 0), \
dist(0, 1, 0), dist(0, 0, 1)]).argmin(axis=0)
Timings - 时间-
In [170]: data = np.random.rand(200,200,3)
In [171]: %timeit org_app()
100 loops, best of 3: 4.24 ms per loop
In [172]: %timeit cdist(data.reshape(-1,3), uc).argmin(1).reshape(data.shape[0],-1)
1000 loops, best of 3: 1.25 ms per loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.