Please help numpy.dot with the numpy.dot function. As it is not very clear description in the documentation. Does this feature work the same way as described in this article?

There is the following code:

 Nj = 100 Nin = 100 Xin = np.zeros((Nin,1)) Winj = np.zeros((Nin,Nj)) WinjT = np.transpose(Winj) Uj = np.dot(WinjT,Xin) 

The idea is to get an array Uj with the number of rows Nj and 1 column, but it turns out a two-dimensional array. The part of the code going after the initialization is forgiven, since it has nothing to do with the question.

  • one
    This function is a scalar product if vectors and matrix products (the most ordinary) are transferred to it, if matrices are transferred. - m9_psy
  • As I understand it, a vector means a one-dimensional array, and a two-dimensional matrix. Or is it necessary to define matrices with the numpy.matrix function? - Alexey Voronov

1 answer 1

scalar product:

 In [60]: np.dot(2, 3) Out[60]: 6 

product of 1D arrays (vectors):

 In [61]: a = np.array([1, 2]) In [62]: b = np.array([10, 11]) In [63]: np.dot(a, b) Out[63]: 32 

product of 2D arrays:

 In [64]: a = np.array([[1,2], [3,4]]) In [65]: b = np.array([[2,3], [4,5]]) In [66]: a Out[66]: array([[1, 2], [3, 4]]) In [67]: b Out[67]: array([[2, 3], [4, 5]]) In [68]: np.dot(a, b) Out[68]: array([[10, 13], [22, 29]]) 

Explanation:

 10: 1*2 + 2*4 13: 1*3 + 2*5 22: 3*2 + 4*4 29: 3*3 + 4*5 

Your example:

 In [69]: %paste Nj = 100 Nin = 100 Xin = np.zeros((Nin,1)) Winj = np.zeros((Nin,Nj)) WinjT = np.transpose(Winj) Uj = np.dot(WinjT,Xin) ## -- End pasted text -- 

It turned out a 2D array consisting of 100 rows and one column:

 In [70]: Uj.shape Out[70]: (100, 1)