Unit 5 - Python Programming
Unit 5 - Python Programming
Definition:
NumPy (Numerical Python) is a library used for numerical computations in Python. It provides
powerful N-dimensional array objects and functions for operations on these arrays. The core of
NumPy is the ndarray (N-dimensional array).
Vectorized Computation:
Vectorization replaces slow Python loops with fast array expressions. For example:
Use Cases:
● Data analysis
● Machine learning preprocessing
● Simulations
Definition:
An ndarray is the core data structure of NumPy that represents arrays of any dimension.
Creating ND arrays:
Attributes of ndarray:
Example:
3. Creating ND Arrays
Purpose:
NumPy allows setting specific data types for efficient memory and compatibility with C-based
libraries.
● int32, int64
● float32, float64
● bool
● complex
Element-wise Operations:
Operations:
● Addition: a + b
● Subtraction: a - b
● Multiplication: a * b
● Division: a / b
Scalar Operations:
These operations are highly optimized in NumPy and run faster than native Python loops.
Indexing:
Slicing:
2D Indexing:
7. Boolean Indexing
Definition:
Allows filtering arrays using boolean conditions.
Example:
Transpose:
Swapping Axes:
These operations are crucial in reshaping data for ML models and image processing.
Definition:
Functions like np.add(), np.sqrt() work element-wise and are faster than Python loops.
Examples:
Other Examples:
Functions:
Example:
Sorting:
Unique:
Set Operations:
● np.intersect1d()
● np.union1d()
● np.setdiff1d()
Definition:
Visualization helps interpret data patterns using graphs and plots.
Libraries:
● matplotlib.pyplot
● seaborn
Examples:
Common Plots:
● Line plot
● Bar chart
● Histogram
● Scatter plot
● Pie chart