- Python: 3.8.11
- Numpy: 1.20.1
- OS: Ubuntu Kylin 20.04
- Conda: 4.10.1
- Jupyter lab: 3.1.4
Code sample
import numpy as np
Copy the code
a = np.array([[1.2.3.4], [5.6.7.8]])
b = np.array([[1.2.3], [4.5.6], [7.8.9], [10.11.12]])
Copy the code
a
array([[1.2.3.4],
[5.6.7.8]])
Copy the code
b
array([[ 1.2.3],
[ 4.5.6],
[ 7.8.9],
[10.11.12]])
Copy the code
a.shape,b.shape
((2.4), (4.3))
Copy the code
The number of columns in the first matrix is the same as the number of rows in the second matrix
np.dot(a,b)
array([[ 70.80.90],
[158.184.210]])
Copy the code
np.dot(b,a)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-69-e6bd3a7b39a0> in <module>
----> 1 np.dot(b,a)
<__array_function__ internals> in dot(*args, **kwargs)
ValueError: shapes (4.3) and (2.4) not aligned: 3 (dim 1) != 2 (dim 0)
Copy the code
The source code to learn
help(np.dot)
Help on function dot in module numpy:
dot(...)
dot(a, b, out=None)
Dot product of two arrays. Specifically,
- If both `a` and `b` are 1-D arrays, it is inner product of vectors
(without complex conjugation).
- If both `a` and `b` are 2-D arrays, it is matrix multiplication,
but using :func:`matmul` or ``a @ b`` is preferred.
- If either `a` or `b` is 0-D (scalar), it is equivalent to :func:`multiply`
and using ``numpy.multiply(a, b)`` or ``a * b`` is preferred.
- 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`.
- 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`::
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
......
Copy the code
Learning to recommend
- Python documentation – English
- Python documentation – Chinese
- Python code PEP
- Google version of the Python specification
- Python source code
- Python PEP
- Optimal kirin
- The nuggets platform
- Gitee platform
Python is open source, cross-platform, interpretive, interactive, and worth learning. Python’s design philosophy: elegant, unambiguous, simple. Advocate one way, preferably only one way to do one thing. Code should be written in accordance with specifications to facilitate communication and understanding. Every language has its own unique ideas. Beginners need to change their thinking, practice and accumulate.