np.transpose不返回矩阵的转置 [英] np.transpose doesn't return transpose of matrix
问题描述
当我以以下方式编写代码时:
When I write my code in the following manner :
from numpy import *
H = array([1,1])
Ht = transpose(H)
Ht
我得到与H相同的矩阵,而不是H的转置.
I get the same matrix as H instead of the transpose of H.
但是当我以以下方式更改矩阵H时:
But when I change the matrix H in the following way :
from numpy import *
H = array(([1,1],[2,3]))
Ht = transpose(H)
Ht
我得到H的转置.
我不明白这里发生了什么.是使用转置函数的方式还是定义矩阵的方式?
I fail to understand what is happening here. Is it the way transpose function is used or is it the way a matrix is defined?
推荐答案
我认为这是您要寻找的行为:
I think this is the behavior you're looking for:
>>> H = np.array([[1,1]])
>>> H.T
array([[1],
[1]])
等效地,您可以编写np.transpose(H)
.请注意,此数组的形状为(1,2)
,因此具有两个维度.数组H = np.array([1,2])
具有形状(2,)
.它只有一个维度.要交换尺寸(转置),您至少需要两个.
Equivalently you can write np.transpose(H)
. Notice this array has shape (1,2)
, so it has two dimensions. The array H = np.array([1,2])
has shape (2,)
. It only has one dimension. To swap the dimensions (transpose), you need at least two of them.
这篇关于np.transpose不返回矩阵的转置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!