Open In App

Numpy Array Indexing

Last Updated : 10 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Python
# Python program to demonstrate a need of NumPy

list1 = [1, 2, 3, 4 ,5, 6]
list2 = [10, 9, 8, 7, 6, 5]

# Multiplying both lists directly would give an error.
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.

Example #2:

Python
# Python program to demonstrate the use of NumPy arrays
import numpy as np

list1 = [1, 2, 3, 4, 5, 6]
list2 = [10, 9, 8, 7, 6, 5]

# Convert list1 into a NumPy array
a1 = np.array(list1)

# Convert list2 into a NumPy array
a2 = np.array(list2)

print(a1*a2)

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:

Python
# Python program to demonstrate 
# the use of index arrays.
import numpy as np

# Create a sequence of integers from
# 10 to 1 with a step of -2
a = np.arange(10, 1, -2) 
print("\n A sequential array with a negative step: \n",a)

# Indexes are specified inside the np.array method.
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]

 

Example #2:

Python
import numpy as np

# NumPy array with elements from 1 to 9
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Index values can be negative.
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 :

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:

Python
# Python program for basic slicing.
import numpy as np

# Arrange elements from 0 to 19
a = np.arange(20)
print("\n Array is:\n ",a)

# a[start:stop:step]
print("\n a[-8:17:1]  = ",a[-8:17:1]) 

# The : operator means all elements till the end.
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] 

 

Code #2:

Python
# Python program for basic slicing 
# and indexing
import numpy as np

# A 3-Dimensional array
a = np.array([[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]]
print("\n Array is:\n ",a)

# slicing and indexing
print("\n a[0, 3:5]  = ",a[0, 3:5]) 

print("\n a[4:, 4:]  = ",a[4:, 4:]) 

print("\n a[:, 2]  = ",a[:, 2]) 

print("\n a[2:;2, ::2]  = ",a[2:;2, ::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.

Python
# Python program for indexing using 
# basic slicing with ellipsis
import numpy as np

# A 3 dimensional array.
b = np.array([[[1, 2, 3],[4, 5, 6]],
              [[7, 8, 9],[10, 11, 12]]])

print(b[...,1]) #Equivalent to b[: ,: ,1 ]

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.

Python
# Python program showing advanced indexing
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]

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.

Python
# Python program showing advanced 
# and basic indexing
import numpy as np

a = np.array([[0 ,1 ,2],[3 ,4 ,5 ],
              [6 ,7 ,8],[9 ,10 ,11]])

print(a[1:2 ,1:3 ])
print(a[1:2 ,[1,2]])

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

Python
# You may wish to select numbers greater than 50
import numpy as np

a = np.array([10, 40, 80, 50, 100])
print(a[a>50])

Output :

[80 100]

Code #2

Python
# You may wish to square the multiples of 40 
import numpy as np

a = np.array([10, 40, 80, 50, 100])
print(a[a%40==0]**2)

Output :

[1600 6400])

Code #3

Python
# You may wish to select those elements whose
# sum of row is a multiple of 10.
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]])


Previous Article
Next Article

Similar Reads

Indexing Multi-dimensional arrays in Python using NumPy
In this article, we will cover the Indexing of Multi-dimensional arrays in Python using NumPy. NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. It contains various features.Not
3 min read
Basic Slicing and Advanced Indexing in NumPy
Indexing a NumPy array means accessing the elements of the NumPy array at the given index. There are two types of indexing in NumPy: basic indexing and advanced indexing. Slicing a NumPy array means accessing the subset of the array. It means extracting a range of elements from the data. In this tutorial, we will cover basic slicing and advanced in
5 min read
NumPy Array Sorting | How to sort NumPy Array
Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.argsort() functionUsing np.lexsort() functionUsing s
4 min read
Python | Indexing a sublist
In Python, we have several ways to perform the indexing in list, but sometimes, we have more than just an element to index, the real problem starts when we have a sublist and its element has to be indexed. Let's discuss certain ways in which this can be performed. Method #1 : Using index() + list comprehension This method solves this problem in 2 p
3 min read
Indexing in MongoDB using Python
By creating indexes in a MongoDB collection, query performance is enhanced because they store the information in such a manner that traversing it becomes easier and more efficient. There is no need for a full scan as MongoDB can search the query through indexes. Thus it restricts the number of documents that need to be checked for query criteria. S
2 min read
Label-based indexing to the Pandas DataFrame
Indexing plays an important role in data frames. Sometimes we need to give a label-based "fancy indexing" to the Pandas Data frame. For this, we have a function in pandas known as pandas.DataFrame.lookup(). The concept of Fancy Indexing is simple which means, we have to pass an array of indices to access multiple array elements at once. pandas.Data
3 min read
Indexing Lists Of Lists In Python
Lists of lists are a common data structure in Python, providing a versatile way to organize and manipulate data. When working with nested lists, it's crucial to understand how to index and access elements efficiently. In this article, we will explore three methods to index lists of lists in Python using the creation of a sample list, followed by ex
3 min read
Tensor Indexing in Tensorflow
In the realm of machine learning and deep learning, tensors are fundamental data structures used to represent numerical data with multiple dimensions. TensorFlow, a powerful numerical computation library, equips you with an intuitive and versatile set of operations for manipulating and accessing data within these tensors. Understanding tensor index
10 min read
Difference between Numpy array and Numpy matrix
While working with Python many times we come across the question that what exactly is the difference between a numpy array and numpy matrix, in this article we are going to read about the same. What is np.array() in PythonThe Numpy array object in Numpy is called ndarray. We can create ndarray using numpy.array() function. It is used to convert a l
3 min read
NumPy ndarray.tolist() Method | Convert NumPy Array to List
The ndarray.tolist() method converts a NumPy array into a nested Python list. It returns the array as an a.ndim-levels deep nested list of Python scalars. Data items are converted to the nearest compatible built-in Python type. Example C/C++ Code import numpy as np gfg = np.array([1, 2, 3, 4, 5]) print(gfg.tolist()) Output[1, 2, 3, 4, 5] SyntaxSynt
1 min read
Article Tags :
Practice Tags :