在内存numpy.transpose重新排序的数据? [英] Is numpy.transpose reordering data in memory?

查看:391
本文介绍了在内存numpy.transpose重新排序的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了加快像np.std,np.sum等沿一个n维巨大numpy的阵列的轴的功能时,建议沿最后轴线应用

In order to speed up the functions like np.std, np.sum etc along an axis of an n dimensional huge numpy array, it is recommended to apply along the last axis.

当我这样做,np.transpose旋转我想要操作的轴,到最后轴。难道真的改组内存中的数据,或者只是改变了轴的地址分配方式?

When I do, np.transpose to rotate the axis I want to operate, to the last axis. Is it really reshuffling the data in memory, or just changing the way the axis are addressed?

当我尝试使用%timeit来测量时间。它是做这个转微秒(比我是有这个(112x1024x1024)阵列复制所需的时间要小得多。

When i tried to measure the time using %timeit. it was doing this transpose in micro seconds, (much smaller than the time required to copy the (112x1024x1024) array i was having.

如果它是不实际时,适用于新近旋转最后轴重新排序在存储器中的数据,并仅改变寻址,将它仍然加速np.sum或np.std

If it is not actually reordering the data in memory and only changing the addressing, will it still speed up the np.sum or np.std when applied to newly rotated last axis?

当我试图来衡量它,我似乎加快。但我不明白怎么。

When i tried to measure it, i does seem to speed up. But i don't understand how.

这并不似乎有转加快。最快的轴是最后一个时,它是C-排序,第一个当它是用Fortran订购。所以在没有移调申请点或np.sum之前np.std。
对于我的具体code,I由阵列创建过程中,为了给='FORTRAN'解决了这个问题。其中最快制成第一轴线

It doesn't really seem to speed up with transpose. The fastest axis is last one when it is C-ordered, and first one when it is Fortran-ordered. So there is no point in transposing before applying np.sum or np.std. For my specific code, i solved the issue by giving order='FORTRAN' during the array creation. Which made the first axis fastest.

感谢所有的答案。

推荐答案

移调只是改变href=\"https://en.wikipedia.org/wiki/Stride_of_an_array\"> 的步伐在之沿建议最后一个轴(我想看看它的源,顺便说一句。)是等,当一个数组是C-有序,沿着参考最后一个轴preserves地方走。这不会是这样,你转后,由于转阵列将Fortran的排序。

Transpose just changes the strides, it doesn't touch the actual array. I think the reason why sum etc. along the final axis is recommended (I'd like to see the source for that, btw.) is that when an array is C-ordered, walking along the final axis preserves locality of reference. That won't be the case after you transpose, since the transposed array will be Fortran-ordered.

这篇关于在内存numpy.transpose重新排序的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆