numpy.ndarray.transpose

一、ndarray.transpose的官方文档:

ndarray.transpose(*axes)
Returns a view of the array with axes transposed.
//返回对array进行维度转置后的结果

For a 1-D array this has no effect, as a transposed vector is simply the same vector.
//一维array转置后仍然是其本身

To convert a 1-D array into a 2D column vector, an additional dimension must be added. np.atleast2d(a).T achieves this, as does a[:, np.newaxis].

For a 2-D array, this is a standard matrix transpose.
//对二维array,进行标准矩阵转置

For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples). If axes are not provided and a.shape = (i[0], i[1], … i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], … i[1], i[0]).

Parameters
axes:None, tuple of ints, or n ints
None or no argument: reverses the order of the axes.

tuple of ints: i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.
//将原array中第i个维度置于新array的第j个维度

n ints: same as an n-tuple of the same ints (this form is intended simply as a “convenience” alternative to the tuple form)

Returns:out:ndarray
View of a, with axes suitably permuted.

官方文档中给的Examples:

a = np.array([[1, 2], [3, 4]])
a

array([[1, 2],
[3, 4]])
//使用np.array()创建array

a.transpose()

array([[1, 3],
[2, 4]])
//本Example作用:说明文档中“If axes are not provided and a.shape = (i[0], i[1], … i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], … i[1], i[0]).”

a.transpose((1, 0))

array([[1, 3],
[2, 4]])
//本Example作用:说明文档中“tuple of ints: i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.”的参数形式

a.transpose(1, 0)

array([[1, 3],
[2, 4]])
//本Example作用:说明文档中“n ints: same as an n-tuple of the same ints (this form is intended simply as a “convenience” alternative to the tuple form)”参数形式

注意的是:由于上述函数没有将转置后的结果进行返回,因此array a始终是最初的形式

二、使用ndarray.transpose函数:
(1)原array:

In:a = np.array([[[3,6],[2,7],[8,6]],[[4,3],[5,8],[0,3]]])
In:a
Out[6]: 
array([[[3, 6],
        [2, 7],
        [8, 6]],
       [[4, 3],
        [5, 8],
        [0, 3]]])
In:a.shape
Out[7]: (2, 3, 2)

(2)进行“If axes are not provided and a.shape = (i[0], i[1], … i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], … i[1], i[0]).”形式的转置:

In:a.transpose()
Out[8]: 
array([[[3, 4],
        [2, 5],
        [8, 0]],
       [[6, 3],
        [7, 8],
        [6, 3]]])
In:a.transpose().shape
Out[10]: (2, 3, 2)

此处为三维矩阵进行转置,如下图所示:

[1]https://segmentfault.com/a/1190000016731276
在这里插入图片描述
沿着绿色方向线进行交换
因为与绿色方向线垂直的方向是一维轴线和三维轴线的角平分线

(3)进行“uple of ints: i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.”形式的转置:

In:a.transpose((1,2,0))
Out[11]: 
array([[[3, 4],
        [6, 3]],
       [[2, 5],
        [7, 8]],
       [[8, 0],
        [6, 3]]])

转置直观图如下所示:
在这里插入图片描述
在(2)中,该转置操作改变了立方体的构造;
在(3)中,该转置操作仅仅改变了立方体的摆放位置,但是没有改变构造。

三、注意事项:
1、无order参数,则默认使用“全部颠倒过来”(If axes are not provided and a.shape = (i[0], i[1], … i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], … i[1], i[0]).)的置换方式
2、order参数可以是int tuple(如a.transpose((2,0,1))),也可以是ints(如a.transpose(2,0,1),注意,不是tuple)

四、使用场景:

_img = cv2.imread(filename)
_img = cv2.resize(_img, (mycfg.IMG_WIDTH, mycfg.IMG_HEIGHT), interpolation=cv2.INTER_CUBIC)
_img = _img.transpose((2, 0, 1))
_img = _img / 255
_img = torch.from_numpy(_img)
_img = _img.to(torch.float32)
return _img

使用cv2.imread()函数读入img,形式为numpy.ndarray,其shape为(height,weight,channel)
进行转置,变换为(channel,height,weight)
在这里插入图片描述
使用a.transpose(2,0,1)并没有改变原array(如上图所示立方体)的任何构造,只是进行了位置改变
因此图片内容(各个像素之间的位置关系)没有发生改变

猜测:此处对图像进行transpose(2,0,1)转置操作,是为了_img = torch.from_numpy(_img),将其转为张量。待更新。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值