NumPy Arrays
Let's learn how to create a NumPy array in Python.
First, we need to import the NumPy module using the import numpy as np
command. Here, np
is used as an alias. This means that instead of writing numpy.function_name
, we can use np.function_name
.
We will have a look at four ways of creating a NumPy array:
- Array filled with zeros – the
np.zeros
command - Array filled with ones – the
np.ones
command - Array filled with random numbers – the
np.random.rand
command - Array filled with values specified – the
np.array
command
Let's start with the np.zeros
and np.ones
commands. There are two important arguments for these functions:
- The shape of the array. For a 2D array, this is
(number of rows, number of columns)
. - The data type of the elements. By default, NumPy uses floating-point numbers for its data types. For images, we will use unsigned 8-bit integers –
np.uint8
. The reason behind this is...