Week7B PBD
Week7B PBD
🔹 1. Introduction to NumPy
What is NumPy?
NumPy (Numerical Python) is a powerful open-source Python library used for numerical
computing.
It provides support for multi-dimensional arrays and matrices, along with a large collection of
mathematical functions to operate on them efficiently.
Efficient array computations
Supports multidimensional arrays (ndarray)
Vectorization (faster operations without using loops)
Broadcasting (operations between arrays of different shapes)
Integration with other scientific libraries like Pandas, Matplotlib, SciPy
Importing NumPy:
import numpy as np
🔹 2. Operations on NumPy
A. Creating Arrays:
np.array([1, 2, 3]) # 1D array
np.array([[1, 2], [3, 4]]) # 2D array
np.zeros((2,3)) # Array of zeros
np.ones((3,2)) # Array of ones
np.arange(0, 10, 2) # Range array
np.linspace(0, 1, 5) # Evenly spaced values
np.arange(11,17).reshape((2,3)) #for 2D array
np.sort(a) #sorting R wise
np.sort(a,axis=0) #sorting C wise
np.argsort(x,axis=1)
B. Indexing and Slicing:
a = np.array([10, 20, 30, 40])
a[1] # 20
a[1:3] # [20, 30]
a[-1] # 40
a[1:6:2]#[Start,stop,step]
a[-1:-3:-1]
a[::-1]
2D
a[1,] #[R,C]
a[:,1]
a[1:3,1:3] #[start R:end R, Start C: End C]
a[1:3,]
a[:,1:3]
a[1:3,1]
a[1:3,:1]
C. Array Operations:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
a+b # array([5, 7, 9])
a*b # array([ 4, 10, 18])
a ** 2 # array([1, 4, 9])
D. Attributes Of Numpy Array
a.ndim #1D,2D
a.shape #number columns and rows
a.size # number of elements
a.dtype #type of data
a.itemsize #memory space
E. Statistical Operations on 1D Array:
np.max() # array of name as a argument
np.min()
np.sum()
np.mean()
np.median()
np.prod()
np.var()
np.std()
F. Mathematical Functions:
np.sin(a)
np.log(a)
np.sqrt(a)
np.mean(a)
np.std(a)
G. Matrix Operations:
A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 3]])
np.dot(A, B) # Matrix multiplication
A.T # Transpose
np.linalg.inv(A) # Inverse
np.linalg.det(A) # Determinant
🔹 3. Use of NumPy
A. Scientific Computing:
Foundation for libraries like SciPy, TensorFlow, Pandas
Efficient mathematical operations on large datasets
B. Data Analysis:
Used to handle and process large arrays and matrices in data science projects
Can clean, filter, and manipulate data efficiently
C. Machine Learning:
Works with large numerical datasets
Used for pre-processing data, transforming input, and feeding arrays into models
D. Image Processing:
Images can be represented as NumPy arrays for pixel-level operations
E. Simulation and Modeling:
Supports simulations that involve linear algebra, random numbers, statistics