typestar

NumPy

22 steps in 8 sets of Python.

NumPy is the layer nearly every scientific Python library is standing on, and the array is the only idea in it. This tour builds one, reshapes it, indexes it every way NumPy allows, and does arithmetic across it without a loop in sight.

The parts worth slowing down for are broadcasting, which is either elegant or baffling depending on the day, and the difference between a view and a copy, which is where the surprising bugs live. Dtypes, random generation and saving arrays round it out.

Start this tour

Arrays

Math & shapes

Selection & random

Types & shapes

Indexing

  • Boolean masksA boolean array selects, counts and assigns in one expression.
  • Fancy indexingIndexing with arrays of positions, which copies rather than views.
  • The axis argumentaxis=0 walks down the rows, axis=1 across the columns.

Maths & memory

Randomness & storage

  • The Generator APInp.random.default_rng is the modern, seedable source of randomness.
  • Saving arraysnpy for one array, npz for several, savetxt when a human must read it.

Encore

  • np_kmeans.pyCluster 2-D points with Lloyd's k-means in NumPy.

The other Python tours