## Broadcasting

- Generally arithmetic operations are performed between two arrays are having same dimension, shape and size.
- Eventhough dimensions are different,shapes are different and sizes are different still some arithmetic operations are allowed by Broadcasting.
- Broadcasting will be performed automatically by numpy itself and we are not required to perform explicitly.
- Broadcasting won’t be possible in all cases.
- Numpy follow some rules to perform broadcasting. If the rules are satisfied then only broadcasting will be performed internally while performing arithmetic operations.

#### Note

- If both arrays have same dimension, same shape and same size then broadcasting is not required.
- Different dimensions or different shapes or different sizes then only broadcasting is required.

### Rules of Broadcasting

##### Rule-1: Make sure both arrays should have same dimension.

- If the two arrays are of different dimensions, numpy will make equal dimensions. Padded 1’s in the shape of lesser dimension array on the left side, until both arrays have same diemension.

#### Eg: Before

- a array has shape :: (4,3) :::: 2-D array.
- b array has shape :: (3,) :::: 1-D array.

### After

- Both arrays a and b are different dimensions.
- By Rule-1 Numpy will add 1’s to the lesser dimension array(here array b).Now the array a is ::: (4,3).
- and the array b becomes ::: (1,3) — By using Rule-1.

Now Both arrays are in same dimension array a:: 2-D and array-b :: 2-D.

- a array has shape :: (4,3) :::: 2-D array.
- b array has shape :: (1,3) :::: 2-D array.

### Rule-2:

- If the size of two arrays does not match in any dimension, then the arrays with size equal to 1 in that dimension will be increased to size of other dimension to match.

#### Eg:

- From Rule-1 we got a ==> (4,3) and b ==> (1,3).
- Here first co-ordinate of a => 4 and b => 1. Sizes are different.
- According to Rule-2 the array with size 1 (here arrray b with size 1) will be increased to 4 (corresponding size of array a).
- Second co-ordinate of a => 3 and b => 3. Sizes are matched.
- After applying Rule-2 the dimensions of a and b are changed as follows.
- a array has shape ==> (4,3) ====> 2-D array.
- b array has shape ==> (4,3) ====> 2-D array.
- Now both the arrays are having same dimension, shape and size. So we can perform any arithmetic operations.

#### Note

- In any dimension, the sizes are not matched and neither equal to 1, then we will get error, Numpy does not able to perform broadcasting between those arrays.
- The data will be reused from the same input array.
- If the rows are required then reuse existing rows.
- If columns are required then reuse existing columns.
- The result is always higher dimension of input arrays.
- Eg: Broadcasting between (3,2,2) and (3,) possible or not.

### Before applying Rule-1

- a :: (3,2,2) – 3-D array.
- b :: (3,) – 1-D array.
- Here bothe are different dimensions. By applying the Rule-1 the Numpy changes the array b as (1,1,3).

### After applying Rule-1

- a :: (3,2,2) – 3-D array.
- b :: (1,1,3) – 3-D array.
- Now both arrays are in same dimension.

### Before applying Rule-2

- a :: (3,2,2) – 3-D array.
- b :: (1,1,3) – 3-D array.
- By applying Rule-2 Numpy changes the array b to (3,2,3) because it has size 1’s. It will be replaced with corresponding sizes of the arrray a.

### After applying Rule-2

- a :: (3,2,2) – 3-D array.
- b :: (3,2,3) – 3-D array.
- Now here array a and b are having same dimensions, but different shapes. So Numpy unable to perform broadcasting.

#### Example

Python

```
# Broadcasting between 1-D arrays of different sizes
import numpy as np
a = np.array([10,20,30,40])
b = np.array([1,2,3])
# a : (4,) b: (3,).
# Both are having same diemensions. But sizes are different so arithmetic operation is not performed
# Broadcasting by Numpy will be failed while performing arithmetic operation in this case
a+b
```

**Output**

PowerShell

```
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-199-e084bfeb9eb8> in <module>
6 # Both are having same diemensions. But sizes are different so arithm
etic operation is not performed
7 # Broadcasting by Numpy will be failed while performing arithmetic op
eration in this case
----> 8 a+b
ValueError: operands could not be broadcast together with shapes (4,) (3,)
```

#### Example

Python

```
# Broadcasting between 1-D arrays
a = np.array([10,20,30])
b = np.array([40])
print(f"Shape of a : {a.shape}")
print(f"Shape of b : {b.shape}")
print(f"a+b : {a+b}")
```

**Output**

PowerShell

```
Shape of a : (3,)
Shape of b : (1,)
a+b : [50 60 70]
```

figar banana h

#### Example

Python

```
# Broadcasting between 2-D and 1-D arrays
a = np.array([[10,20],[30,40],[50,60]])
b = np.array([10,20])
print(f"Shape of the array a : {a.shape}")
print(f"Shape of the array b : {b.shape}")
print(f"array a :\n {a}")
print(f"array b :\n {b}")
print("Arithmetic operations :")
```

**Output**

PowerShell

`output nhi diya h`

#### Example

Python

```
print(f"Addition operation a+b :\n {a+b}")
print(f"Subtraction operation a-b :\n {a-b}")
print(f"Multiplication operation a*b:\n {a*b}")
print(f"Division operation a/b:\n {a/b}")
print(f"Floor division operation a//b:\n {a//b}")
print(f"Modulo operation a%b:\n {a%b}")
```

**Output**

PowerShell

```
Shape of the array a : (3, 2)
Shape of the array b : (2,)
array a :
[[10 20]
[30 40]
[50 60]]
array b :
[10 20]
Arithmetic operations :
Addition operation a+b :
[[20 40]
[40 60]
[60 80]]
Subtraction operation a-b
[[ 0 0]
[20 20]
[40 40]]
Multiplication operation a*b:
[[ 100 400]
[ 300 800]
[ 500 1200]]
Division operation a/b:
[[1. 1.]
[3. 2.]
[5. 3.]]
Floor division operation a//b:
[[1 1]
[3 2]
[5 3]]
Modulo operation a%b:
[[0 0]
[0 0]
[0 0]]
```

figar banana

#### Example

Python

```
# Broadcasting between 1-D array and 2-D array
a = np.array([[10],[20],[30]])
b = np.array([10,20,30])
print(f"Shape of the array a : {a.shape}")
print(f"Shape of the array b : {b.shape}")
print(f"array a :\n {a}")
print(f"array b :\n {b}")
print("Arithmetic operations :")
print(f"Addition operation a+b :\n {a+b}")
print(f"Subtraction operation a-b :\n {a-b}")
print(f"Multiplication operation a*b:\n {a*b}")
print(f"Division operation a/b:\n {a/b}")
print(f"Floor division operation a//b:\n {a//b}")
print(f"Modulo operation a%b:\n {a%b}")
```

**Output**

PowerShell

```
Shape of the array a : (3, 1)
Shape of the array b : (3,)
array a :
[[10]
[20]
[30]]
array b :
[10 20 30]
Arithmetic operations :
Addition operation a+b :
[[20 30 40]
[30 40 50]
[40 50 60]]
Subtraction operation a-b :
[[ 0 -10 -20]
[ 10 0 -10]
[ 20 10 0]]
Multiplication operation a*b:
[[100 200 300]
[200 400 600]
[300 600 900]]
Division operation a/b:
[[1. 0.5 0.33333333]
[2. 1. 0.66666667]
[3. 1.5 1. ]]
Floor division operation a//b:
[[1 0 0]
[2 1 0]
[3 1 1]]
Modulo operation a%b:
[[ 0 10 10]
[ 0 0 20]
[ 0 10 0]]
```

figar banana h