runtime
201 / 250
lesson 201 NumPy Foundations · advanced

Arrays and their dtype

A NumPy array holds many numbers of one kind in one block of memory, so one call can work on all of them. Its dtype says what that kind is.

You'll be able to
  • Explain why an array has one dtype for every value, and what happens when the values are mixed
  • Read dtype, shape and size, and convert text readings with astype(float)
  • Reduce a whole array with sum, mean, min and max, and turn NumPy results into plain Python values
The idea

A weather station reports a reading every day; a year is 365 numbers, a hundred stations are 36,500. A Python list can hold them, but every list item is a separate Python object, and every calculation is a Python loop. NumPy stores the numbers side by side in one block of memory, all of the same kind, and runs calculations over the whole block in compiled code.

np.array(values) builds the array. Three attributes describe it: dtype is the kind of value every slot holds (float64 is a 64-bit decimal number), shape is its size along each dimension as a tuple, and size is how many values there are. Methods such as sum(), mean(), min() and max() reduce the whole array to one number without a loop. (np.zeros(n) builds an array of n zeros, handy when you fill values in later.)

Run each example to see what it prints. Running examples doesn't count toward progress.

example-1.py · A week of highs at Wichita
import numpy as np

# Daily high temperatures in Celsius, Wichita, 1 to 7 January 2024 (NOAA GHCN-Daily)
tmax = np.array([-0.5, 6.7, 9.4, 7.2, 1.7, 5.0, 7.8])
print(tmax)
print(tmax.dtype, tmax.shape, tmax.size)
print(tmax.min(), tmax.max(), tmax.mean())

Every slot has the same dtype, so NumPy picks one that can hold all the values. Integers and floats together become float64. One piece of text turns the whole array into text (a dtype such as <U3, "Unicode strings up to 3 characters"). Readings from a CSV file are text, and NOAA writes a trace of rain as T: converting that array to numbers fails on the value that isn't a number.

example-2.py · A trace reading in a rain column
import numpy as np

rain_text = np.array(["0", "0.3", "T", "2.3"])
print(rain_text.dtype)
rain = rain_text.astype(float)
example-3.py · Rain totals from text readings
import numpy as np

def rain_summary(readings):
    rain = np.array(readings).astype(float)
    return rain.sum(), rain.max()

week = ["0", "0", "0.3", "0", "0", "14.7", "2.3"]  # Orlando, 1 to 7 January 2024
total, wettest = rain_summary(week)
print(total, wettest)
print(rain_summary(week))
print(float(total), round(float(total)))

Integers and the platform. For a list of whole numbers NumPy picks a default integer dtype, and that default is platform-dependent: int64 on most 64-bit Linux and macOS machines, int32 on Windows before NumPy 2 and in this course's browser Python, which is 32-bit. When the width matters (large counts, IDs, sums of big values), say it: np.array(counts, dtype=np.int64). And astype(int) on floats cuts off the decimals (2.7 becomes 2); it does not round.

When to use an array: many numbers of one kind that you calculate with as a whole. Mistakes to avoid: mixing text into a numeric array, assuming a default integer width, and passing NumPy scalars on where plain Python values are expected.

Review
  • An array holds values of one dtype; mixed values are converted to one kind (int + float → float64, any text → text).
  • dtype, shape and size describe an array; astype builds a converted copy, and astype(int) cuts decimals off.
  • sum, mean, min and max reduce the whole array; float() turns a NumPy result into a plain Python number.
  • Next: pick single values, rows, columns and every value that passes a test.
Practise this

Loading your progress…

To master this lesson, solve both challenges without hints or the answer, and pass the concept check.

Arrays and their dtype — NumPy Foundations · Runtime