NumPy Indexing
In Python, the “Numpy” library is used for numerical computation and it provides a powerful and efficient way to store and manipulate multidimensional arrays of data such as matrices, images, sounds, and more. “Indexing” is the process of specifying the location or position of an element or a group of elements in an array. It allows you to access, modify, slice, filter, and combine arrays in various ways.
This Python post will present a complete guide on NumPy indexing using appropriate examples via the following supported content:
- NumPy Indexing in Python.
- Using NumPy Indexing to Access an Element.
- Using NumPy Indexing to Access “2-D” Array.
- Using NumPy Indexing to Access “3-D” Array.
- Using NumPy Negative Indexing to Access an Array.
NumPy Indexing in Python
In Python, “NumPy Indexing” is a way of accessing and manipulating elements of a “NumPy” array based on their positions or conditions. The “NumPy” arrays are like Python lists, but with more features. The “NumPy” array index means the value or number that is affiliated with each item/element. We can use indexes to get items from the given NumPy arrays. The first item is “0”, the second is “1”, and so on. We can also use negative indexing to get items from the end. The last item is “-1”, the second last is “-2”, and so on.
Let’s briefly understand using the following examples:
Example 1: Using “NumPy Indexing” to Access an Element
The following code is used to access a “NumPy” array element via indexing:
arr = numpy.array([23, 34, 43, 24])
print(arr[2])
In the above code, the “numpy.array()” function is utilized to construct an array. The “arr[2]” notation invokes the third element from the given NumPy array.
Output
The specified element value of the given array has been accessed successfully via indexing.
Example 2: Using NumPy Indexing to Access a “2-D” Array
The below code is used to access a “2-D” array:
array_value = numpy.array([[22, 44, 55, 77], [16, 27, 38, 59]])
print(array_value[0, 1])
According to the above code, the “numpy.array()” function is used to initialize a “2-D” array, and the “array_value[0, 1]” notation is used to access the “2nd” element from the “1st” row of the array.
Output
The specified array element has been accessed successfully.
Example 3: Using “NumPy Indexing” to Access “3-D” Array
The following code is executed to retrieve the array element of the defined “3-D” array:
array_value = numpy.array([[[25, 50], [44, 45]], [[37, 28], [50, 70]]])
print(array_value[0, 1, 1])
According to the above code, the “array_value[0, 1, 1]” notation is used to access the second element of the second array of the first array in the given “3-D” array.
Output
The corresponding element in the given “3-D” array is shown in the above output.
Example 4: Using NumPy “Negative Indexing” to Access an Array
The below code is used to access the given NumPy array from the end side:
array_value = numpy.array([37, 28, 50, 70])
print(array_value[-2])
In the above code, the “numpy.array()” function is used to generate a NumPy array, and the negative indexing “-2” is used to access the element from the end of the given array.
Output
The last array element has been accessed from the given array accordingly.
Conclusion
The “NumPy Indexing” in Python allows you to access and manipulate elements of a NumPy array based on their positions or index values. This approach is used to access the specified element from the given “1-D”, “2-D” and “3-D” array. This article presented a complete guide on “NumPy Indexing” using various examples.
Source: linuxhint.com