TenMinuteTutor

Programming tutorials

Introduction to numpy

Numpy is a Python package that allows you to efficiently store and process large arrays of numerical data. Obvious examples of this type of data are sound data and image data, but numpy can also be used anywhere you have large data sets to process.

Part of the attraction of numpy is that it uses simple and familiar Python syntax to perform complex operations on arrays, which simplifies your code. The other attraction is that numpy is highly efficient, both in terms of speed and memory usage. These two factors are not unrelated - numpy provides high level array operations, and these operations are efficient because, under the hood, the entire processing loop is written in C.

Some of the main features of numpy are:

  • Memory efficient storage of large amounts of data.
  • Fast element-wise operations on arrays.
  • Simple clean syntax for array operations.
  • Access sub-arrays.
  • Change array shape.
  • Read and write images, sound, and other data.
  • Integration with SciPy (Scientific Python library).

Setting up

Before you start, you will need to install numpy. The official numpy site will point you at the latest version, with instructions for installing the package.

When you import the numpy package into Python, it is common practice to import it as np (so that you can use the short name np in your code).:

>>> import numpy as np

You don’t have to, but most people who use numpy do, and will recognise the np prefix.