Creating and loading arrays

In this section, we will see how to create and load NumPy arrays.

Creating arrays

First, there are several NumPy functions for creating common types of arrays. For example, np.zeros(shape) creates an array containing only zeros. The shape argument is a tuple giving the size of every axis. Hence, np.zeros((3, 4)) creates an array of size (3, 4) (note the double parentheses, because we pass a tuple to the function).

Here are some further examples:

In [1]: import numpy as np
        print("ones", np.ones(5))
        print("arange", np.arange(5))
        print("linspace", np.linspace(0., 1., 5))
        print("random", np.random.uniform(size=3))
        print("custom", np.array([2, 3, 5]))
Out[1]: ones [ 1.  1.  1.  1.  1.]
        arange [0 1 2 3 4]
 linspace [ 0. 0.25 ...

Get Learning IPython for Interactive Computing and Data Visualization - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.