3

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?

2
  • 1
    You keep using the term matrix, but none of these are matrices, they are all arrays Commented Sep 23, 2018 at 3:07
  • 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. Commented Sep 23, 2018 at 7:09

1 Answer 1

5
  1. No, that's not a row matrix in Numpy, as it only has one dimension (5,)

  2. No, because a is not a row matrix (see 1)

  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, just a 1D array

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.