NumPy Array Slicing
The purpose of this article is to assist you in achieving your learning goals by introducing Numpy array slicing and presenting examples.
Slicing NumPy arrays
Numpy array slicing in Python is moving data from one index to another index.
In place of an index, we provide a slice: [start:end].
It is also possible to define the step as follows: [start:end:step].
In the absence of a start, it is assumed to be zero.
It is assumed the length of the array in that dimension if the limit is not specified.
It’s counted as 1 if we don’t complete step
From the below array, slice items from index 1 to index 6:
Numpy Array Slicing Example: 1
Numpy Array Slicing Example: 2
From index 3 to the tail of the array, slice the items as follows:
Numpy Array Slicing Example: 3
Numpy Array Slicing Example: 4
When slicing through a Numpy array, you can extract items from the start up to (but not including) index 6.
Numpy Array Slicing Example: 5
Numpy Array Slicing Example: 6
Negative Array Slice
In Numpy array slicing, utilizing a negative index indicates starting from the last:
Slice from index -7 to index -4.
NumPy Array Negative Slicing Example: 1
NumPy Array Negative Slicing Example: 2
Python NumPy STEP
The step value is utilized to decide the interval of the slicing.
From index 1 to index 7, the following items have an increment of 2:
Python NumPy STEP Example: 1
Python NumPy STEP Example: 2
Provide all items with an increment of two from the whole array:
Python NumPy STEP Example: 3
Python NumPy STEP Example: 4
Slice 2-D Arrays
Take items index 2 to index 4 from the first array:
Example: 1