0

In python library numpy, is array([2,1]) (1,2) or (2,1)_(row,col)?

I made 3 tries below but couldn't find the answer.

import numpy as np

x = np.array([1,2])
y = np.array([[1,3,5], [2,4,6]])
z = np.array([[1,2],[3,4],[5,6]])
o = np.array([1,2,3])

print(np.dot(x,y))
print(np.dot(z,x))
print(np.dot(x,o))

The first and second one worked but the last one didn't. The error is:-

ValueError: shapes (2,) and (3,) not aligned: 2 (dim 0) != 3 (dim 0)

In my opinion, in case 1, x = (1 row and 2 col) in case 2, x = (2 row and 1 col) in case 3, x should be (2 row and 1 col) but it didn't worked

Please let me know why this happened.

3
  • The numbers denotes the value of the array not the shape. Commented Jan 30, 2021 at 3:59
  • It's neither. It's a one dimensional array, has shape (2,). Commented Jan 30, 2021 at 4:02
  • What was the exact array contents you were expecting as a result of np.dot(x,o)? Commented Jan 30, 2021 at 4:04

3 Answers 3

1
In [272]: x = np.array([1,2])
     ...: y = np.array([[1,3,5], [2,4,6]])
     ...: z = np.array([[1,2],[3,4],[5,6]])
     ...: o = np.array([1,2,3])
     ...: 
In [273]: x.shape
Out[273]: (2,)              # 1 element tuple
In [274]: y.shape
Out[274]: (2, 3)            # 2 element tuple
In [275]: z.shape
Out[275]: (3, 2)
In [276]: o.shape
Out[276]: (3,)

The row/columns interpretation of dimensions fits the 2 arrays like y and z. It's a poor fit with x and o.

We can make a 2d array from x, with one explicit row, 2 columns:

In [277]: x[None,:].shape
Out[277]: (1, 2)

But for many purposes the (2,) shape works just as well as the (1,2).

np.dot has well documented rules about how it handles 1d arrays.

The basic rule is the the sum-of-products is performed on the last dim of A, and 2nd to the last of B, with allowance for 1d.

x, y   (2,) with (2,3) => (3,)  (the 2's pair)
z, x   (3,2) with (2,) => (3,)  (the 2's pair)
x, o  (2,) with (3,)  no match!

A (n,2) will dot with (2,3) to produce a (n,3) result. Likewise a (3,2) with (2,n) produces a (3,n).

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

Comments

1

Here's a "golden" rule while learning numpy: When applying shape-compatibility rules, never think in terms of rows and columns.

Having said that, here are the shape-compatibility rules for np.dot(a,b) (reproduced here as a numbered bullets)

  1. If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
  2. If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
  3. If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
  4. If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
  5. If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b

(Notice that none of the rules are expressed in terms of rows and columns. They are expressed only in terms of the dimensions of the arrays a and b)

In our case:

x is a 1-D array (or "vector") with 2 elements.

y is a 2-D array with shape (2,3).

z is a 2-D array with shape (3,2).

o is a 1-D array (or "vector") with 3 elements.

For np.dot(x,y), rule(5) applies.

For np.dot(z,x), rule(4) applies.

For np.dot(x,o), rule(1) is attempted, and fails, because you can't do an inner product of a vector of 2 elements, with another vector of 3 elements. (Both vectors need to have the same number of elements)

1 Comment

I totally understand. Thank you very much!!
0

I'm not sure what your issue is here, when you have (X,) X implies number of elements and (X,Y) is x rows and Y cols

x.shape -> (2,) 2 elements
o.shape -> (3,) 3 elements
y.shape -> (2, 3) 2 rows 3 cols
z.shape -> (3, 2) 3 rows 2 cols

So np.dot(x,o) will give you an error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 6, in dot
ValueError: shapes (2,) and (3,) not aligned: 2 (dim 0) != 3 (dim 0)

4 Comments

No, (3,) is 3 elements. (1,3) is a 2d row/columns shape.
Yes I know I just wanted to use the (r,c) notation for the person to understand
@Kenan -- Any mention of rows and columns is ill-advised in the context of 1-dimensional arrays, and is the source of misunderstandings in learning numpy. Rows and columns should be mentioned strictly in the context of 2-dimensional arrays. So, I would rather say x does not have 1 row and 2 columns. It is just a 1-dimensional array, that's all.
Updated to help stop the spread of misunderstanding

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.