- Related Questions & Answers
- Discuss broadcasting in Numpy in Python?
- How to find intersection between two Numpy arrays?
- Discuss how the sort function can be applied on NumPy arrays in Python?
- How to find set difference between two Numpy arrays?
- What is Broadcasting in Computer Network?
- Linear regression with Matplotlib/Numpy
- Plot numpy datetime64 with Matplotlib
- Find elements within range in numpy in Python
- Basic Slicing and Advanced Indexing in NumPy Python
- Multiplication of two Matrices using Numpy in Python
- Python program to reverse a Numpy array?
- Change data type of given numpy array in Python
- Python - Filter out integers from float numpy array
- Overlay an image segmentation with Numpy and Matplotlib
- Flatten a 2d numpy array into 1d array in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We know the arithmetic operations between different arrays happens monthly if the arrays are of equal size awesome required specific size. But there are scenarios when we can take erase of unequal size and still apply arithmetic operations on them by enhancing one of the arrays by filling array with smaller ndim prepended with '1' in its shape. So basically broadcasting and array means changing its shape to any required shape.

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.

The below example shows how the broadcasting happens during array manipulation using numpy arrays.

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

Running the above code gives us the following result −

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.] ]

Advertisements