Section 2.2: NumPy, The Array
A model does not read a table as rows you loop over. It reads one block of numbers of one type, the array, and pandas, scikit-learn and matplotlib all hold their numbers in it. This section covers NumPy's array: building it and the type it holds, selecting from it without corrupting the original, computing over all of it in one expression, combining arrays of different shapes, and collapsing it into summaries along the right axis.
Section 2.1 queried a database down to the rows you need, and those rows came back as Python tuples, fine for reading and slow and clumsy for arithmetic over thousands of values. This section turns rows into arrays. The habits it builds, checking the dtype, checking the shape, asking whether a selection is a view, are the ones pandas in Section 2.4 and PyTorch's tensors in Module 4 rely on.
Lessons
- Installing NumPy, the ndarray and Generating Arrays - the install, the array and its dtype, and arrays built filled, ranged and random.
- Indexing, Slicing, Boolean Masking and the Copy-on-Write Model - selecting from an array, and knowing whether a write reaches the original.
- Vectorized Operations - arithmetic and math functions over whole arrays, and the warnings invalid values print.
- Broadcasting - the rule that pairs arrays of different shapes, and the silent result when it pairs the wrong ones.
- Aggregation and the
axisArgument - summaries over a whole array, per feature and per listing.
By the end you will be able to:
- Build arrays of the dtype and shape you intend, and generate reproducible random data from a seed.
- Predict whether a selection is a view or a copy, and filter and repair values with boolean masks.
- Replace a Python loop with one vectorized expression, and predict the shape an operation between two arrays returns.
- Summarize a feature table per feature or per listing with
axis, standardize it, and keep missing values from poisoning the summary.