Creating and loading arrays

In this section, we will see how we can create and load arrays either from scratch or from existing data. This is the first step when analyzing data with Python.

Creating arrays

There are several ways of creating an array. We will review them in this section.

From scratch, element by element

First, we can create an array by manually specifying its coefficients. This is the most direct way of creating an array, but it is not used very often in practice. The NumPy function array takes a list of elements and returns a corresponding NumPy array, as shown in the following example (the pylab mode of IPython needs to be activated):

In [1]: x = array([1, 2, 3])
In [2]: x.shape
Out[2]: (3,)
In [3]: x.dtype
Out[3]: dtype('int32') ...

Get Learning IPython for Interactive Computing and Data Visualization now with O’Reilly online learning.

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