Learning NumPy Array
上QQ阅读APP看书,第一时间看更新

The NumPy array object

NumPy has a multidimensional array object called ndarray. It consists of two parts as follows:

  • 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. The ndarray object can have more than one dimension.

The advantages of using NumPy arrays

A NumPy array is a general homogeneous array—the items in an array have to be of the same type (there is a special array type that is heterogeneous). The advantage is that if we know that the items in an array are of the same type, it is easy to determine the storage size required for the array. NumPy arrays can perform vectorized operations working on a whole array. Contrast this to Python lists, where normally you have to loop through the list and perform operations on each element at a time. Also, NumPy uses an optimized C API for these operations, making them especially fast.

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

We will create an array using the arange() function again (see the arrayattributes.py file in the Chapter02 folder of this book's code bundle). In this chapter, you will see code snippets from IPython sessions where NumPy is already imported. The following code snippet shows us how to get the data type of an array:

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

The data type of the array a is int64 (at least on my machine), but you may get int32 as the output if you are using 32-bit Python. In both cases, we are dealing with integers (64-bit or 32-bit). Apart from the data type of an array, it is important to know its shape. The example in Chapter 1, Getting Started with NumPy, 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 little earlier in this section:

In: a
Out: 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 one element, which contains the length in each dimension.