我有一个巨大的空数组(300000,80,80),我想使用numpy.reshape
交换它的轴。我试过numpy.rollaxis
、numpy.swapaxes
和numpy.transpose
。他们工作得很有魅力,但他们放慢了前进的步伐
我也尝试过用C或F顺序创建空数组,但没有任何改变
那么,我如何使用numpy.reshape
来改变轴的顺序呢
(300000,80,80)——>;(80,80300000)而不使用numpy.rollaxis
等
任何想法都将受到赞赏
这是我的密码:
patch = np.ones([3,80,80])
image = np.empty([300000,80,80], dtype='uint8', order='C')
for i in range(0,300000,3):
image[i:i+3] = patch
# if i use np.rollaxis, next fancy indexing execute too slow.
pt = ([...], [...]) #some tuple
ij = ([...], [...]) #some tuple
transformed[pt] = image[ij]
reshape
不能与transpose/swapaxes
工作相同我将试着举例说明
arr
实际上是源arange
的view
,共享数据缓冲区中元素的顺序为:transpose
也是一个view
,但有不同的shape
、strides
和order
要遍历
tarr
的列,它需要执行24个字节或3个元素的步骤,从0到3,从1到4等等因为它是一个
view
,transpose
很快。但后续操作通常需要一个拷贝,这对于大型阵列来说要慢得多如果我们尝试重塑,我们会得到:
形状匹配
tarr
,但strides
不匹配。[0,1,2]
行已被拆分相关问题 更多 >
编程相关推荐