Indexing and slicing

Posted under » Python Data Analysis on 15 June 2023

From Shape and size of array

You can index and slice NumPy arrays in the same ways you can slice Python lists.

data = np.array([1, 2, 3])

data[1]
2

data[0:2]
array([1, 2])

data[1:]
array([2, 3])

data[-2:]
array([2, 3])

You may want to take a section of your array or specific array elements to use in further analysis or additional operations. To do that, you’ll need to subset, slice, and/or index your arrays. For example, if you start with this array:

a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a[a < 5])
[1 2 3 4]

five_up = (a >= 5)

print(a[five_up])
[ 5  6  7  8  9 10 11 12]

Or you can select elements that satisfy two conditions using the & and | operators:

c = a[(a > 2) & (a < 11)]

print(c)
[ 3  4  5  6  7  8  9 10]

You can also make use of the logical operators & and | in order to return boolean values that specify whether or not the values in an array fulfill a certain condition. This can be useful with arrays that contain names or other categorical values.

five_up = (a > 5) | (a == 5)

print(five_up)
[[False False False False]
 [ True  True  True  True]
 [ True  True  True True]]

Let’s say you have this array. Grab a section of your array from index position 3 through index position 8.

a = np.array([1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
arr1 = a[3:8]

arr1
array([4, 5, 6, 7, 8])

You can also stack two existing arrays, both vertically and horizontally. Let’s say you have two arrays, a1 and a2:

a1 = np.array([[1, 1],
               [2, 2]])

a2 = np.array([[3, 3],
               [4, 4]])

np.vstack((a1, a2))
array([[1, 1],
       [2, 2],
       [3, 3],
       [4, 4]])

np.hstack((a1, a2))
array([[1, 1, 3, 3],
       [2, 2, 4, 4]])

You can split an array into several smaller arrays using hsplit. You can specify either the number of equally shaped arrays to return or the columns after which the division should occur. Let’s say you have this array:

x = np.arange(1, 25).reshape(2, 12)
x
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12],
       [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]])

np.hsplit(x, 3)
  [array([[ 1,  2,  3,  4],
         [13, 14, 15, 16]]), array([[ 5,  6,  7,  8],
         [17, 18, 19, 20]]), array([[ 9, 10, 11, 12],
         [21, 22, 23, 24]])]


np.hsplit(x, (3, 4))
  [array([[ 1,  2,  3],
         [13, 14, 15]]), array([[ 4],
         [16]]), array([[ 5,  6,  7,  8,  9, 10, 11, 12],
         [17, 18, 19, 20, 21, 22, 23, 24]])]

View and copy

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data