broadcasting.py 5.4 KB

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