O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating value initialized arrays with the full() and full_like() functions

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.

How to do it...

Let's see how full() and full_like() function:

  1. Create a 1 by 2 array with full(), filled with the lucky number 7:
    print(np.full((1, 2), 7))

    Accordingly, we get the following array:

    array([[ 7.,  7.]])
    

    The array elements are floating-point numbers.

  2. Specify an integer data type, as follows:
    print(np.full((1, 2), 7, dtype=np.int))

    The output changes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required