广播
# 什么是广播
广播是指不同形状的数组之间的算术运算的执行方式。受限于某些约束,较小的数组依据较大数组“broadcasting”,使得它们具有兼容的形状。
# 广播的原则
如果两个数组的后缘维度(即从末尾开始算起的维度)的轴长度相符或其中一方的长度为 1,则认为它们是广播兼容的。广播会在缺失和(或)长度为 1 的维度上进行。
import numpy as np
arr1 = np.arange(24).reshape(3,4,2)
arr2 = np.array([[4],
[4],
[4],
[4]])
1
2
3
4
5
6
7
2
3
4
5
6
7
arr1
arr2
1
2
2
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11],
[12, 13],
[14, 15]],
[[16, 17],
[18, 19],
[20, 21],
[22, 23]]])
array([[4],
[4],
[4],
[4]])
arr1 * 3
1
array([[[ 0, 3],
[ 6, 9],
[12, 15],
[18, 21]],
[[24, 27],
[30, 33],
[36, 39],
[42, 45]],
[[48, 51],
[54, 57],
[60, 63],
[66, 69]]])
arr1.shape
arr2.shape
1
2
2
(3, 4, 2)
(4, 1)
arr1 - arr2
1
array([[[-4, -3],
[-2, -1],
[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15],
[16, 17],
[18, 19]]])
arr3 = np.arange(48).reshape((8, 1, 6, 1))
arr4 = np.arange(35).reshape((7, 1, 5))
arr3.shape
arr4.shape
1
2
3
4
2
3
4
(8, 1, 6, 1)
(7, 1, 5)
arr3 - arr4
1
array([[[[ 0, -1, -2, -3, -4],
[ 1, 0, -1, -2, -3],
[ 2, 1, 0, -1, -2],
[ 3, 2, 1, 0, -1],
[ 4, 3, 2, 1, 0],
[ 5, 4, 3, 2, 1]],
[[ -5, -6, -7, -8, -9],
[ -4, -5, -6, -7, -8],
[ -3, -4, -5, -6, -7],
[ -2, -3, -4, -5, -6],
[ -1, -2, -3, -4, -5],
[ 0, -1, -2, -3, -4]],
[[-10, -11, -12, -13, -14],
[ -9, -10, -11, -12, -13],
[ -8, -9, -10, -11, -12],
[ -7, -8, -9, -10, -11],
[ -6, -7, -8, -9, -10],
[ -5, -6, -7, -8, -9]],
...,
[[-20, -21, -22, -23, -24],
[-19, -20, -21, -22, -23],
[-18, -19, -20, -21, -22],
[-17, -18, -19, -20, -21],
[-16, -17, -18, -19, -20],
[-15, -16, -17, -18, -19]],
[[-25, -26, -27, -28, -29],
[-24, -25, -26, -27, -28],
[-23, -24, -25, -26, -27],
[-22, -23, -24, -25, -26],
[-21, -22, -23, -24, -25],
[-20, -21, -22, -23, -24]],
[[-30, -31, -32, -33, -34],
[-29, -30, -31, -32, -33],
[-28, -29, -30, -31, -32],
[-27, -28, -29, -30, -31],
[-26, -27, -28, -29, -30],
[-25, -26, -27, -28, -29]]],
[[[ 6, 5, 4, 3, 2],
[ 7, 6, 5, 4, 3],
[ 8, 7, 6, 5, 4],
[ 9, 8, 7, 6, 5],
[ 10, 9, 8, 7, 6],
[ 11, 10, 9, 8, 7]],
[[ 1, 0, -1, -2, -3],
[ 2, 1, 0, -1, -2],
[ 3, 2, 1, 0, -1],
[ 4, 3, 2, 1, 0],
[ 5, 4, 3, 2, 1],
[ 6, 5, 4, 3, 2]],
[[ -4, -5, -6, -7, -8],
[ -3, -4, -5, -6, -7],
[ -2, -3, -4, -5, -6],
[ -1, -2, -3, -4, -5],
[ 0, -1, -2, -3, -4],
[ 1, 0, -1, -2, -3]],
...,
[[-14, -15, -16, -17, -18],
[-13, -14, -15, -16, -17],
[-12, -13, -14, -15, -16],
[-11, -12, -13, -14, -15],
[-10, -11, -12, -13, -14],
[ -9, -10, -11, -12, -13]],
[[-19, -20, -21, -22, -23],
[-18, -19, -20, -21, -22],
[-17, -18, -19, -20, -21],
[-16, -17, -18, -19, -20],
[-15, -16, -17, -18, -19],
[-14, -15, -16, -17, -18]],
[[-24, -25, -26, -27, -28],
[-23, -24, -25, -26, -27],
[-22, -23, -24, -25, -26],
[-21, -22, -23, -24, -25],
[-20, -21, -22, -23, -24],
[-19, -20, -21, -22, -23]]],
[[[ 12, 11, 10, 9, 8],
[ 13, 12, 11, 10, 9],
[ 14, 13, 12, 11, 10],
[ 15, 14, 13, 12, 11],
[ 16, 15, 14, 13, 12],
[ 17, 16, 15, 14, 13]],
[[ 7, 6, 5, 4, 3],
[ 8, 7, 6, 5, 4],
[ 9, 8, 7, 6, 5],
[ 10, 9, 8, 7, 6],
[ 11, 10, 9, 8, 7],
[ 12, 11, 10, 9, 8]],
[[ 2, 1, 0, -1, -2],
[ 3, 2, 1, 0, -1],
[ 4, 3, 2, 1, 0],
[ 5, 4, 3, 2, 1],
[ 6, 5, 4, 3, 2],
[ 7, 6, 5, 4, 3]],
...,
[[ -8, -9, -10, -11, -12],
[ -7, -8, -9, -10, -11],
[ -6, -7, -8, -9, -10],
[ -5, -6, -7, -8, -9],
[ -4, -5, -6, -7, -8],
[ -3, -4, -5, -6, -7]],
[[-13, -14, -15, -16, -17],
[-12, -13, -14, -15, -16],
[-11, -12, -13, -14, -15],
[-10, -11, -12, -13, -14],
[ -9, -10, -11, -12, -13],
[ -8, -9, -10, -11, -12]],
[[-18, -19, -20, -21, -22],
[-17, -18, -19, -20, -21],
[-16, -17, -18, -19, -20],
[-15, -16, -17, -18, -19],
[-14, -15, -16, -17, -18],
[-13, -14, -15, -16, -17]]],
...,
[[[ 30, 29, 28, 27, 26],
[ 31, 30, 29, 28, 27],
[ 32, 31, 30, 29, 28],
[ 33, 32, 31, 30, 29],
[ 34, 33, 32, 31, 30],
[ 35, 34, 33, 32, 31]],
[[ 25, 24, 23, 22, 21],
[ 26, 25, 24, 23, 22],
[ 27, 26, 25, 24, 23],
[ 28, 27, 26, 25, 24],
[ 29, 28, 27, 26, 25],
[ 30, 29, 28, 27, 26]],
[[ 20, 19, 18, 17, 16],
[ 21, 20, 19, 18, 17],
[ 22, 21, 20, 19, 18],
[ 23, 22, 21, 20, 19],
[ 24, 23, 22, 21, 20],
[ 25, 24, 23, 22, 21]],
...,
[[ 10, 9, 8, 7, 6],
[ 11, 10, 9, 8, 7],
[ 12, 11, 10, 9, 8],
[ 13, 12, 11, 10, 9],
[ 14, 13, 12, 11, 10],
[ 15, 14, 13, 12, 11]],
[[ 5, 4, 3, 2, 1],
[ 6, 5, 4, 3, 2],
[ 7, 6, 5, 4, 3],
[ 8, 7, 6, 5, 4],
[ 9, 8, 7, 6, 5],
[ 10, 9, 8, 7, 6]],
[[ 0, -1, -2, -3, -4],
[ 1, 0, -1, -2, -3],
[ 2, 1, 0, -1, -2],
[ 3, 2, 1, 0, -1],
[ 4, 3, 2, 1, 0],
[ 5, 4, 3, 2, 1]]],
[[[ 36, 35, 34, 33, 32],
[ 37, 36, 35, 34, 33],
[ 38, 37, 36, 35, 34],
[ 39, 38, 37, 36, 35],
[ 40, 39, 38, 37, 36],
[ 41, 40, 39, 38, 37]],
[[ 31, 30, 29, 28, 27],
[ 32, 31, 30, 29, 28],
[ 33, 32, 31, 30, 29],
[ 34, 33, 32, 31, 30],
[ 35, 34, 33, 32, 31],
[ 36, 35, 34, 33, 32]],
[[ 26, 25, 24, 23, 22],
[ 27, 26, 25, 24, 23],
[ 28, 27, 26, 25, 24],
[ 29, 28, 27, 26, 25],
[ 30, 29, 28, 27, 26],
[ 31, 30, 29, 28, 27]],
...,
[[ 16, 15, 14, 13, 12],
[ 17, 16, 15, 14, 13],
[ 18, 17, 16, 15, 14],
[ 19, 18, 17, 16, 15],
[ 20, 19, 18, 17, 16],
[ 21, 20, 19, 18, 17]],
[[ 11, 10, 9, 8, 7],
[ 12, 11, 10, 9, 8],
[ 13, 12, 11, 10, 9],
[ 14, 13, 12, 11, 10],
[ 15, 14, 13, 12, 11],
[ 16, 15, 14, 13, 12]],
[[ 6, 5, 4, 3, 2],
[ 7, 6, 5, 4, 3],
[ 8, 7, 6, 5, 4],
[ 9, 8, 7, 6, 5],
[ 10, 9, 8, 7, 6],
[ 11, 10, 9, 8, 7]]],
[[[ 42, 41, 40, 39, 38],
[ 43, 42, 41, 40, 39],
[ 44, 43, 42, 41, 40],
[ 45, 44, 43, 42, 41],
[ 46, 45, 44, 43, 42],
[ 47, 46, 45, 44, 43]],
[[ 37, 36, 35, 34, 33],
[ 38, 37, 36, 35, 34],
[ 39, 38, 37, 36, 35],
[ 40, 39, 38, 37, 36],
[ 41, 40, 39, 38, 37],
[ 42, 41, 40, 39, 38]],
[[ 32, 31, 30, 29, 28],
[ 33, 32, 31, 30, 29],
[ 34, 33, 32, 31, 30],
[ 35, 34, 33, 32, 31],
[ 36, 35, 34, 33, 32],
[ 37, 36, 35, 34, 33]],
...,
[[ 22, 21, 20, 19, 18],
[ 23, 22, 21, 20, 19],
[ 24, 23, 22, 21, 20],
[ 25, 24, 23, 22, 21],
[ 26, 25, 24, 23, 22],
[ 27, 26, 25, 24, 23]],
[[ 17, 16, 15, 14, 13],
[ 18, 17, 16, 15, 14],
[ 19, 18, 17, 16, 15],
[ 20, 19, 18, 17, 16],
[ 21, 20, 19, 18, 17],
[ 22, 21, 20, 19, 18]],
[[ 12, 11, 10, 9, 8],
[ 13, 12, 11, 10, 9],
[ 14, 13, 12, 11, 10],
[ 15, 14, 13, 12, 11],
[ 16, 15, 14, 13, 12],
[ 17, 16, 15, 14, 13]]]])
上次更新: 2023/11/01, 03:11:44