Prerequisites : Numpy in Python Introduction
NumPy or Numeric Python is a package for computation on homogeneous n-dimensional arrays. In numpy dimensions are called as axes.
Why do we need NumPy ?
A question arises that why do we need NumPy when python lists are already there. The answer to it is we cannot perform operations on all the elements of two list directly. For example we cannot multiply two lists directly we will have to do it element wise. This is where the role of NumPy comes into play.
Python
list1 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
list2 = [ 10 , 9 , 8 , 7 , 6 , 5 ]
print (list1 * list2)
|
Output :
TypeError: can't multiply sequence by non-int of type 'list'
Where as this can easily be done with NumPy arrays.
Another example,
Python
import numpy as np
list1 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
list2 = [ 10 , 9 , 8 , 7 , 6 , 5 ]
a1 = np.array(list1)
a2 = np.array(list2)
print (a1 * a2)
|
Output :
array([10, 18, 24, 28, 30, 30])
This article will help you get acquainted with indexing in NumPy in detail. Numpy package of python has a great power of indexing in different ways.
Indexing using index arrays
Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples. The last element is indexed by -1 second last by -2 and so on.
Python
import numpy as np
a = np.arange( 10 , 1 , - 2 )
print ( "\n A sequential array with a negative step: \n" ,a)
newarr = a[np.array([ 3 , 1 , 2 ])]
print ( "\n Elements at these indices are:\n" ,newarr)
|
Output :
A sequential array with a negative step:
[10 8 6 4 2]
Elements at these indices are:
[4 8 6]
Another example,
Python
import numpy as np
x = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ])
arr = x[np.array([ 1 , 3 , - 3 ])]
print ( "\n Elements are : \n" ,arr)
|
Output :
Elements are:
[2 4 7]
Types of Indexing
There are two types of indexing :
1. Basic Slicing and indexing : Consider the syntax x[obj] where x is the array and obj is the index. Slice object is the index in case of basic slicing. Basic slicing occurs when obj is :
- a slice object that is of the form start : stop : step
- an integer
- or a tuple of slice objects and integers
All arrays generated by basic slicing are always view of the original array.
Python
import numpy as np
a = np.arrange( 20 )
print ( "\n Array is:\n " ,a)
print ( "\n a[-8:17:1] = " ,a[ - 8 : 17 : 1 ])
print ( "\n a[10:] = " ,a[ 10 :])
|
Output :
Array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
a[-8:17:1] = [12 13 14 15 16]
a[10:] = [10 11 12 13 14 15 16 17 18 19]
Ellipsis can also be used along with basic slicing. Ellipsis (…) is the number of : objects needed to make a selection tuple of the same length as the dimensions of the array.
Python
import numpy as np
b = np.array([[[ 1 , 2 , 3 ],[ 4 , 5 , 6 ]],
[[ 7 , 8 , 9 ],[ 10 , 11 , 12 ]]])
print (b[..., 1 ])
|
Output :
[[ 2 5]
[ 8 11]]
2. Advanced indexing : Advanced indexing is triggered when obj is :
- an ndarray of type integer or Boolean
- or a tuple with at least one sequence object
- is a non tuple sequence object
Advanced indexing returns a copy of data rather than a view of it. Advanced indexing is of two types integer and Boolean.
Purely integer indexing : When integers are used for indexing. Each element of first dimension is paired with the element of the second dimension. So the index of the elements in this case are (0,0),(1,0),(2,1) and the corresponding elements are selected.
Python
import numpy as np
a = np.array([[ 1 , 2 ],[ 3 , 4 ],[ 5 , 6 ]])
print (a[[ 0 , 1 , 2 ],[ 0 , 0 , 1 ]])
|
Output :
[1 3 6]
Boolean Indexing
This indexing has some boolean expression as the index. Those elements are returned which satisfy that Boolean expression. It is used for filtering the desired element values.
Python
import numpy as np
a = np.array([ 10 , 40 , 80 , 50 , 100 ])
print (a[a> 50 ])
|
Output :
[80 100]
Python
import numpy as np
a = np.array([ 10 , 40 , 80 , 50 , 100 ])
print (a[a % 40 = = 0 ] * * 2 )
|
Output :
[1600 6400])
Python
import numpy as np
b = np.array([[ 5 , 5 ],[ 4 , 5 ],[ 16 , 4 ]])
sumrow = b. sum ( - 1 )
print (b[sumrow % 10 = = 0 ])
|
Output :
array([[ 5, 5], [16, 4]])
Reference :
SciPy.org
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Nov, 2022
Like Article
Save Article