0

In the following code I expect that the shape of s should be (2,2) but it is only 2. Why? If I convert b in two dimensional array it works fine. But how python handles arrays have shape(2,)? If I take its transpose shape should be (1,2) but it remains(2,) indicating that it still remains row even after taking transpose. Is it compulsory to convert b int 2 dimensional array to perform element wise matrix multiplication?

`v = np.arange(6).reshape(2,3)
for i in range(1):
    b =(v[:,i])
    c= b.T
    s = np.multiply(b,c)
    print(s)  #(2,)`
jerry
  • 385
  • 6
  • 18
  • Transpose exchanges dimensions, it does not add any. `(2,) => (2,)`, `(1,2) => (2,1)`. – hpaulj Aug 20 '19 at 03:32
  • Can you please elaborate it a bit more. transpose should convert rows into columns.riight? that's why I expect (1,2) shape after taking transpose. – jerry Aug 20 '19 at 03:36
  • Forget the words, row and column, and focus on the shape tuple. What does `reverse the dimensions` mean? Reread the docs. – hpaulj Aug 20 '19 at 04:06
  • *"But how python handles arrays have shape(2,)? If I take its transpose shape should be (1,2)"*. That is not how the transpose operation works in NumPy. If an array has shape (2, 1), then its transpose has shape (1, 2), as expected. But an array with shape (2,) has no dimensions to swap. The transpose operation on such an array is a "no op"; the result is the same array, with shape (2,). – Warren Weckesser Aug 20 '19 at 05:12
  • You are not the first to be surprised by this. With the right search terms, you will find many similar questions here on Stackoverlow. For example, https://stackoverflow.com/questions/5954603/transposing-a-numpy-array, https://stackoverflow.com/questions/52024613/np-transpose-doesnt-return-transpose-of-matrix (so this question can be closed as a duplicate). – Warren Weckesser Aug 20 '19 at 05:13

0 Answers0