NumPy

Predict the array before you run it

Array code fails in four ways, and all four are settled before anything is computed: the wrong shape, the wrong dtype, two operands that will not line up, and a write that reached further than you meant. This is practice at answering all four from the page, with broadcasting drilled hardest.

Modules
5
Drills
11
Build steps
6
Time
~5h

Walk out able to

Say what shape and dtype an expression produces, whether two operands broadcast, and whether a write through one name will show up in another.

What you drill

01

Shape, not length

What each call leaves behind, and which axis vanished.

02

The dtype decides

Fixed width, and the totals that go quietly wrong.

03

Broadcasting

Two shapes in, one shape out — or a ValueError.

04

Views, copies, one line

Who else changed, and the loop you can delete.

What you get

Skills that stick

Every project mixes short lessons, focused drills and real build steps, so you finish with working code and the judgement behind it.

You will practise

  • Shapes after reshape, transpose and slicing
  • The dtype two operands produce
  • Whether two shapes broadcast, or raise
  • Whether a write reaches the original

Afterwards you can

  • Read a shape off a line of array code
  • Line two shapes up, or fix the one that will not
  • Say which expressions alias and which copy
  • Replace a numeric loop with one expression

Modules

Step by step

Each module builds on the last. Read a short lesson, drill it until it's second nature, then put it to work.

  1. 1

    Shape, not length

    ~52 min

    Say what shape any expression has, and read a value out of a grid without a loop.

    • LessonWhy an array and not a list5 min
    • LessonThree parts, and the axis that goes9 min
    • LessonOne colon changes the shape6 min
    • DrillSay the shape after the call10 min
    • DrillRead a value out of a grid10 min
    • Build stepBuild the readings grid12 min
  2. 2

    The dtype decides

    ~50 min

    Predict what an operation produces, and where a total can go silently wrong.

    • LessonOne dtype, decided once, for everything9 min
    • LessonThe dtype that comes out7 min
    • DrillName the dtype that comes out10 min
    • DrillSay what the array actually holds10 min
    • Build stepMake a total come out wrong14 min
  3. 3

    Broadcasting

    ~51 min

    Say what shape two operands produce, or why they raise — and fix the ones that do.

    • LessonLine the two shapes up on the right10 min
    • LessonGive a vector an axis of its own7 min
    • DrillSay the shape, or say it raises12 min
    • DrillMake two shapes line up10 min
    • Build stepCentre every station on its own peak12 min
  4. 4

    Views and copies

    ~53 min

    Say whether two names share memory, and predict what the original prints after a write.

    • LessonFind out who else changed10 min
    • LessonWhich side the mask is on7 min
    • DrillSay which line copies10 min
    • DrillPredict what the original prints12 min
    • Build stepFix a report that edits its own source14 min
  5. 5

    Delete the loop

    ~1h

    Replace element-at-a-time Python with one expression that means the same thing.

    • LessonSay it once, for every element10 min
    • LessonWhen a mask throws too much away7 min
    • DrillPick the line that replaces the loop12 min
    • DrillReduce along the right axis10 min
    • DrillWrite the line that does it12 min
    • Build stepReport every station in its own terms13 min
    • Build stepFlag the big days, prove nothing moved14 min

Ready when you are.

Work through it at your own pace and pick up right where you left off.