You are on page 1of 7

Vectorization

operation
NumPy Vectorization
NumPy vectorization involves
performing mathematical operations
on entire arrays, eliminating the
need to loop through individual
elements.

We've used the concept of


vectorization many times in NumPy.
It refers to performing element-wise
operations on arrays.
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. Indexing starts
from 0. Let's see an array example below to understand the concept of indexing:
elements of array : 2 3 11 9 6 4 10 12
index: 0 1 2 3 4 5 6 7

We can perform indexing on arrays in many ways. Let's discuss how indexing is performed
on arrays.
Indexing Using Index Arrays
In NumPy arrays, when arrays are used as indexes to access groups of elements, this is
called indexing using index arrays. NumPy arrays can be indexed with arrays or with any
other sequence like a list, etc.
Types of Indexing
There are two types of indexing:
Basic indexing
Advanced indexing
Basic Slicing and indexing
Using basic indexing and slicing we can access a particular element or group of
elements from an array. Basic indexing and slicing return the views of the
original arrays.

Basic slicing occurs when the arr[index] is: a slice object


(constructed by start: stop: step) an integer a tuple of slice
objects and integer
Slicing an Array
Slicing a NumPy array refers to accessing elements from a
NumPy array in a specific range using NumPy indexing. It obtains
a subtuple, substring, or sublist from a tuple, string, or list

1) As slicing is performed on Python lists, in the same way, it is


performed on NumPy arrays

The syntax of slicing is as follows:


Syntax: arr_name[start:stop:step] Start: Starting
index Stop: Ending index Step: Difference between
the index
Slicing 1D NumPy Arrays
Slicing a 2D Array

In a 2-D array, we have to


specify start:stop 2 times. One
for the row and 2nd one for the
column.

You might also like