This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 2: NumPy arrays

Array creation routines

face Josiah Wang Joe Stacey

NumPy provides some functions to easily create some simple arrays. For example, you can create a np.array of zeros easily with the np.zeros() function, passing the shape of your array (a tuple) as a parameter. This is useful when you want to preallocate an array, but will only fill it up later with values.

>>> x = np.zeros((2, 3))
>>> print(x)
[[0. 0. 0.]
 [0. 0. 0.]]

Note the the dtype is a float and not an int.

Now, examine the array creation functions in the official NumPy documentation. Then, try to generate the array listed in the quiz questions below, using the functions from the page (you will need only the ones under the “From shape or value” heading).

1 2 3 4 5 6

Question 1

Write a function call to generate a np.array that looks like the output below. Your answer should be something like np.zeros((2, 3)).

[[1. 1.]
 [1. 1.]
 [1. 1.]]

np.ones((3, 2))
Explanation:

The answer is np.ones((3, 2)). Quite straightforward!

Question 2

Write a function call to generate a np.array that looks like the output below. Your answer should be something like np.zeros((2, 3)).

[[5. 5.]
 [5. 5.]
 [5. 5.]]

np.full((3, 2), 5.)
Explanation:

One answer is np.full((3, 2), 5.), which creates a np.array filled with the given number. Another possible answer is np.ones((3, 2)) * 5, where you create an array of ones, and multiply each element by 5.

Question 3

Write a function call to generate a np.array that looks like the output below. Your answer should be something like np.zeros((2, 3)).

[0. 0.]

np.zeros(2)
Explanation:

The answer can be either np.zeros(2) or np.zeros((2,)). Both gives the same output. If you give the np.zeros() an integer as the first argument, then it will create a 1D array with 2 elements.

Question 4

Write a function call to generate an identity matrix as the output below. Your answer should be something like np.zeros((2, 3)).

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

np.identity(4)
Explanation:

You can use either the np.identity() function or the np.eye() function. np.identity(4) gives you a 4x4 identity matrix. Similarly, you can use np.eye(4) to achieve the same purpose. You can also give np.eye() a second argument representing a number of columns, np.eye(4, 4). So technically you can create an asymmetric identity matrix like np.eye(4, 3) - try this and see the output!

Question 5

Write a function call to generate a np.array that looks like the output below. Your answer should be something like np.zeros((2, 3)).

[[0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [0. 0. 0. 0.]]

np.eye(4, k=1)
Explanation:

You can do more with the np.eye() function compared to np.identity(). np.eye() takes an optional k argument to specify the offset of the diagonal. In this case, k=1 moves the diagonal one step up (k=-1 will move the diagonal to the lower diagonal).

Question 6

Fill in the blank in the code below (in the second line) to get the output as shown. We are aiming to generate a np.array of ones with the same shape and dtype as x.

As an example, if you think np.zeros(x) is the answer, then type in zeros into the answer box.

>>> x = np.array([[3, 1, 2], [4, 5, -1]])
>>> y = np._____(x)
>>> print(y)
[[1 1 1]
 [1 1 1]]

ones_like
Explanation:

The expected answer is y = np.ones_like(x). This function is like np.ones(), except that you pass in an existing np.array. The function then generates a new np.array with the same shape (2, 3). Also note that y has the same dtype (int) as x, instead of float.