Menu bar

07/08/2021

NumPy Array Broadcasting

Arrays with difference sizes cannot be added, subtracted, or generally be used in arithmetic. A way to overcome this is to duplicate the smaller array so that it has the dimensionality and size as the large array. This is called array broadcasting. 


Limitation with Array Arithmetic

Arithmetic may only be performed on arrays that have the same dimensions and dimensions with the same size. This means that a one-dimensional array with the length of 10 can only perform arithmetic with another one-dimension array with the length 10. This limitation on array arithmetic is quite limiting indeed. 


Array Broadcasting 

Broadcasting is a the name given to the method that Numpy uses to allow array arithmetic between arrays with a difference shape of size. Although the technique was developed for Numpy, it has also been adopted more broadly in other numerical computational libraries, such as Theano, TensorFlow and Octave.


Broadcasting In Numpy

We can make broadcasting concrete by looking at three examples in NumPy.

Scalar and One-Dimensional Array

# broadcast scalar to one-dimensional array
from numpy import array
# define array
a = array([1, 2, 3])
print(a)
# define scalar
b = 2
print(b)
# broadcast
c = a + b
print(c)


-----Result-----

[1 2 3]
2
[3 4 5]



Scalar and Two-Dimensional Array

# broadcast scalar to two-dimensional array
from numpy import array
# define array
A = array([
[1, 2, 3],
[1, 2, 3]])
print(A)
# define scalar
b = 2
print(b)
# broadcast
C = A + b
print(C)


-----Result-----

[[1 2 3]
[1 2 3]]
2
[[3 4 5]
[3 4 5]]



One-Dimensional and Two-Dimensional Arrays

# broadcast one-dimensional array to two-dimensional array
from numpy import array
# define two-dimensional array
A = array([
[1, 2, 3],
[1, 2, 3]])
print(A)
# define one-dimensional array
b = array([1, 2, 3])
print(b)
# broadcast
C = A + b
print(C)


-----Result-----

[[1 2 3]
[1 2 3]]
[1 2 3]
[[2 4 6]
[2 4 6]]



Limitations of Broadcasting

Arithmetic, including broadcasting, can only be performed when the shape of each dimension in the arrays are equal or one has the
dimension size of 1. The dimensions are considered in reverse order, starting with the trailing dimension; for example, looking at columns before rows in a two-dimensional case.


# broadcasting error
from numpy import array
# define two-dimensional array
A = array([
[1, 2, 3],
[1, 2, 3]])
print(A.shape)
# define one-dimensional array
b = array([1, 2])
print(b.shape)
# attempt broadcast
C = A + b
print(C)


-----Result-----

(2, 3)
(2,)
ValueError: operands could not be broadcast together with shapes (2,3) (2,)


No comments:

Post a Comment