7 routes42 sections324 units

Unit 1.2 · about 4 h

2 / 324

NumPy 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

  1. NumPy Quickstart Tutorial
    NumPy 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

Given an array with shape (120, 8), explain what each axis represents, compute a column-wise mean, select rows by a boolean condition and predict the shape of a valid broadcast operation.

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

NumPy learning resources

Complete one beginner tutorial and implement the same array operation without a Python loop.

Check shapes and dtypes at every step.

Self-test

  1. For an array with shape (120, 8), what does axis=0 reduce?

  2. Why can arrays with shapes (120, 1) and (8,) broadcast together?

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

Comes after

Resources for this unit

  • NumPy Quickstart Tutorial
    NumPyprimarydocumentation
    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
    Kaggle Learnprimarycourse
    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
    Python Software Foundationreferencereference
    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
    Google Researchsupportnotebook-environment
    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
    Kaggle Learnprimarycourse
    Complete the short exercises on reading data, selecting, summary functions, grouping, sorting, data types, missing values and combining tables.
  • pandas Getting Started Tutorials
    pandas Development Teamrequireddocumentation
    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
    Matplotlibsupportdocumentation
    Use the Figure/Axes workflow to make labeled line plots, histograms and scatter plots. Keep plots diagnostic rather than decorative.
  • Bike Sharing Dataset
    UCI Machine Learning Repositoryrequireddataset
    Use the hourly hour.csv data for the two linked starter projects. Cite Fanaee-T (2013), DOI 10.24432/C5W894, and the CC BY 4.0 licence. cnt is the target; casual and registered are components of that target and must never be used as input features.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels