Skip to content Skip to sidebar Skip to footer

Np Array Dot Product Of Vector And Array

I have a problem in understanding the working behind the numpy dot function and broadcasting.Below is the snippet I am trying to understand a=np.array([[1,2],[3,5]]) if we check th

Solution 1:

The way that numpy is programmed means that a 1D array, shape=(n,), is treated as neither a column or a row vector, but can act like either one based on the position in a dot product. To better explain this consider comparing the case with an asymmetric array to a symmetric array:

>>>a=numpy.arange(3)
>>>a.shape=(1,3)
>>>a
array([0,1,2])

>>>b=numpy.arange(9)
>>>b.shape=(3,3)
>>>b
array([0,1,2]
      [3,4,5]
      [6,7,8])

Then define a (3,) vector:

>>>c=numpy.arange(3)
>>>c
array([0,1,2])
>>>c.shape
(3,)

In normal linear algebra, if c were a column vector we would expect a.c to make a constant, 1x3 matrix dot with 3x1 column vector, and c.a to produce a 3x3 matrix, 3x1 column times a 1x3 row. Doing this in python you will find that a.dot(c) will produce a (1,) array (the constant we expect), but c.dot(a) will raise an error:

>>>d=a.dot(c)
d.shape=(1,)
>>>e=c.dot(a)
ValueError: shapes (3,) and (1,3) not aligned: 3 (dim 0) != 1 (dim 0)

What has gone wrong is that that numpy has checked the only dimension of c against the first dimension of a, not checked the last dimension of c against a. According to numpy a 1D array has only 1 dimension and all checks are done against that dimension. Because of this we find 1D arrays don't act strictly as a column or a row vector. E.g. b.dot(c) checks the second dimension of b against the one dimension of c (c acts like column vector) and c.dot(b) checks the one dimension of c against first dimension of b (c acts like a row vector). Therefore, they both work:

>>>f=b.dot(c)
>>>f
array([ 5, 14, 23])

>>>g=c.dot(b)
>>>g
array([15, 18, 21]) 

To avoid this, you must give your array its second dimension for it to be a row or column vector. In this example you would explicitly say that c.shape=(3,1) for a column vector or c.shape=(1,3) for a row vector.

>>>c.shape=(3,1)
>>>c.dot(a)
array([0,0,0]
      [0,1,2]
      [0,2,4])
>>>h=c.dot(b)
ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)


>>>c.shape=(1,3) 
>>>i=c.dot(b)
>>>i
array([[15, 18, 21]])

The point to take from this is that: According to numpy, row and column vectors have two dimensions


Solution 2:

At first, a=np.array([[1,2],[3,5]) changed as a=np.array([[1,2],[3,5]]) in order to work

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

Answer to your question b shape is 2, that is row size.

a = np.array([1, 2, 3])
a.shape
(3,) #here 3 is row size its one dimensional array. 

Dot operator:

numpy.dot

Example:

np.dot(2, 4)
8

Another example with 2D array:

>>> a = [[1, 0], [0, 1]]
>>> b = [[4, 1], [2, 2]]
>>> np.dot(a, b)
array([[4, 1],
       [2, 2]])
  • The dot function to compute inner products of vectors, to multiply a vector by a matrix, and to multiply matrices.

  • Dot is available both as a function in the numpy module and as an instance method of array objects

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b:

How do they calculate?

b.dot(a) is also possible and results in array([21,36])and this blew my mind.How are they checking the compatibility of the vector for matrix multiplication and how do they calculate?

This is basic matrix product.

     a
    array([[1, 2],  #2D array 
           [3, 5]])
    >>> b
    array([3, 6]) #1D array 

(7*3 6*6) = ([21, 36])

Post a Comment for "Np Array Dot Product Of Vector And Array"