Chapter 2. Beginning with NumPy Fundamentals

After installing NumPy and getting some code to work, it's time to cover NumPy basics.

The topics we shall cover in this chapter are:

  • Data types
  • Array types
  • Type conversions
  • Array creation
  • Indexing
  • Slicing
  • Shape manipulation

Before we start, let me make a few remarks about the code examples in this chapter. The code snippets in this chapter show input and output from several IPython sessions. Recall that IPython was introduced in Chapter 1, NumPy Quick Start, as the interactive Python shell of choice for scientific computing. The advantages of IPython are the PyLab switch that imports many scientific computing Python packages, including NumPy, and the fact that it is not necessary to explicitly call the print function to display variable values. However, the source code delivered alongside the book is regular Python code that uses imports and print statements.

NumPy array object

NumPy has a multi-dimensional array object called ndarray . It consists of two parts:

  • The actual data
  • Some metadata describing the data

The majority of array operations leave the raw data untouched. The only aspect that changes is the metadata.

We have already learned, in the previous chapter, how to create an array using the arange function. Actually, we created a one-dimensional array that contained a set of numbers. ndarray can have more than one dimension.

The NumPy array is in general homogeneous (there is a special array type that is heterogeneous)—the items in the array have to be of the same type. The advantage is that, if we know that the items in the array are of the same type, it is easy to determine the storage size required for the array.

NumPy arrays are indexed just like in Python, starting from 0. Data types are represented by special objects. These objects will be discussed comprehensively in this chapter.

We will create an array with the arange function again. Here's how to get the data type of an array:

In: a = arange(5)
In: a.dtype
Out: dtype('int64')

The data type of array a is int64 (at least on my machine), but you may get int32 as output if you are using 32-bit Python. In both cases, we are dealing with integers (64-bit or 32-bit). Besides the data type of an array, it is important to know its shape.

The example in Chapter 1, NumPy Quick Start, demonstrated how to create a vector (actually, a one-dimensional NumPy array). A vector is commonly used in mathematics but, most of the time, we need higher-dimensional objects. Let's determine the shape of the vector we created a few minutes ago:

In [4]: a
Out[4]: array([0, 1, 2, 3, 4])
In: a.shape
Out: (5,)

As you can see, the vector has five elements with values ranging from 0 to 4. The shape attribute of the array is a tuple, in this case a tuple of 1 element, which contains the length in each dimension.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset