- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Broadcasting with NumPy Arrays in Python

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.

## Rules of array Boradcasting

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.

## Example

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 '

' print 'Second array:' print b print '

' print 'First Array + Second Array' print a + b

## Output

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

- Related Questions & Answers
- Discuss broadcasting in Numpy in Python?
- Add two vectors using broadcasting in Numpy
- Join a sequence of Numpy arrays with stack()
- What is Broadcasting in Computer Network?
- Convert inputs to arrays with at least one dimension in Numpy
- Convert inputs to arrays with at least two dimensions in Numpy
- Convert inputs to arrays with at least three dimensions in Numpy
- Matrix product of two arrays in Numpy
- Join a sequence of arrays with stack() over specific axis in Numpy
- Join a sequence of arrays with stack() over negative axis in Numpy
- Join a sequence of Numpy arrays with stack() over axis 0
- Join a sequence of Numpy arrays with stack() over axis 1
- Concatenate a sequence of masked arrays in Numpy
- Join a sequence of masked arrays in Numpy
- Append masked arrays along axis 1 in Numpy