Exercise 1

Use np.arange and reshape to create the array

A = [[1 2 3 4]
     [5 6 7 8]]

In [1]:
import numpy as np
A = np.arange(1, 9).reshape(2, -1)
print A


[[1 2 3 4]
 [5 6 7 8]]

Use np.array to create the array

B = [1 2]

In [2]:
B = np.array([1, 2])
print B


[1 2]

Use broadcasting to add B to A to create the final array

A + B = [[2  3  4  5]
         [7  8  9 10]

Hint: what shape does B have to be changed to?


In [3]:
print A + B.reshape(2, 1)


[[ 2  3  4  5]
 [ 7  8  9 10]]