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
Shape, not length
What each call leaves behind, and which axis vanished.
The dtype decides
Fixed width, and the totals that go quietly wrong.
Broadcasting
Two shapes in, one shape out — or a ValueError.
Views, copies, one line
Who else changed, and the loop you can delete.
The pitch
What you practise, and what you leave with
A station report over a seeded grid of readings: per-station shares, flagged counts and a busiest station, with no loop over readings anywhere in 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
5 modules, 28 items
Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.
- 1
Shape, not length
~52 minSay 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
The dtype decides
~50 minPredict 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
Broadcasting
~51 minSay 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
Views and copies
~53 minSay 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
Delete the loop
~1hReplace 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
More in Data & databases
Keep going
SQL: Queries
Say what a query returns before you run it.
Intermediate~3h6 modules
ORMs
Count the queries before the mapper sends them.
Intermediate~4h6 modules
SQL: Joins & NULL
The rows that found no partner, and the values that are not there.
Advanced~4h5 modules
SQL: Advanced
The clause you did not write decided the number.
Advanced~6h7 modules
Know what comes out before you run it.
The first item is free. ~5h of focused work, at your own pace.
Intermediate~5h5 modules