The
full()
and full_like()
functions are new additions to NumPy meant to facilitate initialization. Here's what the documentation says about them:
>>> help(np.full) Return a new array of given shape and type, filled with `fill_value`. >>> help(np.full_like) Return a full array with the same shape and type as a given array.
Let's see how full()
and full_like()
function:
Create a
1
by2
array withfull()
, filled with the lucky number7
:print(np.full((1, 2), 7))
Accordingly, we get the following array:
array([[ 7., 7.]])
The array elements are floating-point numbers.
Specify an integer data type, as follows:
print(np.full((1, 2), 7, dtype=np.int))
The output changes accordingly:
array([[7, 7]])
The
full_like()
function checks the metadata of an array and reuses it for the new array. For example, create an array usinglinspace()
, and apply it as a template for thefull_like()
function:a =...