Indexing and slicing
From a data table you almost always need only a part: one column, the first rows, the objects priced above a threshold. A numpy array returns such parts by index, like a list, but it can do more:
import numpy as np
a = np.array([120, 95, 300, 210, 80])
print(a[0], a[-1]) # 120 80
print(a[1:3]) # [ 95 300]
print(a[:2], a[3:]) # [120 95] [210 80]The index 0 is the first element, and -1 is the last. The slice a[1:3] takes the elements from index 1 up to index 3, not including it. An omitted start means "from the first element", and an omitted end means "to the last". A slice of an array is an array too, and it has shape, sum and mean as well.
Rows and columns
A two-dimensional array takes two indices separated by a comma: the row first, then the column. A colon in place of an index means "all": X[:, 1] is the second column of every row. A slice of rows keeps all the columns, so X[1:] is the table without its first row.
X = np.array([[1.5, 20.0], [2.0, 35.0], [0.5, 12.0]])
print(X[1, 0]) # 2.0
print(X[:, 1]) # [20. 35. 12.]
print(X[1:].shape) # (2, 2)In machine learning, a row is usually an object and a column is a feature. So X[:, 1] reads as "the second feature of every object". A slice and an index can be combined: X[:2, 1] is the second feature of the first two objects.
Boolean mask
Comparing an array with a number runs for each element and gives an array of True and False. Such an array is called a boolean mask. If you pass a mask in place of an index, the elements marked True remain:
mask = a > 100
print(mask) # [ True False True True False]
print(a[mask]) # [120 300 210]
print(mask.sum(), a[mask].sum()) # 3 630
print(a[(a > 100) & (a < 250)]) # [120 210]A mask keeps the order: a[mask] lists the matching values as they stood in a. The sum of a mask counts True as 1 and False as 0. So mask.sum() is the number of matching elements, and a[mask].sum() is the sum of their values.
The & operator ("and") joins two conditions. Each comparison goes in parentheses: without them the expression raises ValueError.
Common mistake
The first feature is taken with a single index:
first_feature = X[0] # ✗
print(first_feature) # [ 1.5 20. ]Python reports no error, but X[0] is the first row, that is, every feature of the first object. The first index always selects rows, even when the second is omitted. The first feature of every object is X[:, 0].
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.