broadcasting.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. """
  2. ========================
  3. Broadcasting over arrays
  4. ========================
  5. .. note::
  6. See `this article
  7. <https://numpy.org/devdocs/user/theory.broadcasting.html>`_
  8. for illustrations of broadcasting concepts.
  9. The term broadcasting describes how numpy treats arrays with different
  10. shapes during arithmetic operations. Subject to certain constraints,
  11. the smaller array is "broadcast" across the larger array so that they
  12. have compatible shapes. Broadcasting provides a means of vectorizing
  13. array operations so that looping occurs in C instead of Python. It does
  14. this without making needless copies of data and usually leads to
  15. efficient algorithm implementations. There are, however, cases where
  16. broadcasting is a bad idea because it leads to inefficient use of memory
  17. that slows computation.
  18. NumPy operations are usually done on pairs of arrays on an
  19. element-by-element basis. In the simplest case, the two arrays must
  20. have exactly the same shape, as in the following example:
  21. >>> a = np.array([1.0, 2.0, 3.0])
  22. >>> b = np.array([2.0, 2.0, 2.0])
  23. >>> a * b
  24. array([ 2., 4., 6.])
  25. NumPy's broadcasting rule relaxes this constraint when the arrays'
  26. shapes meet certain constraints. The simplest broadcasting example occurs
  27. when an array and a scalar value are combined in an operation:
  28. >>> a = np.array([1.0, 2.0, 3.0])
  29. >>> b = 2.0
  30. >>> a * b
  31. array([ 2., 4., 6.])
  32. The result is equivalent to the previous example where ``b`` was an array.
  33. We can think of the scalar ``b`` being *stretched* during the arithmetic
  34. operation into an array with the same shape as ``a``. The new elements in
  35. ``b`` are simply copies of the original scalar. The stretching analogy is
  36. only conceptual. NumPy is smart enough to use the original scalar value
  37. without actually making copies, so that broadcasting operations are as
  38. memory and computationally efficient as possible.
  39. The code in the second example is more efficient than that in the first
  40. because broadcasting moves less memory around during the multiplication
  41. (``b`` is a scalar rather than an array).
  42. General Broadcasting Rules
  43. ==========================
  44. When operating on two arrays, NumPy compares their shapes element-wise.
  45. It starts with the trailing dimensions, and works its way forward. Two
  46. dimensions are compatible when
  47. 1) they are equal, or
  48. 2) one of them is 1
  49. If these conditions are not met, a
  50. ``ValueError: operands could not be broadcast together`` exception is
  51. thrown, indicating that the arrays have incompatible shapes. The size of
  52. the resulting array is the maximum size along each dimension of the input
  53. arrays.
  54. Arrays do not need to have the same *number* of dimensions. For example,
  55. if you have a ``256x256x3`` array of RGB values, and you want to scale
  56. each color in the image by a different value, you can multiply the image
  57. by a one-dimensional array with 3 values. Lining up the sizes of the
  58. trailing axes of these arrays according to the broadcast rules, shows that
  59. they are compatible::
  60. Image (3d array): 256 x 256 x 3
  61. Scale (1d array): 3
  62. Result (3d array): 256 x 256 x 3
  63. When either of the dimensions compared is one, the other is
  64. used. In other words, dimensions with size 1 are stretched or "copied"
  65. to match the other.
  66. In the following example, both the ``A`` and ``B`` arrays have axes with
  67. length one that are expanded to a larger size during the broadcast
  68. operation::
  69. A (4d array): 8 x 1 x 6 x 1
  70. B (3d array): 7 x 1 x 5
  71. Result (4d array): 8 x 7 x 6 x 5
  72. Here are some more examples::
  73. A (2d array): 5 x 4
  74. B (1d array): 1
  75. Result (2d array): 5 x 4
  76. A (2d array): 5 x 4
  77. B (1d array): 4
  78. Result (2d array): 5 x 4
  79. A (3d array): 15 x 3 x 5
  80. B (3d array): 15 x 1 x 5
  81. Result (3d array): 15 x 3 x 5
  82. A (3d array): 15 x 3 x 5
  83. B (2d array): 3 x 5
  84. Result (3d array): 15 x 3 x 5
  85. A (3d array): 15 x 3 x 5
  86. B (2d array): 3 x 1
  87. Result (3d array): 15 x 3 x 5
  88. Here are examples of shapes that do not broadcast::
  89. A (1d array): 3
  90. B (1d array): 4 # trailing dimensions do not match
  91. A (2d array): 2 x 1
  92. B (3d array): 8 x 4 x 3 # second from last dimensions mismatched
  93. An example of broadcasting in practice::
  94. >>> x = np.arange(4)
  95. >>> xx = x.reshape(4,1)
  96. >>> y = np.ones(5)
  97. >>> z = np.ones((3,4))
  98. >>> x.shape
  99. (4,)
  100. >>> y.shape
  101. (5,)
  102. >>> x + y
  103. ValueError: operands could not be broadcast together with shapes (4,) (5,)
  104. >>> xx.shape
  105. (4, 1)
  106. >>> y.shape
  107. (5,)
  108. >>> (xx + y).shape
  109. (4, 5)
  110. >>> xx + y
  111. array([[ 1., 1., 1., 1., 1.],
  112. [ 2., 2., 2., 2., 2.],
  113. [ 3., 3., 3., 3., 3.],
  114. [ 4., 4., 4., 4., 4.]])
  115. >>> x.shape
  116. (4,)
  117. >>> z.shape
  118. (3, 4)
  119. >>> (x + z).shape
  120. (3, 4)
  121. >>> x + z
  122. array([[ 1., 2., 3., 4.],
  123. [ 1., 2., 3., 4.],
  124. [ 1., 2., 3., 4.]])
  125. Broadcasting provides a convenient way of taking the outer product (or
  126. any other outer operation) of two arrays. The following example shows an
  127. outer addition operation of two 1-d arrays::
  128. >>> a = np.array([0.0, 10.0, 20.0, 30.0])
  129. >>> b = np.array([1.0, 2.0, 3.0])
  130. >>> a[:, np.newaxis] + b
  131. array([[ 1., 2., 3.],
  132. [ 11., 12., 13.],
  133. [ 21., 22., 23.],
  134. [ 31., 32., 33.]])
  135. Here the ``newaxis`` index operator inserts a new axis into ``a``,
  136. making it a two-dimensional ``4x1`` array. Combining the ``4x1`` array
  137. with ``b``, which has shape ``(3,)``, yields a ``4x3`` array.
  138. """
  139. from __future__ import division, absolute_import, print_function