Skip to content
Theory · NumPy and pandas · lesson 1 of 12

NumPy arrays

Data for machine learning is tables of thousands of numbers. A Python list can hold them, but computing over it takes a loop, one element at a time:

Python
import numpy as np

prices = [120, 95, 300, 210]
with_tax = []
for p in prices:
    with_tax.append(p * 1.2)
print(with_tax)  # [144.0, 114.0, 360.0, 252.0]

a = np.array(prices)
print(a * 1.2)  # [144. 114. 360. 252.]

The numpy library stores numbers in an array and does arithmetic on all the elements at once. The np.array function turns a list into an array, and the expression a * 1.2 multiplies every element without a loop. This style is called vectorization. It is shorter than a loop and runs tens of times faster on thousands of rows.

Shape and type

An array has a shape attribute—a tuple of lengths along each dimension. The array a is one-dimensional, and its shape is (4,).

A table is written as a list of rows and becomes a two-dimensional array. The shape (3, 2) means three rows and two columns. For such an array, len(X) returns the number of rows, that is, X.shape[0].

Python
X = np.array([[1.5, 20.0], [2.0, 35.0], [0.5, 12.0]])
print(X.shape)  # (3, 2)
print(X.dtype)  # float64
print(np.array([1, 2.5]).dtype)  # float64
print(a / 2)  # [ 60.   47.5 150.  105. ]

The dtype attribute is the data type of the elements, one for the whole array. Fractional numbers are stored as float64. If a list mixes integers and fractions, numpy converts every element to the fractional type.

An array of integers only gets an integer type, but the division a / 2 still gives fractional numbers. The shared type is what makes the arithmetic fast: numpy does not check each element separately, as a Python loop has to.

Sum and mean

The np.sum and np.mean functions compute the sum and the mean of the elements. The mean also exists as a method: a.mean() gives the same number. Arithmetic between an array and a number applies to every element, so deviations from the mean take one line:

Python
print(np.sum(a), np.mean(a), a.mean())  # 725 181.25 181.25
print(a - a.mean())  # [-61.25 -86.25 118.75  28.75]
print(np.array([1, 2, 3]) + np.array([10, 20, 30]))  # [11 22 33]

Two arrays of the same shape add element by element: the first element with the first, the second with the second.

Common mistake

The arithmetic is applied to a list instead of an array:

Python
doubled = prices * 2  # ✗
print(doubled)  # [120, 95, 300, 210, 120, 95, 300, 210]

Python reports no error. Multiplying a list by an integer repeats the list, and adding two lists joins them. Multiplying a list by a fraction raises TypeError. Turn the list into an array with np.array(prices), and then * 2 doubles every number.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way