NumPy Complete Notes with Examples
What is NumPy?
NumPy (Numerical Python) is a powerful Python library for numerical computations. It provides support for
arrays, matrices, and many mathematical functions.
Installation
Install using pip:
pip install numpy
Importing NumPy
The convention is:
import numpy as np
Creating Arrays
1D Array: [Link]([1, 2, 3])
2D Array: [Link]([[1, 2], [3, 4]])
Zeros: [Link]((2,3))
Ones: [Link]((2,3))
Arange: [Link](0, 10, 2)
Linspace: [Link](0, 1, 5)
Array Attributes
- shape: [Link]
- size: [Link]
- ndim: [Link]
- dtype: [Link]
Indexing and Slicing
Access: arr[1], arr[1:3], arr[:, 0]
Negative Index: arr[-1]
NumPy Complete Notes with Examples
Boolean Indexing: arr[arr > 5]
Array Operations
Element-wise operations:
- Addition: arr1 + arr2
- Multiplication: arr1 * arr2
Matrix multiplication: [Link](arr1, arr2)
Mathematical Functions
Common functions:
- [Link](), [Link](), [Link](), [Link](), [Link]()
- [Link](), [Link](), [Link](), [Link]()
Reshaping Arrays
Use reshape(): [Link](2, 3)
Flatten: [Link]()
Useful NumPy Functions
- [Link]()
- [Link]()
- [Link]()
- [Link]()
- [Link](condition, x, y)
Random Module in NumPy
- [Link](2, 2): uniform dist
- [Link](2, 2): normal dist
- [Link](0, 10, size=5)
- Set seed: [Link](42)
NumPy Complete Notes with Examples
Example Program
import numpy as np
arr = [Link]([[1, 2, 3], [4, 5, 6]])
print("Shape:", [Link])
print("Sum:", [Link](arr))
print("Mean:", [Link](arr))