MCS 275 Spring 2023
Emily Dumas
Reminders and announcements:
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.
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'
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, most people import it under a shorter name to save typing:
import numpy as np
Numpy is one of the most-used Python packages in scientific computing (computational math, data science, machine learning, ...).
Implemented in `np.ndarray` class, usually made with `np.array`.
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)
I'll build a Python notebook demonstrating some basic features of numpy.
After lecture it will be in the course sample code repo.
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.
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).
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)
Numpy has operations like sum, product, max, min, all, any, that reduce array dimension.