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 as follows:

  • 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. Other features include easy parallel computation and the notebook interface in the form of a persistent worksheet in a web browser.

However, the source code delivered alongside the book is a regular Python code that uses import and print statements.

NumPy array object

NumPy has a multidimensional 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.

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

The NumPy array is in general homogeneous (there is a special array type that is heterogeneous as described in the Time for action – creating a record data type section)—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 starting from 0, just like in Python. Data types are represented by special objects. We will discuss these objects comprehensively in this chapter.

Let's create an array with the arange() function again. Get the data type of an array using the following code:

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 the 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.

In Chapter 1, NumPy Quick Start, we 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. Determine the shape of the vector we created a few minutes ago. The following code is an example of creating a vector:

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.

Note

A tuple in Python is an immutable (it can't change) sequence of values. Once tuples are created, we are not allowed to change the values of tuple elements or append new elements. This makes tuples safer than lists because you can't mutate them by accident. A common use case for tuples is as return value of functions. For more examples, have a look at the Introducing Tuples section of Chapter 3, Dive into Python, available at http://www.diveintopython.net/native_data_types/tuples.html.

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

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