Home python How the numpy.dot function works

# How the numpy.dot function works

Author

Date

Category

Help me with the `numpy.dot `function. As it is not very clear description in the documentation. This function works the same 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)
``````

In theory, you should get an array Uj with Nj rows and 1 column, but you get a two-dimensional array.
The part of the code following the initialization is forgiven, since it is not relevant to the question.

## Answer 1, authority 100%

product of scalars:

``````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 -
``````

The result is a 2D array, consisting of 100 rows and one column:

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

# Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.