Home » Machine Learning » Numpy » Numpy Slicing

Numpy Slicing

Numpy arrays support a rich syntax for referring to or modifying values. Basic slicing uses a start:end:step syntax, like lists.

As with lists, all of these arguments are optional. start defaults to the start of the array (index 0), end defaults to the end of the array, and step size defaults to 1.

So here are some valid slices.

import numpy as np

values = np.arange(0, 10, 1)
print(values)

# Items from index 0 up to but not including index 8
print(values[0:8]) 

# Same items but skipping every other item.
print(values[0:8:2]) 

# All items from index 2 onwards
print(values[2:]) 

# Every other item
print(values[::2]) 

# All items; single colon would also work.
print(values[::]) 
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7]
[0 2 4 6]
[2 3 4 5 6 7 8 9]
[0 2 4 6 8]
[0 1 2 3 4 5 6 7 8 9]

Advanced Indexing

If you include something between the index brackets that isn’t part of the standard slice syntax (above), advanced indexing mode is activated.

The possibilities are basically either a list of indices or else a boolean array that filters the list.

import numpy as np

values = np.arange(0, 10, 1)
print(values)

# List of indices
print(values[[1, 3, 5]])

# Boolean filter list to specify which values to return.
print(values[[True, False, True, False, False, False, True, True, True, False]])
[0 1 2 3 4 5 6 7 8 9]
[1 3 5]
[0 2 6 7 8]

Filtering Using Conditions

It’s not very convenient to supply a list of boolean values to specify which elements you want to return in the filtered Numpy array. In the above example, the array contains ten values so we had to supply an array of ten boolean values to filter the list.

However, you can create such lists of booleans by using condition operators with Numpy arrays.

import numpy as np

values = np.arange(0, 10, 1)
print(values)

# Returns a list containing True only where the
# item value is greater than 2
print(values > 2)

# Returns a list containing True only where the
# item value is greater than 2 and less than 8.
# Notice the bitwise & operator is used to combine
# boolean arrays.
# The round brackets are needed to ensure correct
# operator precedence.
print((values > 2) & (values < 8))
[0 1 2 3 4 5 6 7 8 9]
[False False False  True  True  True  True  True  True  True]
[False False False  True  True  True  True  True False False]

At first glance this may not appear very useful, but it enables you to filter Numpy arrays using the following syntax.

Notice we use the bitwise versions of operators to combine expressions. When expressions are combined, we must place the separate parts in round brackets to ensure the expression is evaluated correctly.

import numpy as np

values = np.arange(0, 10, 1)
print(values)

# All items with value less than 4
print(values[values < 4])

# All items with value less than 4 or greater than 7
print(values[(values < 4) | (values > 7)])
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3]
[0 1 2 3 8 9]

Leave a Reply

Blog at WordPress.com.

%d