WORKING WITH “–1” SUBRANGES WITH ARRAYS
Listing 2.11 displays the content of np2darray2.py that illustrates how to compute exponents of the elements in a NumPy array.
LISTING 2.11: np2darray2.py
import numpy as np
# -1 => "the last element in …" (row or col)
arr1 = np.array([(1,2,3),(4,5,6),(7,8,9),(10,11,12)])
print('arr1:', arr1)
print('arr1[-1,:]:', arr1[-1,:])
print('arr1[:,-1]:', arr1[:,-1])
print('arr1[-1:,-1]:',arr1[-1:,-1])
Listing 2.11 contains a NumPy array called arr1 followed by four print() statements, each of which displays a different subrange of values in arr1. The output from launching Listing 2.11 is here:
(arr1:', array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]]))
(arr1[-1,:]]', array([10, 11, 12]))
(arr1[:,-1]:', array([3, 6, 9, 12]))
(arr1[-1:,-1]]', array([12]))