numpy中transpose的用法

发布时间:2024年01月24日

transpose函数用于改变数组的维度顺序。

在numpy库中,transpose函数可以通过指定轴的顺序来改变数组的维度顺序。

在pytorch库中,transpose函数可以通过指定维度的顺序来改变张量的维度顺序。

1.transpose()可以将矩阵进行转置:

import numpy as np
a=np.random.randn(2,3)
print(a)
print(a.T)               #a.T等价a.transpose()等价a.swapaxes()
print(a.transpose())
print(a.transpose(1,0))
print(a.transpose(0,1))     
print(a.swapaxes(1,0))   #swapaxes()是轴交换函数,必须要赋值参数
print(a.swapaxes(0,1))
[[ 0.81681063  1.10377595  1.02641089]
 [ 2.07708501 -1.22059543 -0.88032416]]

[[ 0.81681063  2.07708501]
 [ 1.10377595 -1.22059543]
 [ 1.02641089 -0.88032416]]

[[ 0.81681063  2.07708501]
 [ 1.10377595 -1.22059543]
 [ 1.02641089 -0.88032416]]

[[ 0.81681063  2.07708501]
 [ 1.10377595 -1.22059543]
 [ 1.02641089 -0.88032416]]

[[ 0.81681063  1.10377595  1.02641089]
 [ 2.07708501 -1.22059543 -0.88032416]]

[[ 0.81681063  2.07708501]
 [ 1.10377595 -1.22059543]
 [ 1.02641089 -0.88032416]]

[[ 0.81681063  2.07708501]
 [ 1.10377595 -1.22059543]
 [ 1.02641089 -0.88032416]]

2.transpose()的作用本质上是交换不同维度的数据:

比如a=np.random.randn(2,3)中a=np.random.randn(第0维,第1维),transpose()默认参数是transpose(1,0),将第0维和第1维的数据进行了交换,当然,如果将其赋值为transpose(0,1),则矩阵不变。

3.transpose()功能相比a.T和a.swapaxes()更强大,其可以处理三维数据。

import numpy as np
a=np.random.randn(2,3,4)
print(a)
print(a.transpose())
print(a.transpose().shape)   #(4, 3, 2),对于三维矩阵,transpose()默认参数是transpose(2,1,0)
print(a.transpose(2,0,1))
print(a.transpose(2,0,1).shape)       #(4, 2, 3)我们可以对其参数赋值
a结果:
[[[ 1.21428348 -1.35542648  0.81645952  0.4012772 ]
  [-0.34938895 -1.27186791 -0.93752179  0.01180899]
  [-0.03543477 -1.49778017 -0.8660784   0.20960659]]

 [[-1.65509767 -1.05410809 -1.06713016 -1.64721447]
  [ 2.37974213  0.49205418 -0.50939474 -0.27133827]
  [-0.21645405  0.54908107 -0.18495571  1.08154255]]]

a.transpose()结果:
[[[ 1.21428348 -1.65509767]
  [-0.34938895  2.37974213]
  [-0.03543477 -0.21645405]]

 [[-1.35542648 -1.05410809]
  [-1.27186791  0.49205418]
  [-1.49778017  0.54908107]]

 [[ 0.81645952 -1.06713016]
  [-0.93752179 -0.50939474]
  [-0.8660784  -0.18495571]]

 [[ 0.4012772  -1.64721447]
  [ 0.01180899 -0.27133827]
  [ 0.20960659  1.08154255]]]

(4, 3, 2)

a.transpose(2,0,1)结果:
[[[ 1.21428348 -0.34938895 -0.03543477]
  [-1.65509767  2.37974213 -0.21645405]]

 [[-1.35542648 -1.27186791 -1.49778017]
  [-1.05410809  0.49205418  0.54908107]]

 [[ 0.81645952 -0.93752179 -0.8660784 ]
  [-1.06713016 -0.50939474 -0.18495571]]

 [[ 0.4012772   0.01180899  0.20960659]
  [-1.64721447 -0.27133827  1.08154255]]]
(4, 2, 3)

以下是transpose函数的用法示例:

1. 在numpy中,transpose函数的用法如下:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
transposed_arr = np.transpose(arr)
print(transposed_arr)


输出:

[[1 4]
?[2 5]
?[3 6]]


2. 在pytorch中,transpose函数的用法如下:

import torch

tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
transposed_tensor = tensor.transpose(0, 1)
print(transposed_tensor)


输出:
?

tensor([[1, 4],
? ? ? ? [2, 5],
? ? ? ? [3, 6]])

文章来源:https://blog.csdn.net/weixin_65544554/article/details/135817437
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。