Numpy | Indexing - GeeksforGeeks

15 Nov, 2018
NumPy or Numeric Python is a package for computation on homogenous 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.
Example #1:
Output :
TypeError: can't multiply sequence by non-int of type 'list'

Where as this can easily be done with NumPy arrays.
Example #2:
Output :
array([10, 18, 24, 28, 30, 30])
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.
Example #1:
Output :
A sequential array with a negative step:
[10  8  6  4  2]

Elements at these indices are:
[4 8 6]

 Example #2:
Output :
Elements are:
[2 4 7]

 
There are two types of indexing :
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.
Code #1:
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] 

 Code #2:
Output :
Array is:
 [[0  1  2  3  4  5] 
  [6 7 8 9 10 11]
  [12 13 14 15 16 17]
  [18 19 20 21 22 23]
  [24 25 26 27 28 29]
  [30 31 32 33 34 35]]

a[0, 3:5]  =  [3 4]

a[4:, 4:] = [[28 29],
             [34 35]]

a[:, 2] =  [2 8 14 20 26 32]

a[2:;2, ::2] = [[12 14 16],
                [24 26 28]]

The figure below makes the concept more clear:
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.
Output :
[[ 2  5]
 [ 8 11]]

 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.
Output :
[1 3 6]

Combining advanced and basic indexing: When there is at least one slice (:), ellipsis (...) or newaxis in the index (or the array has more dimensions than there are advanced indexes), then the behavior can be more complicated. It is like concatenating the indexing result for each advanced index element
In the simplest case, there is only a single advanced index. A single advanced index can, for example, replace a slice and the result array will be the same, however, it is a copy and may have a different memory layout. A slice is preferable when it is possible.
Output :
[4, 5]
[4, 5]

The easiest way to understand the situation may be to think in terms of the result shape. There are two parts to the indexing operation, the subspace defined by the basic indexing (excluding integers) and the subspace from the advanced indexing part. Two cases of index combination need to be distinguished:
The advanced indexes are separated by a slice, Ellipsis or newaxis. For example x[arr1, :, arr2].The advanced indexes are all next to each other. For example x[..., arr1, arr2, :] but not x[arr1, :, 1] since 1 is an advanced index in this regard.In the first case, the dimensions resulting from the advanced indexing operation come first in the result array, and the subspace dimensions after that. In the second case, the dimensions from the advanced indexing operations are inserted into the result array at the same spot as they were in the initial array (the latter logic is what makes simple advanced indexing behave just like slicing). Boolean Array 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.Code #1
Output :
[80 100]

Code #2
Output :
[1600 6400])

Code #3
Output :
array([[ 5, 5], [16, 4]])

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
Learn C++ Programming Step by Step - A 20 Day Curriculum!
Must Do Coding Questions for Product Based Companies
50 Common Ports You Should Know
GeeksforGeeks Jobathon - Are You Ready For This Hiring Challenge?
How to Find Length of String in Bash Script?
Samsung R&D Internship Interview Experience (On-Campus)
Generate UUID in Golang
How to install PHP in windows 10 ?
Types of Distributed System