Numpy Array Indexing
In NumPy, each element in an array is associated # change the value of the first element
with a number. The number is known as an array numbers[0] = 12
index. print("After modifying first element:",numbers) #
Let's see an example to demonstrate NumPy array prints [12 4 6 8 10]
indexing.
# change the value of the third element
numbers[2] = 14
print("After modifying third element:",numbers)
# prints [12 4 14 8 10]
Output
After modifying first element: [12 4 6 8 10]
After modifying third element: [12 4 14 8 10]
Array Indexing in NumPy
In the above array, 5 is the 3rd element. However,
In the above example, we have modified elements
its index is 2.
This is because the array indexing starts from 0, of the numbers array using array indexing.
that is, the first element of the array has index 0, numbers[0] = 12 - modifies the first element
the second element has index 1, and so on. of numbers and sets its value to 12
Now, we'll see how we can access individual items numbers[2] = 14 - modifies the third element
from the array using the index number. of numbers and sets its value to 14
Access Array Elements Using Index NumPy Negative Array Indexing
We can use indices to access individual elements of NumPy allows negative indexing for its array. The
a NumPy array. index of -1 refers to the last item, -2 to the second
Suppose we have a NumPy array: last item and so on.
array1 = [Link]([1, 3, 5, 7, 9])
Now, we can use the index number to access array
elements as:
array1[0] - to access the first element, i.e. 1
array1[2] - to access the third element, i.e. 5 NumPy Array Negative Indexing
array1[4] - to access the fifth element, i.e. 9 Let's see an example.
import numpy as np
Example: Access Array Elements Using Index
import numpy as np # create a numpy array
numbers = [Link]([1, 3, 5, 7, 9])
array1 = [Link]([1, 3, 5, 7, 9])
# access the last element
# access numpy elements using index print(numbers[-1]) # prints 9
print(array1[0]) # prints 1
print(array1[2]) # prints 5 # access the second-to-last element
print(array1[4]) # prints 9 print(numbers[-2]) # prints 7
Output
Note: Since the last element of array1 is at index 4,
if we try to access the element beyond that, say 9
index 5, we will get an index error: IndexError: 7
index 5 is out of bounds for axis 0 with size 5
Modify Array Elements Using Negative
Modify Array Elements Using Index Indexing
We can use indices to change the value of an Similar to regular indexing, we can also modify
element in a NumPy array. For example, array elements using negative indexing. For
import numpy as np example,
import numpy as np
# create a numpy array
numbers = [Link]([2, 4, 6, 8, 10]) # create a numpy array
numbers = [Link]([2, 3, 5, 7, 11])
2nd Element at First Row: 3
# modify the last element Access Row or Column of 2D Array Using
numbers[-1] = 13 Indexing
print(numbers) # Output: [2 3 5 7 13] In NumPy, we can access specific rows or columns
of a 2-D array using array indexing.
# modify the second-to-last element Let's see an example.
numbers[-2] = 17 import numpy as np
print(numbers) # Output: [2 3 5 17 13]
Here, numbers[-1] = 13 modifies the last element # create a 2D array
to 13 and numbers[-2] = 17 modifies the second- array1 = [Link]([[1, 3, 5],
to-last element to 17. [7, 9, 2],
[4, 6, 8]])
Note: Unlike regular indexing, negative indexing
starts from -1 (not 0) and it starts counting from the # access the second row of the array
end of the array. second_row = array1[1, :]
print("Second Row:", second_row) # Output: [7 9
2-D NumPy Array Indexing 2]
Array indexing in NumPy allows us to access and
manipulate elements in a 2-D array. # access the third column of the array
third_col = array1[:, 2]
To access an element of array1, we need to specify
print("Third Column:", third_col) # Output: [5 2 8]
the row index and column index of the element.
Output
Suppose we have following 2-D array,
Second Row: [7 9 2]
array1 = [Link]([[1, 3, 5],
Third Column: [5 2 8]
[7, 9, 2],
[4, 6, 8]])
Here,
Now, say we want to access the element in the third array1[1, :] - access the second row of array1
row and second column we specify the index as: array1[:, 2] - access the third column of array1
array1[2, 1] # returns 6 3-D NumPy Array Indexing
We learned how to access elements in a 2D array.
We can also access elements in higher dimensional
Since we know indexing starts from 0. So to access
arrays.
the element in the third row and second column, we
To access an element of a 3D array, we use three
need to use index 2 for the third row and
indices separated by commas.
index 1 for the second column respectively.
The first index refers to the slice
Example: 2-D NumPy Array Indexing The second index refers to the row
import numpy as np The third index refers to the column.
# create a 2D array Note: In 3D arrays, slice is a 2D array that is
array1 = [Link]([[1, 3, 5, 7], obtained by taking a subset of the elements in one
[9, 11, 13, 15], of the dimensions.
[2, 4, 6, 8]])
Let's see an example.
import numpy as np
# access the element at the second row and fourth
column # create a 3D array with shape (2, 3, 4)
element1 = array1[1, 3] # returns 15 array1 = [Link]([[[1, 2, 3, 4],
print("4th Element at 2nd Row:",element1) [5, 6, 7, 8],
[9, 10, 11, 12]],
# access the element at the first row and second
column [[13, 14, 15, 16],
element2 = array1[0, 1] # returns 3 [17, 18, 19, 20],
print("2nd Element at First Row:",element2) [21, 22, 23, 24]]])
Output
# access a specific element of the array
4th Element at 2nd Row: 15 element = array1[1, 2, 1]
# print the value of the element
print(element)
# Output: 22
Here, we created a 3D array called array1 with
shape (2, 3, 4). This array contains 2 2D arrays,
each with 3 rows and 4 columns.
Then, we used indexing to access a specific
element of array1. Notice the code,
array1[1, 2, 1]
Here,
array1[1, , ,] - access the second 2D array, i.e.
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]
array1[ ,2, ] - access the third row of the 2D array,
i.e.
[21, 22, 23, 24]
array1[ , ,1] - access the second element of the
third row, i.e.
[22]