np.transpose doesn't return transpose of matrix
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
I get the same matrix as H instead of the transpose of 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
I get the transpose of 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?
2 Answers
2
I think this is the behavior you're looking for:
>>> H = np.array([[1,1]])
>>> H.T
array([[1],
[1]])
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(H)
(1,2)
H = np.array([1,2])
(2,)
From the documentation:
numpy.matrix.transpose. Returns a view of the array with axes transposed. For a 1-D array, this has no effect. (To change between column and row vectors, first cast the 1-D array into a matrix object.)
You have called a variable
matrix
and assigned a list to it. That is why from numpy import *
is a bad idea.– BoarGules
Aug 26 at 9:42
matrix
from numpy import *
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
When I use the following: matrix('[1, 2; 3, 4]') It says 'list' object is not callable
– Dhruv Thakkar
Aug 26 at 9:07