为什么使用transpose而不是直接设置数组?

时间:2016-08-26 00:36:21

标签: python numpy

在python代码中我看到下面两行(最初,标签是类型(15093,即1-d数组)(来自py-faster-rcnn)

labels = labels.reshape((1, height, width, A)).transpose(0, 3, 1, 2)
labels = labels.reshape((1, 1, A * height, width))

作者使用转置是否有任何理由而不是直接设置

labels = labels.reshape((1, A, height, width))
labels = labels.reshape((1, 1, A * height, width))

甚至,

labels = labels.reshape((1, 1, A * height, width))

? (我猜它与初始标签数组中的数据顺序有关)

1 个答案:

答案 0 :(得分:1)

答案很简单:是的,有一个原因,给定相同的labels数组,你的3种方法的结果根本不相同。

让我们用一个例子来检查:

import numpy as np
height, width, A = [2,3,4]
arr=np.random.rand(1*height*width*A)

print("Method 1")
labels1=np.copy(arr)
labels1 = labels1.reshape((1, height, width, A)).transpose(0, 3, 1, 2)
labels1 = labels1.reshape((1, 1, A * height, width))
print(labels1)

print("Method 2")
labels2=np.copy(arr)
labels2 = labels2.reshape((1, A, height, width))
labels2 = labels2.reshape((1, 1, A * height, width))
print(labels2)

print("Method 3")
labels3=np.copy(arr)
labels3 = labels3.reshape((1, 1, A * height, width))
print(labels3)

给出了:

>>> Method 1
>>> [[[[ 0.97360395  0.40639034  0.92936386]
>>>    [ 0.01687321  0.94744919  0.39188023]
>>>    [ 0.34210967  0.36342341  0.6938464 ]
>>>    [ 0.60065943  0.00356836  0.91785409]
>>>    [ 0.57095964  0.61036102  0.17318427]
>>>    [ 0.38002045  0.08596757  0.29407445]
>>>    [ 0.95899964  0.13046103  0.36286533]
>>>    [ 0.86970793  0.11659624  0.82073826]]]]
>>> Method 2
>>> [[[[ 0.97360395  0.34210967  0.57095964]
>>>    [ 0.95899964  0.40639034  0.36342341]
>>>    [ 0.61036102  0.13046103  0.92936386]
>>>    [ 0.6938464   0.17318427  0.36286533]
>>>    [ 0.01687321  0.60065943  0.38002045]
>>>    [ 0.86970793  0.94744919  0.00356836]
>>>    [ 0.08596757  0.11659624  0.39188023]
>>>    [ 0.91785409  0.29407445  0.82073826]]]]
>>> Method 3
>>> [[[[ 0.97360395  0.34210967  0.57095964]
>>>    [ 0.95899964  0.40639034  0.36342341]
>>>    [ 0.61036102  0.13046103  0.92936386]
>>>    [ 0.6938464   0.17318427  0.36286533]
>>>    [ 0.01687321  0.60065943  0.38002045]
>>>    [ 0.86970793  0.94744919  0.00356836]
>>>    [ 0.08596757  0.11659624  0.39188023]
>>>    [ 0.91785409  0.29407445  0.82073826]]]]

因此方法1与2和3不同,而2和3是相同的。