In the following code, I am creating 2 numpy array. One is 1D and the other is 2D.
When I transpose the 1D array it stays the same. It does not changes from row matrix to column matrix. When I transpose the 2D array it changes from a row matrix to a column matrix.
Code:
a = np.array([1,2,3,4,5])
print("a: ", a)
print("a.T: ", a.T)
b = np.array([[1,2,3,4,5]])
print("b: ", b)
print("b.T: ", b.T)
Output:
a: [1 2 3 4 5]
a.T: [1 2 3 4 5]
b: [[1 2 3 4 5]]
b.T: [[1]
[2]
[3]
[4]
[5]]
Now, I have some questions:
a = np.array([1,2,3,4,5])
, does that really create a row matrix?print("a.T: ", a.T)
does that convert a from row matrix to column matrix implicitly? Or it really remains unchanged?b = np.array([[1,2,3,4,5]])
does that really create a row matrix?print("b.T: ", b.T)
does that convert the row matrix to a column matrix? I mean, am I right?Or if I create a numpy array with just a single bracket, there is no issue or row or column matrix at all?
And another thing, when perform the dot operation, I get the following thing:
Code:
print(b.dot(a))
print(b.dot(a.T))
Output:
[55]
[55]
But, as far as I am concerned dot() function performs the task of matrix multiplication. If that's the case, wasn't there supposed to be an error in one of the case, as number of columns of first matrix must be equal to the number of rows of the seconds matrix, according to the rule of matrix multiplication?
a
has shape (5,);b
(1,5).
transpose` reverses the shape - but without adding dimensions. Sooner or later when working withnumpy
you'll want to drop the 'matrix/vector' terminology, and focus on shape. – hpaulj