numpy数组转换中的性能改进 [英] Performance improvement in numpy array transformation
问题描述
给出三个 numpy
一维数组,我想按如下方式转换它们:
Given three numpy
1D arrays, I want to transform them as follows:
import numpy as np
Xd = np.asarray([0, 0, 1, 1, 0.5])
Yd = np.asarray([0, 0, 0, 2.5, 2.5])
Zd = np.asarray([0, 1.5, 1.5, 1.5, 1.5])
points = np.stack([Xd, Yd, Zd], axis=1).reshape(-1, 1, 3)
segments = np.concatenate([points[:-1], points[1:]], axis = 1)
print(segments.shape)
print(segments)
输出:
(4, 2, 3)
[[[0. 0. 0. ]
[0. 0. 1.5]]
[[0. 0. 1.5]
[1. 0. 1.5]]
[[1. 0. 1.5]
[1. 2.5 1.5]]
[[1. 2.5 1.5]
[0.5 2.5 1.5]]]
有没有办法提高这种转换的性能?
Is there a way to improve the performance of this transformation?
背景
此转换对于将 matplotlib
中的 XYZ
坐标与数千个坐标或插值数据以获得更好的分辨率,优化方法是必要的.
This transformation is necessary to use the XYZ
coordinates in matplotlib
with Line3DCollection
. So far, I have only seen variations of the above code but with thousands of coordinates or interpolated data for better resolution, an optimized approach is necessary.
摘要
由于 @Mercury ,可以得出结论,对于较短的数组(长度小于1k),
Thanks to @Mercury, it can be concluded that for shorter arrays (<1k in length) the answer by @Miguel performs better but the approach by @mathfux scales way better when the arrays get longer.
推荐答案
您似乎正在尝试在二维数组中滚动形状为 (2, 3)
的窗口.这类似于图像卷积,可以非常有效地使用 np.lib.stride_tricks
进行卷积.方式.
It seems like you're trying to roll a window of shape (2, 3)
in a 2D array. This is similar to convolution of image which can be done with np.lib.stride_tricks
in a very efficient way.
a = np.transpose([Xd, Yd, Zd])
window = (2, 3)
view_shape = (len(a) - window[0] + 1,) + window # (4,2,3) if len(a) == 5
sub_matrix = np.lib.stride_tricks.as_strided(a, shape = view_shape, strides = (a.itemsize,) + a.strides)
>>> sub_matrix
array([[[0. , 0. , 0. ],
[0. , 0. , 1.5]],
[[0. , 0. , 1.5],
[1. , 0. , 1.5]],
[[1. , 0. , 1.5],
[1. , 2.5, 1.5]],
[[1. , 2.5, 1.5],
[0.5, 2.5, 1.5]]])
请注意, np.lib.stride_tricks
与其他方法相比,性能非常出色.
Note that np.lib.stride_tricks
is very performant against any alternative ways.
这篇关于numpy数组转换中的性能改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!