Unit 1.2 · about 4 h
2 / 324NumPy arrays: shape, dtype and vectorized operations
ML libraries represent batches, features, weights and activations as arrays or tensors. Shape mistakes are easier to diagnose when array structure is explicit before a framework adds automatic differentiation.
- explain
- implement
- measure
You can now
- Create arrays and inspect their shape, number of dimensions and dtype.
- Explain the difference between element-wise operations, reductions and matrix multiplication.
- Use indexing, slicing and boolean masks to select observations.
- Predict whether a broadcast operation is valid by comparing dimensions from the right.
Covers
- NumPy arrays, shapes, dtypes and vectorization
Study, in this order
1 steps
- NumPy Quickstart TutorialNumPy Quickstart Tutorial
Work through array creation, shape, indexing, universal functions, broadcasting and basic operations.
Focus on
- Array versus Python list
- Shape, axes, dtype and element count
- Indexing, slicing and boolean masks
- Vectorization, reductions and broadcasting
Reproduce closed-book
Traps
- Do not confuse a one-dimensional array with a two-dimensional column matrix.
- Do not use Python loops for operations that NumPy expresses directly; understand the array operation first.
- Do not ignore dtype: integer division, object arrays and implicit casts can change results.
Practice
Complete one beginner tutorial and implement the same array operation without a Python loop.
Check shapes and dtypes at every step.
Self-test
-
For an array with shape (120, 8), what does
axis=0reduce?It reduces across the 120 rows and leaves one result per column, so the output has shape (8,). -
Why can arrays with shapes (120, 1) and (8,) broadcast together?
Broadcasting aligns dimensions from the right; dimensions are compatible when equal or one of them is 1. The resulting shape is (120, 8).
Completion criterion
Stop when
You can explain shapes and dtypes before an operation, write vectorized selections and reductions, and predict whether common broadcast and matrix operations are valid.
Mark passed, then open 1.3 Only mark this when you can do it without notes.Leads to
- 1.3 Inspect and transform tabular data with pandas Follow the practical starter sequence; each unit uses the artifact from the previous step.
Comes after
- 1.1 Notebook workflow, Python essentials and reproducible runsFollow the practical starter sequence; each unit uses the artifact from the previous step.
Resources for this unit
- NumPy Quickstart Tutorial Study array creation, shape, indexing, universal functions, broadcasting and basic operations. Write down shapes and dtypes before changing an array.
Also in this section
- Kaggle Learn — Python Complete the course lessons on variables, functions, conditionals, lists, loops, strings, dictionaries and external libraries. Treat it as the practical Python vocabulary needed for the next units.
- The Python Tutorial Use as a reference for control flow, data structures, modules, files and exceptions. It assumes some prior programming experience; use Kaggle Learn for the first pass if the syntax is new.
- Google Colab FAQ — free resources and limits Use a hosted notebook for the first exercises if local setup is a distraction. The free tier has variable, non-guaranteed compute and GPU access; keep every exercise small enough to run on CPU.
- Kaggle Learn — Pandas Complete the short exercises on reading data, selecting, summary functions, grouping, sorting, data types, missing values and combining tables.
- pandas Getting Started Tutorials Use the official tutorials as a reference for reading and writing tables, selecting rows and columns, creating columns, summary statistics, joins and time-series handling.
- Matplotlib Quick Start Guide Use the Figure/Axes workflow to make labeled line plots, histograms and scatter plots. Keep plots diagnostic rather than decorative.
- Bike Sharing Dataset Use the hourly
hour.csvdata for the two linked starter projects. Cite Fanaee-T (2013), DOI 10.24432/C5W894, and the CC BY 4.0 licence.cntis the target;casualandregisteredare components of that target and must never be used as input features.