Updated On : Jul-13,2020 Time Investment : ~10 mins

Learning Numpy - Simple Tutorial For Beginners - NumPy - Broadcasting - Part 10

The term Broadcasting refers to the ability of NumPy to treat arrays of different shapes during arithmetic operations. Arithmetic operations on arrays are usually done on corresponding elements. If two arrays are of exactly the same shape, then these operations are smoothly performed.

Example 1

import numpy as np

a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c = (a * b)
print (c)
[ 10  40  90 160]

If the dimensions of two arrays are dissimilar, element-to-element operations are not possible. However, operations on arrays of non-similar shapes is still possible in NumPy, because of the broadcasting capability. The smaller array is broadcast to the size of the larger array so that they have compatible shapes.

What Are Some Of The Broadcasting Rules?

  • Array with smaller ndim than the other is prepended with '1' in its shape.

  • Size in each dimension of the output shape is maximum of the input sizes in that dimension.

  • An input can be used in calculation, if its size in a particular dimension matches the output size or its value is exactly 1.

  • If an input has a dimension size of 1, the first data entry in that dimension is used for all calculations along that dimension.

  • A set of arrays is said to be broadcastable if the above rules produce a valid result and one of the following is true −

  • Arrays have exactly the same shape.

  • Arrays have the same number of dimensions and the length of each dimension is either a common length or 1.

  • Array having too few dimensions can have its shape prepended with a dimension of length 1, so that the above stated property is true.

Example 2

import numpy as np
a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])

print ('First array:')
print (a)
print ('\n')

print ('Second array:')
print (b)
print ('\n')

print ('First Array + Second Array')
print (a + b)
First array:
[[ 0.  0.  0.]
 [10. 10. 10.]
 [20. 20. 20.]
 [30. 30. 30.]]

Second array:
[1. 2. 3.]

First Array + Second Array
[[ 1.  2.  3.]
 [11. 12. 13.]
 [21. 22. 23.]
 [31. 32. 33.]]
Dolly Solanki  Dolly Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.

Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription