Gunwoo Park
Gunwoo Park

Reputation: 1

Is array([2,1]) 1 row & 2 cols or 2 rows & 1 col?(In python numpy)

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.

Upvotes: 0

Views: 232

Answers (3)

Kenan
Kenan

Reputation: 14104

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)

Upvotes: 0

fountainhead
fountainhead

Reputation: 3722

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)

Upvotes: 1

hpaulj
hpaulj

Reputation: 231425

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).

Upvotes: 1

Related Questions