The array type

The objects used to manipulate vectors, matrices, and more general tensors in NumPy are called arrays. In this section, we examine their essential properties, how to create them, and how to access their information.

Array properties

Arrays are essentially characterized by three properties, which is given in the following table (Table 4.2):

Name

Description

shape

It describes how the data should be interpreted, as a vector, a matrix or as a higher order tensor, and it gives the corresponding dimension. It is accessed with the shape attribute.

dtype

It gives the type of the underlying data (float, complex, integer, and so on).

strides

This attribute specifies in which order the data should be read. For instance, a matrix could be stored in memory contiguously column by column (the FORTRAN convention), or row by row (the C convention). The attribute is a tuple with the numbers of bytes that have to be skipped in memory to reach the next row and the number of bytes to be skipped to reach the next column. The strides attribute even allows for a more flexible interpretation of the data in memory, which is what makes array views possible.

Table 4.2 : Properties of Arrays

Consider the following array:

A = array([[1, 2, 3], [3, 4, 6]])
A.shape   # (2, 3)
A.dtype   # dtype('int64')
A.strides # (24, 8)

Its elements have type 'int64'; that is, they use 64 bits or 8 bytes in memory. The complete array is stored in memory row-wise. The distance from A[0, 0] to the first element in the next row A[1,0] is thus 24 bytes (three matrix elements) in memory. Correspondingly, the distance in memory between A[0,0] and A[0,1] is 8 bytes (one matrix element). These values are stored in the attribute strides .

Creating arrays from lists

The general syntax to create an array is the function array . The syntax to create a real vector would be:

V = array([1., 2., 1.], dtype=float)

To create a complex vector with the same data:

V = array([1., 2., 1.], dtype=complex)

When no type is specified, the type is guessed. The array function chooses the type that allows storing of all the specified values:

V = array([1, 2]) # [1, 2] is a list of integers
V.dtype # int
V = array([1., 2]) # [1., 2] mix float/integer
V.dtype # float
V = array([1. + 0j, 2.]) # mix float/complex
V.dtype # complex

Silent type conversion NumPy silently casts floats into integers, which might give unexpected results:

a = array([1, 2, 3])
a[0] = 0.5
a # now: array([0, 2, 3])

The same often unexpected array type casting happens from complex to float.

Array and Python parentheses

As we have noticed in section Program and program flow in Chapter 1, Getting Started, Python allows a line break when some opening brace or parenthesis is not closed. This allows a convenient syntax for array creation, which makes it more pleasing to the human eye:

 # the identity matrix in 2D
 Id = array([[1., 0.], [0., 1.]])
 # Python allows this:
 Id = array([[1., 0.],
             [0., 1.]])
 # which is more readable
..................Content has been hidden....................

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