NumPy科学计算库学习_010_NumPy数组的广播机制
NumPy数组广播机制的说明
当两个数组形状不同时,可以通过扩展数组的方式实现计算操作。这种机制就叫做广播机制。
1维NumPy数组的广播机制
\[加法:
\begin{bmatrix}
{0}&{0}&{0}\\
{1}&{1}&{1}\\
{2}&{2}&{2}\\
{3}&{3}&{3}\\
\end{bmatrix}
+
\begin{bmatrix}
\color{red}{1}&\color{red}{2}&\color{red}{3}\\
\color{lightgrey}{1}&\color{lightgrey}{2}&\color{lightgrey}{3}\\
\color{lightgrey}{1}&\color{lightgrey}{2}&\color{lightgrey}{3}\\
\color{lightgrey}{1}&\color{lightgrey}{2}&\color{lightgrey}{3}\\
\end{bmatrix}
=
\begin{bmatrix}
{1}&{2}&{3}\\
{2}&{3}&{4}\\
{3}&{4}&{5}\\
{4}&{5}&{6}\\
\end{bmatrix}
\]
\[减法:
\begin{bmatrix}
{0}&{0}&{0}\\
{1}&{1}&{1}\\
{2}&{2}&{2}\\
{3}&{3}&{3}\\
\end{bmatrix}
-
\begin{bmatrix}
\color{red}{1}&\color{red}{2}&\color{red}{3}\\
\color{lightgrey}{1}&\color{lightgrey}{2}&\color{lightgrey}{3}\\
\color{lightgrey}{1}&\color{lightgrey}{2}&\color{lightgrey}{3}\\
\color{lightgrey}{1}&\color{lightgrey}{2}&\color{lightgrey}{3}\\
\end{bmatrix}
=
\begin{bmatrix}
{-1}&{-2}&{-3}\\
{0}&{1}&{2}\\
{2}&{0}&{-1}\\
{2}&{1}&{0}\\
\end{bmatrix}
\]
\[乘,除,取余,取模等操作同上。
\]
arr1 = np.array([[0,0,0],[1,1,1],[2,2,2],[3,3,3]])
print("【arr1】\n",arr1,"形状:",arr1.shape,"\n")
arr2 = np.array([1,2,3]) # 1维度
print("【arr2】\n",arr2,"形状:",arr2.shape,"\n")
arr3 = arr1 + arr2
arr4 = arr1 - arr2
arr5 = arr1 * arr2
arr6 = arr1 / arr2
print("【arr3 = arr1 + arr2】\n",arr3,"形状:",arr3.shape,"\n")
print("【arr4 = arr1 - arr2】\n",arr4,"形状:",arr4.shape,"\n")
print("【arr5 = arr1 * arr2】\n",arr5,"形状:",arr5.shape,"\n")
print("【arr6 = arr1 / arr2】\n",arr6,"形状:",arr6.shape)
【arr1】
[[0 0 0]
[1 1 1]
[2 2 2]
[3 3 3]] 形状: (4, 3)
【arr2】
[1 2 3] 形状: (3,)
【arr3 = arr1 + arr2】
[[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]] 形状: (4, 3)
【arr4 = arr1 - arr2】
[[-1 -2 -3]
[ 0 -1 -2]
[ 1 0 -1]
[ 2 1 0]] 形状: (4, 3)
【arr5 = arr1 * arr2】
[[0 0 0]
[1 2 3]
[2 4 6]
[3 6 9]] 形状: (4, 3)
【arr6 = arr1 / arr2】
[[0. 0. 0. ]
[1. 0.5 0.33333333]
[2. 1. 0.66666667]
[3. 1.5 1. ]] 形状: (4, 3)
2维NumPy数组的广播机制
\[加法:
\begin{bmatrix}
{0}&{0}&{0}\\
{1}&{1}&{1}\\
{2}&{2}&{2}\\
{3}&{3}&{3}\\
\end{bmatrix}
+
\begin{bmatrix}
\color{red}{1}&\color{lightgrey}{1}&\color{lightgrey}{1}\\
\color{red}{2}&\color{lightgrey}{2}&\color{lightgrey}{2}\\
\color{red}{3}&\color{lightgrey}{3}&\color{lightgrey}{3}\\
\color{red}{4}&\color{lightgrey}{4}&\color{lightgrey}{4}\\
\end{bmatrix}
=
\begin{bmatrix}
{1}&{1}&{1}\\
{3}&{3}&{3}\\
{5}&{5}&{5}\\
{7}&{7}&{7}\\
\end{bmatrix}
\]
\[减法:
\begin{bmatrix}
{0}&{0}&{0}\\
{1}&{1}&{1}\\
{2}&{2}&{2}\\
{3}&{3}&{3}\\
\end{bmatrix}
-
\begin{bmatrix}
\color{red}{1}&\color{lightgrey}{1}&\color{lightgrey}{1}\\
\color{red}{2}&\color{lightgrey}{2}&\color{lightgrey}{2}\\
\color{red}{3}&\color{lightgrey}{3}&\color{lightgrey}{3}\\
\color{red}{4}&\color{lightgrey}{4}&\color{lightgrey}{4}\\
\end{bmatrix}
=
\begin{bmatrix}
{-1}&{-1}&{-1}\\
{-1}&{-1}&{-1}\\
{-1}&{-1}&{-1}\\
{-1}&{-1}&{-1}\\
\end{bmatrix}
\]
\[乘,除,取余,取模等操作同上。
\]
arr1 = np.array([[0,0,0],[1,1,1],[2,2,2],[3,3,3]])
print("【arr1】\n",arr1,"形状:",arr1.shape,"\n")
arr2 = np.array([[1],[2],[3],[4]]) # 2维度
print("【arr2】\n",arr2,"形状:",arr2.shape,"\n")
arr3 = arr1 + arr2
arr4 = arr1 - arr2
arr5 = arr1 * arr2
arr6 = arr1 / arr2
print("【arr3 = arr1 + arr2】\n",arr3,"形状:",arr3.shape,"\n")
print("【arr4 = arr1 - arr2】\n",arr4,"形状:",arr4.shape,"\n")
print("【arr5 = arr1 * arr2】\n",arr5,"形状:",arr5.shape,"\n")
print("【arr6 = arr1 / arr2】\n",arr6,"形状:",arr6.shape)
【arr1】
[[0 0 0]
[1 1 1]
[2 2 2]
[3 3 3]] 形状: (4, 3)
【arr2】
[[1]
[2]
[3]
[4]] 形状: (4, 1)
【arr3 = arr1 + arr2】
[[1 1 1]
[3 3 3]
[5 5 5]
[7 7 7]] 形状: (4, 3)
【arr4 = arr1 - arr2】
[[-1 -1 -1]
[-1 -1 -1]
[-1 -1 -1]
[-1 -1 -1]] 形状: (4, 3)
【arr5 = arr1 * arr2】
[[ 0 0 0]
[ 2 2 2]
[ 6 6 6]
[12 12 12]] 形状: (4, 3)
【arr6 = arr1 / arr2】
[[0. 0. 0. ]
[0.5 0.5 0.5 ]
[0.66666667 0.66666667 0.66666667]
[0.75 0.75 0.75 ]] 形状: (4, 3)
3维NumPy数组的广播机制
arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2)
print("【arr1】\n",arr1)
arr2 = np.array([0,1,2,3,4,5,6,7]).reshape(4,2)
print("【arr2】\n",arr2)
arr3 = arr1 + arr2
arr4 = arr1 - arr2
arr5 = arr1 * arr2
print("【arr3 = arr1 + arr2】\n",arr3,"形状:",arr3.shape,"\n")
print("【arr4 = arr1 - arr2】\n",arr4,"形状:",arr4.shape,"\n")
print("【arr5 = arr1 * arr2】\n",arr5,"形状:",arr5.shape,"\n")
【arr1】
[[[0 1]
[2 3]
[4 5]
[6 7]]
[[0 1]
[2 3]
[4 5]
[6 7]]
[[0 1]
[2 3]
[4 5]
[6 7]]]
【arr2】
[[0 1]
[2 3]
[4 5]
[6 7]]
【arr3 = arr1 + arr2】
[[[ 0 2]
[ 4 6]
[ 8 10]
[12 14]]
[[ 0 2]
[ 4 6]
[ 8 10]
[12 14]]
[[ 0 2]
[ 4 6]
[ 8 10]
[12 14]]] 形状: (3, 4, 2)
【arr4 = arr1 - arr2】
[[[0 0]
[0 0]
[0 0]
[0 0]]
[[0 0]
[0 0]
[0 0]
[0 0]]
[[0 0]
[0 0]
[0 0]
[0 0]]] 形状: (3, 4, 2)
【arr5 = arr1 * arr2】
[[[ 0 1]
[ 4 9]
[16 25]
[36 49]]
[[ 0 1]
[ 4 9]
[16 25]
[36 49]]
[[ 0 1]
[ 4 9]
[16 25]
[36 49]]] 形状: (3, 4, 2)