Chapter 2
Arrays: The Engine Underneath
From a List to an Array
In the Chapter 1 project, every new measure cost another float(), append it to a
This chapter asks a different question: what if a whole column of readings were one value, and you could do arithmetic on the column itself?
This chapter runs in your browser, and none of its code reads a file. Every example
A week of rain as one value
These are the first seven days of January at station ST01, taken from readings.csv. Press Run.
The first line imports NumPy under the short name np. Almost all NumPy code uses that name, so you will see it everywhere.
np.array(...) takes the list of readings and builds an array from it. Printing the array shows the same seven numbers, without commas between them. That is how NumPy prints an array, and it is a quick way to tell an array from a list.
type(rain) is <class 'numpy.ndarray'>. ndarray is NumPy’s name for its array type; the “nd” means it can have any number of dimensions. The arrays in this chapter have one dimension, a row of values, until a later lesson in this chapter adds a second.
dtype: one type for every element
A Python list can hold anything: a float, then a
rain.dtype is float64: every element is a floating-point number stored in 64 bits of memory. The 64 is about how precisely each number is stored. It says nothing about how many elements there are.
Because every element has one known type, NumPy can store the numbers side by side and run one operation over all of them without checking each value first. That is what makes whole-array work fast, and the next lesson relies on it.
shape: how many, in which layout
rain.shape is (7,). The shape lists the array’s size along each dimension, in parentheses. This array has one dimension with seven elements, and the comma after the 7 is how Python writes a group with a single number in it. len(rain) is also 7, as it would be for a list.
The shape matters more once an array has two dimensions. A table of three stations over seven days has the shape (3, 7), and the lesson on axes later in this chapter is about reading that.
What NumPy does with mixed values
NumPy picks one dtype that can hold every value you give it. Press Run to see two cases.
A list of station codes becomes a text array. Its dtype, <U4, means text of up to four characters. The details of that name do not matter for this course; what matters is that it is not a number type, so arithmetic on it would fail.
The second list mixes the whole number 2 with two floats. NumPy stores all three as floats, so the 2 prints as 2. and the dtype is still float64. A list of only whole numbers gives an integer dtype instead, whose exact name can differ from one computer to another. This chapter writes every reading with a decimal point, so its arrays are always float64.
What does this print?
import numpy as np
temps = np.array([2.5, 3.9, 2.7, 3.6, 2.7])
print(temps.shape)What does NumPy make of np.array(["ST01", 4.5])?
You now have an array: one value that holds a whole week of readings, with one dtype and a shape. The next lesson does arithmetic on the whole array at once, the job that took a loop in Chapter 1.