Prev: Array slicing | Next: Array functions

- Much of the power of arrays come from the flexibility of applying fast element-wise and array-wise operations.

- Arithmetic operations with scalars work element-wise on all array member items.

In [1]:

```
import numpy as np
ar = np.arange(10)
print(ar)
br = ar+1
cr = ar-1
dr = ar*2
er = ar//2
br, cr, dr, er
```

Out[1]:

In [2]:

```
import numpy as np
ar = np.arange(1,6)
br = np.arange(10,60,10)
cr = np.arange(100,600,100)
print(ar, br, cr)
```

In [3]:

```
ar+br+cr
```

Out[3]:

In [4]:

```
cr-br
```

Out[4]:

In [5]:

```
ar*cr
```

Out[5]:

In [6]:

```
cr/ar
```

Out[6]:

In [7]:

```
import numpy as np
ar = np.arange(10).reshape(2,5)
br = ar * 10
print(ar,'\n', br)
```

In [8]:

```
ar * br
```

Out[8]:

In [9]:

```
ar + br
```

Out[9]:

In [10]:

```
ar - br
```

Out[10]:

- What happens if arrays are of
*different shapes*? - Numpy tries to bring arrays in '
**compatible**' shapes (if possible). The computational techniques that numpy implements on arrays in order to make them compatible are known as "**broadcasting**". - When arrays can not be given compatible shapes ('cannot be broadcast together') then a relevant ValueError is reported

In [11]:

```
import numpy as np
a = np.array([1,2,3,4])
b = 2
print(a*b)
br = [2,2,2,2]
print(a*br)
```

- In the simplest case of broadcasting an integer (variable b) is handled "as if" it was an one-dimensional array with N members (all of them with the same value)
- In the example above a*b multiplication produces the same outcome as a*br, where br an array like the 'broadcast' form of b integer.

- In order for numpy to succesfully broadcast two arrays: the size of the corresponding axes for both arrays (starting from the trailing axes and moving upward)
**must either be of the***same size*or*one of them must be '1'* - In the example below, ar and br broadcast as shapes (1,4) and (4,1) taken in axes-pairs conform to the broadcast rule: "at least one of the axes is of size '1'"

In [12]:

```
import numpy as np
ar = np.arange(1,5).reshape(1,4)
br = np.arange(1,5).reshape(4,1)
print(ar,'\n',br)
ar*br
```

Out[12]:

- Arrays a (2,3) and b (3,) broadcast together since they have trailing axes of the same size (3)

In [13]:

```
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.array([5,10,15])
a * b
```

Out[13]:

- Arrays ar with shape (2,3) and br with shape (3,1) do NOT broadcast. The trailing axis is OK (size of br is '1'). However, they are not compatible in the next axis where the ar size is 2 and the br size is 3.
- How would you reshape a so that they broadcast?

In [14]:

```
import numpy as np
ar = np.array([[1,2,3],[4,5,6]])
br = np.array([5,10,15]).reshape(3,1)
ar * br
```

In [15]:

```
import numpy as np
ar = np.array([[1,2,3,4],[4,5,6,8]]).reshape(2,1,4)
br = np.arange(10, 170, 10).reshape(2,8,1)
ar * br
```

Out[15]:

In [16]:

```
import numpy as np
ar = np.array([[1,2,3],[4,5,6],[7,8,9]])
br = np.array([5,10,15])
ar + br
```

Out[16]:

. Free learning material

. See full copyright and disclaimer notice