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
  1. Create a one-by-two array with the full() function filled with the number 42 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]])
    
  2. 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 ...

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.