NumPy (Numerical Python) is the fundamental library for numerical computing in Python. It is widely used in data science, machine learning, scientific research, statistics, and any task that involves large-scale numerical data.
NumPy introduces powerful data structures like arrays, along with efficient mathematical operations. It is faster and more memory-efficient than Python lists, making it essential for high-performance computing.
NumPy is a Python library that provides:
NumPy is faster because it internally uses optimized C code, making operations highly efficient.
pip install numpy
Then import it:
import numpy as np
NumPy arrays are different from Python lists:
| Feature | Python List | NumPy Array |
|---|---|---|
| Speed | Slow | Fast |
| Memory Usage | High | Low |
| Supports Vectorized Ops | No | Yes |
| Type | Mixed | Same data type |
| Best For | General use | Numerical computing |
Example of vectorized computation:
import numpy as np
a = np.array([1, 2, 3])
b = a * 2
print(b)
Output:
[2 4 6]
Lists cannot perform arithmetic like this.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
arr = np.array([[1, 2], [3, 4]])
print(arr)
| Function | Purpose |
|---|---|
| np.zeros(n) | Array of zeros |
| np.ones(n) | Array of ones |
| np.arange(start, stop, step) | Range of numbers |
| np.linspace(start, stop, num) | Evenly spaced numbers |
| np.eye(n) | Identity matrix |
Examples:
np.zeros(5)
np.ones((2, 3))
np.arange(0, 10, 2)
np.linspace(1, 5, 5)
np.eye(3)
NumPy provides useful attributes:
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim) # number of dimensions
print(arr.shape) # rows, columns
print(arr.size) # total elements
print(arr.dtype) # data type
arr = np.array([10, 20, 30, 40])
print(arr[1])
arr = np.array([10, 20, 30, 40])
print(arr[1])
arr = np.array([[10, 20, 30],
[40, 50, 60]])print(arr[1, 2]) # row 1, col 2
print(arr[:, 1]) # all rows, col 1
NumPy supports fast mathematical operations on arrays without loops.
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])print(a + b)
print(a * b)
print(a ** 2)
print(a / 2)
NumPy provides built-in statistical operations:
arr = np.array([10, 20, 30, 40])
print(arr.mean())
print(arr.max())
print(arr.min())
print(arr.sum())
arr = np.array([1, 2, 3, 4, 5, 6])
print(arr.reshape(2, 3))
Reshape changes the structure without modifying data.
np.concatenate((a, b))
np.vstack((a, b))
np.hstack((a, b))
NumPy supports linear algebra.
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])print(np.dot(a, b))
print(a.T)
NumPy has a random module:
np.random.rand(3)
np.random.randint(1, 10, 5)
Powerful for selecting elements based on conditions.
arr = np.array([10, 20, 30, 40, 50])
print(arr[arr > 25])
Save array:
np.save("data.npy", arr)
Load array:
np.load("data.npy")