MCS 275 Spring 2022
numpy module is used for multidimensional typed arrays.
Vectors (1D arrays) and matrices (2D arrays) are the most frequently used.
Last time we explored ways to make arrays and access elements.
In 2004, Tim Peters wrote a list of principles that describe the design of Python. One is:
There should be one---and preferably only one---obvious way to do it.
In contrast, numpy often offers many ways to accomplish a given task, allowing the user to decide which is best.
Numpy is big, but you can be productive after learning a small subset.
Let's continue working in the numpy introduction notebook.
and numpy arrays do not support boolean coercion so this cannot appear in
To test if two arrays are equal, use one of:
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).
If an operation expects arrays of the same dimension, but different dimensions are given, numpy attempts to fix this by broadcasting—using duplication to extend an array to a higher dimension.
A+1 works when
A is a numpy array. It adds
1 to each entry. But how?
Broadcasting first turns
1 into an array with the same shape as
A where each entry is
Numpy has operations like sum, product, max, min, all, any, that reduce array dimension.
A is an array and
M is an array of
bool of the same shape, then
A[M] refers to a 1D array that lists elements of
A at positions where
Often used to test or modify elements of an array that meet certain criteria, e.g.