Lecture 25

Numpy

MCS 275 Spring 2022
Emily Dumas

Lecture 25: Numpy

Course bulletins:

  • See Blackboard announcement about week after spring break.
  • Project 3 due 6:00pm on Friday 18 March.

A good book

For numpy, matplotlib, and a few other topics from MCS 275, I strongly recommend reading:

It is available for free online. Chapter 2 is about numpy.

Installing numpy

In most cases, pip is all you need:

python3 -m pip install numpy

Other methods are described in the Numpy docs.

Test:


        >>> import numpy
        >>> numpy.__version__
        '1.17.4'
    

Import as

You can give a module a new name at import time, e.g.


        import math as sun
        sun.tan(0.5)
    

Since numpy has a lot of global names, some of which appear frequently in code, most people import it with


        import numpy as np
    

numpy purpose

  • Fast, type-homogeneous, multidimensional arrays
    • e.g. vector, matrix, tensor, ...
  • Large library of mathematical functions and algorithms (especially linear algebra)

Numpy is one of the most-used Python packages in scientific computing (computational math, data science, machine learning, ...).

arrays

Implemented in `np.ndarray` class.

Without numpy:


        v = [2,3] 
        w = [3,-2]
        v + w    # [2,3,3,-2]
        3*v      # [2,3,2,3,2,3]
        v.dot(w) # fail!
        A = [ [2,1], [1,1] ]
        type(A)  # list
        A*v      # fail!
    

With numpy:


        v = np.array([2,3])
        w = np.array([3,-2])
        v + w    # [5,1]
        3*v      # [6,9]
        v.dot(w) # 0
        A = np.array([ [2,1], [1,1] ])
        A*v      # possibly confusing answer
        A.dot(v) # [7,5] (matrix-vector mult)
    

Notebook time

I'll build a Python notebook demonstrating some basic features of numpy.

After lecture it will be available here.

Indexing and slicing

Numpy has powerful syntax for retrieving individual elements or collections of elements of arrays.

Most basic version: A[i,j] gives the element at row i, column j for a 2D array. Similar in higher dimensions, e.g. A[i,j,k,l].

Slices return views of part of the array, not copies.

Ufuncs

Numpy's "ufuncs" or universal functions are functions that can be applied directly to arrays, automatically acting on each element.

Numpy provides a lot of these.

Usually, ufuncs allow you to avoid explicit iteration over array elements (which is much slower).

Bool gotcha

np.array([5,0,1])==np.array([0,0,0])

evaluates to

np.array([False,True,False])

and numpy arrays do not support boolean coercion so this cannot appear in if.

To test if two arrays are equal, use one of:

np.all(A==B)
np.array_equal(A,B)

References

Revision history

  • 2022-03-09 Initial publication