Lecture 13 - Multidimensional Arrays with Numpy

Announcements:

Goals:

You may need to pip install the imageio and matplotlib packages for some of the following to work. Make sure you install it inside your data 311 virtual environment. The numpy package should already be installed as a dependency of pandas.

The numpy package and ndarray type

The numpy package is largely focused on providing the ndarray type and related functionality; an ndarray is a multi-dimensional array.

Why is this interesting to us?

Array Creation

np.array, np.zeros, np.ones

Array Data Types

a.dtype; dtype kwarg to array, zeros, ones

Unlike DataFrames, ndarrays need to be all one type. Numpy builtin types include:

You can use python's native types too - bool, float (same as np.float64), int (same as int64)...

Array Dimensions

a.shape, a.ndim

Transpose, Reshape

a.T (2d); a.transpose(order) (3d); a.reshape(new_shape)

Elementwise operations

Array/Scalar, Array/Array

Multidimensional arrays as images

Color images are conventionally stored (row, column, channel), where channel is a dimension of size 3 containing red, green, and blue values.

Indexing, Slicing, Masking/Boolean Indexing

Useful Methods

a.sum, a.mean; axis kwarg

Broadcasting

Array-Array elementwise operations require the arrays to have the same shape.

Exception: if a corresponding dimension is 1 in one array, the values will be repeated ("broadcast") along that dimension.