1
votes

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:

  1. a = np.array([1,2,3,4,5]), does that really create a row matrix?

  2. print("a.T: ", a.T) does that convert a from row matrix to column matrix implicitly? Or it really remains unchanged?

  3. b = np.array([[1,2,3,4,5]]) does that really create a row matrix?

  4. print("b.T: ", b.T) does that convert the row matrix to a column matrix? I mean, am I right?

  5. 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?

1
You keep using the term matrix, but none of these are matrices, they are all arraysuser3483203
a has shape (5,); b (1,5). transpose` reverses the shape - but without adding dimensions. Sooner or later when working with numpy you'll want to drop the 'matrix/vector' terminology, and focus on shape.hpaulj

1 Answers

3
votes
  1. No, that's not a row matrix
  2. No, because a is not a row matrix
  3. Yes, assuming by "row matrix" you mean "2D array with one row and N columns"
  4. Yes, assuming by "column matrix" you mean "2D array with N rows and one column"
  5. With a single set of brackets, you're not creating a "matrix" (read: 2D array) at all.

  6. (?) The documentation for np.dot makes it clear that it is only matrix multiplication in some special cases.