The term broadcasting refers to how numpy treats arrays with different Dimension during arithmetic operations which lead to certain constraints, the smaller array is broadcast across the larger array so that they have compatible shapes.
Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python as we know that Numpy implemented in C. It does this without making needless copies of data and which leads to efficient algorithm implementations. There are cases where broadcasting is a bad idea because it leads to inefficient use of memory that slow down the computation.
Example to get deeper understanding –
Let’s assume that we have a large data set, each datum is a list of parameters. In Numpy we have a 2-D array, where each row is a datum and the number of rows is the size of the data set. Suppose we want to apply some sort of scaling to all these data every parameter gets its own scaling factor or say Every parameter is multiplied by some factor.
Just to have some clear understanding, let’s count calories in foods using a macro-nutrient breakdown. Roughly put, the caloric parts of food are made of fats (9 calories per gram), protein (4 cpg) and carbs (4 cpg). So if we list some foods (our data), and for each food list its macro-nutrient breakdown (parameters), we can then multiply each nutrient by its caloric value (apply scaling) to compute the caloric breakdown of every food item.
With this transformation, we can now compute all kinds of useful information. For example, what is the total number of calories present in some food or, given a breakdown of my dinner know how much calories did I get from protein and so on.
Let’s see a naive way of producing this computation with Numpy:
array([[ 2.4, 8.7, 31.2 ], [ 157.2, 70.8, 292 ], [ 165.6, 95.1, 191.2], [ 43.2, 33, 39.2]])
Inputs: Array A with m dimensions and array B with n dimensions
p = max(m, n) if m < p: left-pad A's shape with 1s until it also has p dimensions else if n < p: left-pad B's shape with 1s until it also has p dimensions result_dims = new list with p elements for i in p-1 ... 0: A_dim_i = A.shape[i] B_dim_i = B.shape[i] if A_dim_i != 1 and B_dim_i != 1 and A_dim_i != B_dim_i: raise ValueError("could not broadcast") else: # Pick the Array which is having maximum Dimension result_dims[i] = max(A_dim_i, B_dim_i)
Broadcasting two arrays together follow these rules:
- If the arrays don't have the same rank then prepend the shape of the lower rank array with 1s until both shapes have the same length.
- The two arrays are compatible in a dimension if they have the same size in the dimension or if one of the arrays has size 1 in that dimension.
- The arrays can be broadcast together iff they are compatible with all dimensions.
- After broadcasting, each array behaves as if it had shape equal to the element-wise maximum of shapes of the two input arrays.
- In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension.
Example #1: Single Dimension array
[17 11 19] 3 [20 14 22]
Example 2: Two Dimensional Array
[[11 22 33] [10 20 30]] 4 [[15 26 37] [14 24 34]]
[[ 4 5] [ 8 10] [12 15]] [[2 4 6] [5 7 9]] [[ 5 6 7] [ 9 10 11]] [[ 5 6 7] [ 9 10 11]] [[ 2 4 6] [ 8 10 12]]
Plotting a two-dimensional function -
Broadcasting is also frequently used in displaying images based on two-dimensional functions. If we want to define a function z=f(x, y).
- Benefit of NumPy arrays over Python arrays
- Python | Numpy numpy.resize()
- Python | Numpy numpy.transpose()
- Python | Numpy numpy.ndarray.__lt__()
- Python | Numpy numpy.ndarray.__gt__()
- Python | Numpy numpy.ndarray.__le__()
- Python | Numpy numpy.ndarray.__ge__()
- Python | Numpy numpy.ndarray.__eq__()
- Python | Numpy numpy.ndarray.__ne__()
- Python | Numpy numpy.ndarray.__neg__()
- Python | Numpy numpy.ndarray.__pos__()
- Python | Numpy numpy.ndarray.__truediv__()
- Python | Numpy numpy.ndarray.__mul__()
- Python | Numpy numpy.ndarray.__sub__()
- Python | Numpy numpy.ndarray.__add__()
- Python | Numpy numpy.ndarray.__floordiv__()
- Python | Numpy numpy.ndarray.__mod__()
- Python | Numpy numpy.ndarray.__invert__()
- Python | Numpy numpy.ndarray.__divmod__()
- Python | Numpy numpy.ndarray.__pow__()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.