Intro to NumPy

1. DataTypes

Ndarray Attributes

2. Creating Arrays

Basics

np.ones(shape, dtype=None)

np.zeroes(shape, dtype=None)

np.arange([start,] stop[, step,], dtype=None)

Random Numbers

np.random.randint(low, high=None, size=None, dtype='l')

np.random.random(size=None)

np.random.rand(d0, d1, ..., dn)

Random Seed

np.random.seed(seed=None)

3. Viewing Arrays & Matrices

Matrix – an array with 2+ dimensions

Access matrix elements by index via square brackets, e.g. arr[index_d0, index_d1]

np.unique(ndarray)

How would we get the center 3 numbers of the innermost arrays?

4. Manipulating & Comparing Arrays

Broadcasting Rules

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing (i.e. rightmost) dimensions and works its way left. Two dimensions are compatible when:

NumPy Docs

Arithmetic

Aggregation

Aggregation – performing a collective operation on a group of numbers/items

NumPy Aggregation Functions

Standard Deviation & Variance

NumPy Functions

Explanation of Standard Deviation

4a. Reshaping & Transposing

NumPy Docs: Reshape

4b. Element Wise Ops vs. Dot Product

Resources

4c. Comparison Operators

NumPy Docs: Logic Functions

5. Sorting Arrays

Argsort is bit tricky at first. Be careful of the Einstellung trap here!

The indices along the sorted axis are NOT telling you the sorted position of the corresponding element in the unsorted array.

So how does Argsort actually work?

The indices themselves are in sorted order. E.g. looking at row 1 above:

6. Images → NumPy Arrays

Panda