Paul's Python CoursePPC

NumPy arrays

Not met yet

np.array() turns a list of numbers into an array: a block of numbers of one type, stored side by side. An array has a shape, the number of items along each axis, so 395 students with three marks each is (395, 3). marks[3, 2] is row 3, column 2, and marks[:, 0] is every row of column 0. Methods like sum and mean take axis=, and the axis you name is the one that collapses.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks multiplying a list by a number multiplies each item, rather than repeating the list.
  • Thinks a NumPy array behaves like a list, so * repeats it and + joins two arrays end to end.
  • Thinks axis=0 keeps one result per row. The axis named is the one that is collapsed, so axis=0 gives one result per column.

Where it comes up