Updated April 3, 2023
Introduction to NumPy Indexing
NumPy indexing is the part of the Numpy package which is used for computation in python. This is basically a library that is used to deal with a homogenous multi-dimension array in python. As we know, we already have a list, but NumPy provides us faster manipulation and accessing of array elements because NumPy library most of the code written in C and C++. NumPy indexing is divided into three categories; by using it, we can apply different types of indexing on array elements that are not very flexible in other languages.
NumPy stands for numeric python. This is used for computation in python; it is a computational package; we use this NumPy library to work efficiently with an array in python. NumPy provides us fast access to array elements. By the use of NumPy, we can perform computation on a homogenous n dimension array. We use continuous place in memory to store the NumPy array, which makes it easy to manipulate and access. NumPy library is written on python, but most parts where faster operations are required are written in C.
Syntax of NumPy Indexing
To use NumPy in our program, we need to include one package because, as we discussed, it is a library available in python. In order to use this package, install the NumPy by using the below command also; we will see one practice syntax for a better understanding to using NumPy in our program see below:
- Step 1: pip install numpy
- Step 2: import numpy as name_your_package
- Step 3: Name_of_variable = package_name.array([val1, vale2, val3 ..])
Example:
import numpy as myNum
arr = myNum.array([500, 600, 900, 100, 400])
How indexing work in NumPy?
As we are now a little bit familiar with the NumPy, let’s discuss why we need it when we already have a list available in python. So the list is not very much faster in accessing and manipulating data, whereas NumPy arrays are faster in manipulation and accessing operation because of its most of the code written in C++ and C.
Suppose if we want to multiply the two lists so we cannot do this operation directly, we have to access each element of the list in order to perform any operation, but this is not the case in NumPy array we can do it directly by without being iterating each element. This package provides us with a different way of indexing as well; it is very powerful as well.
Also, we can use the NumPy package with an alias; this is a handy way to use our package or to refer our package in the program while working with them. Basically, the NumPy package provides us with a way to work with the array, and this array object is called as ndarray in python; if we want to create an array, we have array() function available let’s see its syntax;
myArr = np.array([val1, val2 , ...])
NumPy provides us with array indexing which is used to access the elements of an array faster. We can use indexing in different ways; if we want to access one particular element from an array, we will use the index number for that. This index number will start from 0, 1, 2, and so on. In NumPy indexing, it returns us two types of array shallow, and a copy of the original array depends upon the operation that we are currently applying on our array.
NumPy provides us with three types of indexing methods which are as follows see below;
- Advanced indexing
- Field access
- Basic slicing
The slice function takes three parameters to start, stop, and step. As the name suggests, it will start working from the element we will mention until the stop element, and it will take a step as mentioned step element. Let’s see on example to understand it better;
Example:
import numpy
myarr = numpy.arange(10)
result = slice(4,8,1)
print myarr[result]
In the above example, we are creating an array by using the NumPy function available. In the next line, we use the slice function and pass their parameter there as a start, stops, and step. Here 4 is the start, 8 is the stop point, and 1 is the step parameter. So this function will start from 4 and goes till 8 and would take the step of 1. In the next line, we are just printing the result.
We have so many different ways to do this slice indexing we will see an example of each one in the next section.
Points to remember while working with the numPy in python;
- NumPy provides us with faster accessing of array elements in comparison to the list.
- We can use this NumPy with a homogenous n-dimensional array.
- The indexing of the array will start from 0 and so on.
- But we can use this Numpy library with homogenous array dimensions.
Examples of NumPy Indexing
Given below are the examples of NumPy Indexing:
Example #1
In this example, we are just creating an array using numpy and printing the output.
Code:
import numpy as mynum
#creating array using numpy
myArr = mynum.array([1, 2, 3, 4])
#printing output
print("Resul is :")
print(myArr)
Output:
Example #2
In this example, we are multiplying the two arrays here using numpy.
Code:
import numpy as mynum
#creating list here ..
l1 = [20, 60, 40, 10, 5, 90]
myaarr1 = mynum.array(l1)
l2 = [30, 40, 18, 20, 40 , 38]
# we are using array method of numpy here ...
myaarr2 = mynum.array(l2)
print("Result after multiplication is :::")
print(myaarr1*myaarr2)
Output:
Example #3
In this example, we are doing slicing for indexing by using numpy.
Code:
import numpy as mynum
myarr = mynum.arange(20)
#using slice here for indexing ..
result = slice(3, 18, 3)
print("Result is ::")
#priting result here ..
print (myarr[result])
Output:
Example #4
In this example, we are using a slice in different ways by using the NumPy library.
Code:
import numpy as mynum
myarr = mynum.arange(20)
#using slice here for indexing ..
print("Result is ::")
#priting result here ..
print (myarr[5:])
Output:
Example #5
In this example, we are using slice indexing in another way. Provided by NumPy library. The syntax we are using here is different; we are directly passing parameters here using (:) colon.
Code:
import numpy as mynum
myarr = mynum.arange(20)
#using slice here for indexing ..
print("Result is ::")
#priting result here ..
print (myarr[5:10])
Output:
Conclusion
NumPy is a library provided for computation to work with an array in python; also, they provide faster accessing of array elements. We can do indexing in one of three ways in python, i.e. basic, field, and advanced, and they return us shallow or copy of the original array. Indexing starts at 0 and so on. NumPy library is written n C++ and C.
Recommended Articles
This is a guide to NumPy Indexing. Here we discuss How indexing works in NumPy and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –