BY IQR MUSHTAQ
NumPy (Numerical Python) for AI
NumPy (Numerical Python) is a foundational and essential open-source library for
Artificial Intelligence (AI) and machine learning (ML) development in Python. It
provides high-performance, multi-dimensional array objects and a suite of tools for
numerical computing that are critical for efficiently processing large datasets and
implementing AI algorithms
NumPy mainly handles numerical data
(numbers) in the form of:
Arrays (1D)
Matrices (2D)
Multi-dimensional arrays (ND)
What is an Array? 1D Data
An array is defined as the data structure combining
of a collection of elements (values or variables),
each represented by at least one array index or key.
2D Data → Matrix (2D Array)
Dimensions in Arrays
0-D Arrays
import numpy as np
arr = [Link](42)
print(arr)
1-D Arrays
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
Dimensions in Arrays
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array
import numpy as np
arr = [Link]([[1, 2, 3], [4, 5, 6]])
print(arr)
Check Number of Dimensions?
Dimensions in Arrays
import numpy as np
a = [Link](42)
b = [Link]([1, 2, 3, 4, 5])
c = [Link]([[1, 2, 3], [4, 5, 6]])
d = [Link]([[[1, 2, 3], [4, 5, 6]],
[[1, 2, 3], [4, 5, 6]]])
print([Link])
print([Link])
print([Link])
print([Link])
NumPy Array Indexing
Array indexing is the same as accessing an array element.
You can access an array element by referring to its index number.
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and
the second has index 1 etc.
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[0])
Get third and fourth elements from the following array and add them.
print(arr[2] + arr[3])
Access the element on the first row, second column
import numpy as np
arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])
NumPy Array Slicing
Slicing in python means taking elements from one given index to another
given index.
We pass slice instead of index like this: [start:end].
If we don't pass start its considered 0
If we don't pass end its considered length of array in that dimension
import numpy as np import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
arr = [Link]([1, 2, 3, 4,
5, 6, 7]) print(arr[4:]
print(arr[1:5])
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6,
7])
print(arr[:4])
Array Properties (Important in AI)
print([Link]) # dimensions
print([Link]) # total elements
print([Link]) # data type