0% found this document useful (0 votes)
64 views13 pages

Indexing and Slicing

This document discusses indexing and slicing of NumPy arrays. Indexing is used to access individual elements of an array starting from index 0. Slicing allows extracting subsets of elements by specifying a start and end index. There are different types of indexing like integer indexing and boolean indexing to select elements based on conditions. Slicing can be used to extract rows, columns or subsets from multi-dimensional arrays. Broadcasting allows mathematical operations on arrays of different shapes.

Uploaded by

naveen kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views13 pages

Indexing and Slicing

This document discusses indexing and slicing of NumPy arrays. Indexing is used to access individual elements of an array starting from index 0. Slicing allows extracting subsets of elements by specifying a start and end index. There are different types of indexing like integer indexing and boolean indexing to select elements based on conditions. Slicing can be used to extract rows, columns or subsets from multi-dimensional arrays. Broadcasting allows mathematical operations on arrays of different shapes.

Uploaded by

naveen kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

INDEXING AND SLICING

BY
MR.Naveen kumar M.tech
Introduction
To get some specific data or elements from numpy
arrays, NumPy indexing and slicing are used. Indexing
starts from 0 and slicing is performed using indexing.

Indexing an Array

Indexing is used to access individual elements. It is also


possible to extract entire rows, columns, or planes from
multi-dimensional arrays with numpy indexing. below to
understand the concept of indexing:
Element
2 3 11 9 6 4 10 12
of array

Index 0 1 2 3 4 5 6 7

Types of Indexing
There are two types of indexing:

1. Index Arrays

2. Integer indexing

3. Advanced indexing
1. Index Arrays

Array indexing refers to accessing an array element using an index


number that starts from 0. The difference between indexing and slicing
is that with the former, you simply access the element.

One-D array indexing


You need to pass the index of that element as shown below, to access
the 1-D array.

The output shows 10 is at index 0.


Two-D array indexing

To access the 2-D array, you need to use commas to separate


the integers which represent the dimension and the index of
the element. The first integer represents the row and the other
represents the column.
Indexing in Two-dimensions Here is a diagram of the array:
We can create a 2 dimensional numpy
array from a python list of lists, like
this: import numpy as np
a2 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

We can index an element of the array using two indices - i selects the row, and j selects the column:
print(a2[2, 1]) # 8

2. Integer Indexing

This mechanism helps in selecting any arbitrary item in an array based on


its Ndimensional index. Each integer array represents the number of
indexes into that dimension. When the index consists of as many integer
arrays as the dimensions of the target ndarray, it becomes straightforward.
Example
import numpy as np

x = np.array([[11, 28], [23, 84], [95, 56]])


print ("The array used for Integer
Indexing")
print(x)

y = x[[0,1,2], [0,0,1]]
print("The Output is:")
print(y)
Output

The array used for Integer Indexing

0 [[11 28]

index 1 [23 84]

2 [95 56]]

The Output is:


[11 23 56]
3. Boolean Indexing

Boolean Indexing is a kind of advanced indexing that is used when we


want to pick elements from an ndarray based on some condition using
comparison operators or some other operator.
import numpy as np

x = np.array([[11, 51, 2],[23, 24, 5],


Output
[16, 47, 8] ,[91, 10, 11]])
The array is: [[11 1 2]
[23 24 5]
print("The array is:")
[16 47 8]
print(x)
[91 10 11]]
The items greater than 6
# Now we will print the items greater
are:
than 11
[51 23 24 16 47 91]
print("The items greater than 11 are:")
print(x[x>11])
SLICING AN ARRAY
When slicing an array, you pass the starting index and the ending index
which are separated by a full colon. You use the square brackets as
shown below.

Syntax

<slice> = array[start:stop:step]

Here:
start - index of the first element to be included in the slice

stop - index of the last element (exclusive)

step - step size between each element in the slice


The starting index is 1 and the ending index is 5. Therefore, you will slice
from the second element since indexing in the array starts from 0 up to
the fourth element.
Negative slicing

The minus operator is used to refer to an index from the end of an array;
you slice an array from the end instead of from the start.
Example: Slice from index 4 (from the end) to index 2 (from the end).
Slicing 2-D arrays

You can slice a 2D array in both axes to obtain a rectangular subset


of the original array. For example:
import numpy as np
a2 = np.array([[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]])
print(a2[1:,2:4]) # [[17 18]
# [22 23]
# [27 28]]

This selects rows 1: (1 to the end of bottom of


the array) and columns 2:4 (columns 2 and 3), as
shown here:
Broadcasting in arrays
In mathematical operations, we may need to consider
the arrays of different shapes .Numpy can perform
such operations where the array of different shapes
are involved.

Ex:
Import numpy as np
A=np.array([1,2,3,4,5,6,7])
B=np.array([2,4,6,8,10,12,14])
C=A*B;
Print (C)

Output:
[2,8,18,32,50,72,98]

You might also like