numpy:高效执行数组的复杂重塑

发布于 2024-10-26 10:04:47 字数 969 浏览 4 评论 0原文

我正在将供应商提供的大型二进制数组读取到 2D numpy 数组 tempfid(M, N) 中

# load data
data=numpy.fromfile(file=dirname+'/fid', dtype=numpy.dtype('i4'))

# convert to complex data
fid=data[::2]+1j*data[1::2]

tempfid=fid.reshape(I*J*K, N)

,然后需要使用索引的非平凡映射将其重塑为 4D 数组 use4d(N,I,J,K) 。我按照以下方式使用 for 循环执行此操作:

for idx in range(M):
    i=f1(idx) # f1, f2, and f3 are functions involving / and % as well as some lookups
    j=f2(idx)
    k=f3(idx)
    newfid[:,i,j,k] = tempfid[idx,:] #SLOW! CAN WE IMPROVE THIS?

转换为复数需要 33% 的时间,而复制这些切片 M 切片需要剩余的 66%。计算索引的速度很快,无论我是在如图所示的循环中逐一执行此操作,还是通过 numpy.vectorizing 操作并将其应用到 arange(M) 来执行此操作。

有没有办法加快这个速度?对于更有效的切片、复制(或不复制)等的任何帮助,我们表示赞赏。

编辑: 正如在问题 “将交错 NumPy 整数数组转换为复杂 64 的最快方法是什么?”的回答中了解到的,转换为如果使用视图来代替,复杂性可以加速 6 倍:

 fid = data.astype(numpy.float32).view(numpy.complex64)

I am reading a vendor-provided large binary array into a 2D numpy array tempfid(M, N)

# load data
data=numpy.fromfile(file=dirname+'/fid', dtype=numpy.dtype('i4'))

# convert to complex data
fid=data[::2]+1j*data[1::2]

tempfid=fid.reshape(I*J*K, N)

and then I need to reshape it into a 4D array useful4d(N,I,J,K) using non-trivial mappings for the indices. I do this with a for loop along the following lines:

for idx in range(M):
    i=f1(idx) # f1, f2, and f3 are functions involving / and % as well as some lookups
    j=f2(idx)
    k=f3(idx)
    newfid[:,i,j,k] = tempfid[idx,:] #SLOW! CAN WE IMPROVE THIS?

Converting to complex takes 33% of the time while the copying of these slices M slices takes the remaining 66%. Calculating the indices is fast irrespective of whether I do this one by one in a loop as shown or by numpy.vectorizing the operation and applying it to an arange(M).

Is there a way to speed this up? Any help on more efficient slicing, copying (or not) etc appreciated.

EDIT:
As learned in the answer to question "What's the fastest way to convert an interleaved NumPy integer array to complex64?" the conversion to complex can be sped up by a factor of 6 if a view is used instead:

 fid = data.astype(numpy.float32).view(numpy.complex64)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念满溢 2024-11-02 10:04:47
idx = numpy.arange(M)
i = numpy.vectorize(f1)(idx)
j = numpy.vectorize(f2)(idx)
k = numpy.vectorize(f3)(idx)

# you can index arrays with other arrays
# that lets you specify this operation in one line.    
newfid[:, i,j,k] = tempfid.T

我从未使用过 numpy 的矢量化。 Vectorize 只是意味着 numpy 会多次调用你的 python 函数。为了提高速度,您需要使用数组运算,就像我在这里展示的那样,您过去常常获取复数。

编辑

问题是尺寸 128 的尺寸在 newfid 中排在第一位,但在 tempfid 中排在最后。通过使用采用转置的 .T 可以轻松实现这一点。

idx = numpy.arange(M)
i = numpy.vectorize(f1)(idx)
j = numpy.vectorize(f2)(idx)
k = numpy.vectorize(f3)(idx)

# you can index arrays with other arrays
# that lets you specify this operation in one line.    
newfid[:, i,j,k] = tempfid.T

I've never used numpy's vectorize. Vectorize just means that numpy will call your python function multiple times. In order to get speed, you need use array operations like the one I showed here and you used to get complex numbers.

EDIT

The problem is that the dimension of size 128 was first in newfid, but last in tempfid. This is easily by using .T which takes the transpose.

长安忆 2024-11-02 10:04:47

这个怎么样。使用 f1、f2、f3 的矢量化版本(不一定使用 np.vectorize,但可能只是编写一个接受数组并返回数组的函数)为我们设置您的索引,然后使用 np.ix_ :

http://docs.scipy.org/doc/numpy/参考/生成/numpy.ix_.html

获取索引数组。然后将 tempfid 重塑为与 newfid 相同的形状,然后使用 np.ix_ 的结果来设置值。例如:

tempfid = np.arange(10)
i = f1(idx) # i = [4,3,2,1,0]
j = f2(idx) # j = [1,0]
ii = np.ix_(i,j)
newfid = tempfid.reshape((5,2))[ii]

这会将 tempfid 的元素映射到具有不同顺序的新形状上。

How about this. Set us your indicies using the vectorized versions of f1,f2,f3 (not necessarily using np.vectorize, but perhaps just writing a function that takes an array and returns an array), then use np.ix_:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ix_.html

to get the index arrays. Then reshape tempfid to the same shape as newfid and then use the results of np.ix_ to set the values. For example:

tempfid = np.arange(10)
i = f1(idx) # i = [4,3,2,1,0]
j = f2(idx) # j = [1,0]
ii = np.ix_(i,j)
newfid = tempfid.reshape((5,2))[ii]

This maps the elements of tempfid onto a new shape with a different ordering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文