Time for action – creating value initialized arrays with the full() and full_like() functions
Let's demonstrate how the full()
and full_like()
functions work. If you are not in a Python shell already, type the following:
$ python >>> import numpy as np
Create a one-by-two array with the
full()
function filled with the number42
as follows:>>> np.full((1, 2), 42) array([[ 42., 42.]])
As you can deduce from the output, the array elements are floating-point numbers, which is the default data type for NumPy arrays. Specify an integer data type as follows:
>>> np.full((1, 2), 42, dtype=np.int) array([[42, 42]])
The
full_like()
function looks at the metadata of an input array and uses that information to create a new array, filled with a specified value. For instance, after creating an array with thelinspace()
function, use that as a template for thefull_like()
function:>>> a = np.linspace(0, 1, 5) >>> a array([ 0. , 0.25, 0.5 , 0.75, 1. ...