Book Image

IPython Notebook Essentials

By : Luiz Felipe Martins
Book Image

IPython Notebook Essentials

By: Luiz Felipe Martins

Overview of this book

Table of Contents (15 chapters)
IPython Notebook Essentials
About the Author
About the Reviewers

Array creation and member access

NumPy arrays are objects of the ndarray class, which represents a fixed-size multidimensional collection of homogeneous data.

Here, we will assume that the NumPy library has been imported using the following command line:

import numpy as np

Once we have done that, we can create ndarray (from now on, informally called array object or simply array) from a list of lists as indicated in the following command line:

a = np.array([[-2,3,-4,0],[2,-7,0,0],[3,-4,2,1]],dtype=np.float64)
print a

Contrary to Python lists and tuples, all entries of an array object must be of the same type. The types themselves are represented by NumPy objects and are referred to as dtype (from data type) of the array. In the preceding example, we explicitly specify dtype as float64, which represents a 64-bit floating-point value.

Arrays have several attributes that give information about the data layout. The more commonly used ones are as follows:

  • The shape of the array is computed using the following command:


    The preceding command returns the tuple (3, 4), since this is a two-dimensional array with three rows and four columns. Somewhat surprisingly, the shape attribute is not read-only and we can use it to reshape the array:

    a.shape = (6,2)
    print a

    After running the preceding example, run a.shape(3,4) to return to the original dimensions.

  • The number of dimensions of the array is obtained using the following command:


    This, of course, returns 2. An important notion in NumPy is the idea of axes of an array. A two dimensional array has two axes, numbered 0 and 1. If we think of the array as representing a mathematical matrix, axis 0 is vertical and points down, and axis 1 is horizontal and points to the right. Certain array methods have an optional axis keyword argument that lets the user specify along which axis the operation is performed.

  • To get the number of elements in the array, we can use the following command:


    In the preceding example, the output returned is 12, as expected.

  • One final attribute of arrays is computing the transpose of an array. This can be done using the following command:

    b = a.T
    print b

    An important thing that this creates is a view of the array a. The NumPy package is designed to work efficiently with very large arrays, and in most cases, avoids making copies of data unless absolutely necessary, or is explicitly directed to do so.

  • Run the following lines of code:

    print a
    b[1,2] = 11
    print a

    Note that the entry 2, 1 of the array a is changed, demonstrating that both variables, a and b, point to the same area in memory.

  • An array with uninitialized data can be created with the empty() function as follows:

    c = np.empty(shape=(3,2), dtype=np.float64)
    print c
  • Using uninitialized data is not recommended, so it is perhaps preferable to use either the zeros() or ones() function as follows:

    • To use the zeros() function, execute the following command lines:

      d = np.zeros(shape=(3,2), dtype=np.float64)
      print d
    • To use the ones() function, execute the following command lines:

      e = np.ones(shape=(3,2), dtype=np.float64)
      print e
      a_like = np.zeros_like(a)
      print a_like

    There are also functions that create new arrays with the same shape and data type of an existing array:

  • The functions ones_like() and empty_like() produce arrays of ones and uninitialized data with the same shape as a given array.

  • NumPy also has the eye() function that returns an identity array of the given dimension and dtype:

    f = np.eye(5, dtype=np.float64)
    print f

    The number of rows and columns do not have to be the same. In this case, the resulting matrix will only be a left- or right- identity, as applicable:

    g = np.eye(5, 3, dtype=np.float64)
    print g
  • Arrays can also be created from existing data. The copy() function clones an array as follows:

    aa = np.copy(a)
    print a
    print aa
  • The frombuffer() function creates an array from an object that exposes the (one-dimensional) buffer interface. Here is an example:

    ar = np.arange(0.0, 1.0, 0.1, dtype=np.float64)
    v = np.frombuffer(ar)
    v.shape = (2, 5)
    print v

    The arange() function is a NumPy extension of the Python range. It has a similar syntax, but allows ranges of floating-point values.

  • The loadtxt() function reads an array from a text file. Suppose the text file matrix.txt contains the following data:

     1.3  4.6  7.8
    -3.6  0.4  3.54
     2.4  1.7  4.5

    Then, we can read the data with the following command:

    h = np.loadtxt('matrix.txt', dtype=np.float64)
    print h
  • Arrays can also be saved and loaded in the .npy format:'matrix.npy',h)
    hh = np.load('matrix.npy')
    print hh