Introduction to NumPy and Matplotlib
Chapter 2: NumPy arrays
Array creation routines
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).
❮
❯