...

Web Technologies - NumPy

Back to Course

Lesson Description


Lession - #296 NumPy Slicing


Slicing in python implies taking components starting with one given index then onto the next given index .
We pass slice rather than index like this: [start:end].
We can likewise characterize the progression, similar to this: [start:end:step].
In the event that we don't pass start its viewed as 0
In the event that we don't pass end its viewed as length of array in that dimension
In the event that we don't pass step its viewed as 1
Example 1: Slice elements from index 1 to index 5 from the following array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7]>
print(arr[1:5]>

Example 2:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7]>
print(arr[1:5]>


Negative Slicing

Use the minus operator to refer to an index from the end:
Example
Slice from the index 3 from the end to index 1 from the end:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7]>
print(arr[-3:-1]>


Slicing 2-D Arrays

Example
From the second element, slice elements from index 1 to index 4 (not included>
:
import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]>
print(arr[1, 1:4]>


Example
From both elements, return index 2:
import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]>
print(arr[0:2, 2]>

Example
From both elements, slice index 1 to index 4 (not included>
, this will return a 2-D array:

import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]>
print(arr[0:2, 1:4]>








Scientific Library for Python. ... pip install scipy ... SciPy (pronounced “Sigh Pie”>
is open-source software for mathematics, science
Python List Slicing ... With this operator, one can specify where to start the slicing, where to end, and specify the step.
There are two ways to create empty NumPy array can be created: numpy.zeros and numpy.empty .
NumPy Data Types · i – integer · b – boolean · u – unsigned · f – float · c – complex float · m – timedelta · M – datetime · O – object .
Numpy Factorial Function · Python numpy numpy.math.factorial(num>
method accepts a positive integer number as a argument.