Introduction

We are now ready to start exploring NumPy, the fundamental package upon which the whole scientific Python stack is built. This chapter presents an introduction to the essential features of NumPy that are used in day-to-day scientific and data computations.

Built-in Python data structures, such as lists and dictionaries, are ill suited for scientific and data-oriented computing and their use results in programs that are significantly slower than numerical code written in compiled languages such as C, C++, and Fortran. NumPy was created to address this problem, and solves it by defining specialized array-oriented objects and methods designed for efficient numerical and data computing. The main data structure defined in NumPy with this purpose is ndarray, which represents a multidimensional array of data.

Objects of ndarray type differ from the native Python data structures in the following respects:

  • In general, data held in an ndarray must be of a uniform type, determined at the time the array is constructed.
  • Arrays can hold a wider range of data types than Python. In general, all types supported by standard implementations of the C programming language are supported.
  • Objects of ndarray type have fixed dimensions. Resizing or reshaping an array results in the creation of a copy.
  • Objects of ndarray type have a rich set of methods for efficient manipulation of arrays, including creation, element reference and assignment, access to ranges, reshaping, and concatenation.
  • A special kind of function, represented by the ufunc class, is defined to operate on arrays. A ufunc can apply the same function to all elements of an array of arbitrary shape..

Examples in this chapter were tested on release 1.12.0 of NumPy, which is the latest available version as of writing. The package documentation can be accessed at the site https://docs.scipy.org/doc/.

To run the examples in this chapter, it is recommended that you enter the code in an interactive Python environment. The options are as follows, in order of preference:

  • The Jupyter Notebook, which can be started by running the command jupyter notebook at the terminal.
  • The IPython shell, which can be started by running ipython at the command line.
  • The Python shell, which can be started by running python3 or python at the command line. Make sure you are running the shell corresponding to the installation of Python for which the scientific Python stack was installed.

In this chapter, we will present recipes for performing the following tasks:

  • How to create NumPy arrays
  • How to change the size and shape of an array
  • How to concatenate, or stack arrays
  • How to select elements or ranges of an array with different indexing mechanisms
  • How to store/retrieve arrays to/from permanent storage
  • How to apply vectorized functions and operations to arrays
  • How to define vectorized functions using symbolic expressions

To run the examples in this chapter, it is first necessary to import the numpy module using the following code:

import numpy as np
Never use from numpy import * to import the numpy module. This would have the effect of importing hundreds of names into the currently running module, which could lead to hard to detect bugs due to name collisions.
..................Content has been hidden....................

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