BASIC SLICING AND ADVANCED INDEXING IN NUMPY.pptx

duraikaviya13 79 views 19 slides Sep 18, 2024
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

slicing and Indexing


Slide Content

BASIC SLICING AND ADVANCED INDEXING IN NUMPY By Kaviya.A & Sowjanya.S

contents Index Slicing Negative Indices Specify Step of the Slicing Reversing Elements of Data Structure Slice() Function in Python Slice Insertion in Python Slice Deletion in Python

index An index is a position of an individual character or element in a list, tuple, or element in a list, tuple, or string. The index value always starts at zero and ends at one less than the number of items . Negative indexes enable users to index a list, tuple, or other indexable containers from the end of the container , rather than the start. 0 1 2 3 4 5 a -6 -5 -4 -3 -2 -1

sLICING Slicing is the extraction of a part of a string , list, or tuple. It enable users to access the specific range of elements by mentioning their indices. SYNTAX Object[ start:stop:step ] “START” specifies the starting index of a slice “STOP” specifies the ending element of a slice * You can use one of these if you want to skip certain items

Example

Specify step of slicing #string #lsit #tuple Output: Output: Output: LEA [2,3,4,5,6,7 ] (4,5,6)

Reversing elements of data structure We can use a negative step to reverse the elements of a data structure Output: Output:

Slicing() Function in Python The slicing()method extracts a section of data and returns it as new data , Without modifying it. This means users can take a specific range of elements without changing it A Sequence of objects of any type(string , list , tuple or range) or the object which implements__ getitem __() and __ len __() method then this object can be sliced using slice()method. Return Type: Returns a sliced object containing elements in the given range only.

Syntax : slice( start,stop,step ) Example: Output: Output: Output: Output: IMP [1,2,3] [2,3,4,5] [2.4,6]

Slice insertion in python User can insert a items into a list without replacing other elements . EXAMPLE: #insert at start Output: [1,2,3,’a’,’b’,’c’,’d’] #insert at last Output: [ ‘a’, ‘b’, ’c’, ’d’,1,2,3]

Slice deletion in python Users can delete multiple items out of the data structure by using a del statement. EXAMPLE: Output: array(‘ i ’ ,[1,2,3,4,5]) *NOTE Tuple object does not support item deletion

NumPy – A dvanced i ndexing It is possible to make a selection from ndarray that is a non-tuple sequence, ndarray object of integer or Boolean data type, or a tuple with at least one item being a sequence object. Advanced indexing always returns a copy of the data. As against this, the slicing only presents a view. There are two types of advanced indexing 1. INTEGER 2. BOOLEAN

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.

examples Output:

examples output

Boolean array indexing This type of advanced indexing is used when the resultant object is meant to be the result of Boolean operations, such as comparison operators. EXAMPLE: Output

N aN -example In this example, NaN (Not a Number) elements are omitted by using ~ (complement operator). EXAMPLE: Output: [1,2,3,4,5]

example The following example shows how to filter out the non-complex elements from an array. Output: [2. +6.j 3.5+5.j]

Thank you
Tags