Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

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 value. For instance, after creating an array with the linspace() function, use that as a template for the full_like() function:

    >>> a = np.linspace(0, 1, 5)
    >>> a
    array([ 0.  ,  0.25,  0.5 ,  0.75,  1. ...