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 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 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 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(" A sequential array with a negative step: ",a) # Indexes are specified inside the np.array method. newarr = a[np.array([3, 1, 2 ])] print(" Elements at these indices are: ",newarr)

**Output : **

A sequential array with a negative step: [10 8 6 4 2] Elements at these indices are: [4 8 6]

**Example #2:**

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(" Elements are : ",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 program for basic slicing. import numpy as np # Arrange elements from 0 to 19 a = np.arange(20) print(" Array is: ",a) # a[start:stop:step] print(" a[-8:17:1] = ",a[-8:17:1]) # The : operator means all elements till the end. print(" 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 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(" Array is: ",a) # slicing and indexing print(" a[0, 3:5] = ",a[0, 3:5]) print(" a[4:, 4:] = ",a[4:, 4:]) print(" a[:, 2] = ",a[:, 2]) print(" 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 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 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 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**

# 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**

# 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**

# 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]])

## leave a comment

## 0 Comments