NumPy for Numerical Computation: A Comprehensive Guide
1. Introduction to NumPy
NumPy (Numerical Python) is the foundation of numerical computing in Python. It provides efficient and fast operations on large datasets using multi-dimensional arrays and mathematical functions. It is widely used in data science, machine learning, scientific computing, and financial analysis.
✅ Why Use NumPy?
✔ Faster than Python lists (uses C-based optimizations)
✔ Supports multi-dimensional arrays (ndarrays)
✔ Provides vectorized operations (avoids loops)
✔ Contains advanced mathematical and statistical functions
✔ Essential for machine learning, deep learning, and AI
📌 Use Cases of NumPy:
- Mathematical and statistical operations (mean, median, variance, etc.)
- Matrix operations (dot product, inverse, eigenvalues, etc.)
- Image processing (pixels as numerical arrays)
- Financial computations (stock prices, risk calculations)
- Machine learning & AI (data preprocessing, model building)
2. Installing NumPy
To install NumPy, use the following command:
pip install numpy
📌 Verify Installation:
import numpy as np
print(np.__version__)
3. Creating NumPy Arrays
NumPy arrays (ndarrays) are more efficient than Python lists.
A. Creating a 1D Array
import numpy as np
# Creating a NumPy array from a list
arr = np.array([1, 2, 3, 4, 5])
print(arr)
📌 Key Features:
✔ Supports homogeneous data types
✔ Uses contiguous memory allocation (faster operations)
B. Creating Multi-Dimensional Arrays (2D & 3D)
# 2D Array (Matrix)
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)
# 3D Array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d)
📌 Shapes:
✔ 1D Array → (n,)
✔ 2D Array → (rows, columns)
✔ 3D Array → (depth, rows, columns)
C. Creating Arrays with Default Values
NumPy provides built-in functions to create arrays with predefined values:
# Array of zeros
zeros = np.zeros((3, 3))
print(zeros)
# Array of ones
ones = np.ones((2, 2))
print(ones)
# Identity Matrix
identity = np.eye(3)
print(identity)
# Random Numbers (0 to 1)
random_vals = np.random.rand(3, 3)
print(random_vals)
📌 Use Cases:
✔ Zeros & Ones → Placeholders for algorithms
✔ Identity Matrix → Used in linear algebra
✔ Random Numbers → Monte Carlo simulations, AI
4. NumPy Array Properties
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Shape of the array
print(arr.shape) # (2, 3)
# Data type
print(arr.dtype) # int32 or int64
# Number of elements
print(arr.size) # 6
# Number of dimensions
print(arr.ndim) # 2
📌 Key Insights:
✔ shape
→ Tells rows & columns
✔ dtype
→ Data type (int, float, etc.)
✔ size
→ Total elements
✔ ndim
→ Number of dimensions
5. Indexing & Slicing NumPy Arrays
A. Accessing Elements
arr = np.array([10, 20, 30, 40, 50])
# Accessing elements
print(arr[0]) # First element (10)
print(arr[-1]) # Last element (50)
B. Slicing Arrays
arr = np.array([10, 20, 30, 40, 50])
# Slicing
print(arr[1:4]) # [20, 30, 40]
print(arr[:3]) # [10, 20, 30]
print(arr[-3:]) # [30, 40, 50]
C. Indexing in 2D Arrays
arr_2d = np.array([[10, 20, 30], [40, 50, 60]])
# Access element at row index 1, column index 2
print(arr_2d[1, 2]) # 60
# Access entire row
print(arr_2d[0, :]) # [10, 20, 30]
# Access entire column
print(arr_2d[:, 1]) # [20, 50]
6. Mathematical Operations with NumPy
NumPy supports element-wise operations for fast computation.
A. Basic Arithmetic
arr = np.array([1, 2, 3, 4])
print(arr + 2) # [3, 4, 5, 6]
print(arr * 3) # [3, 6, 9, 12]
print(arr ** 2) # [1, 4, 9, 16]
B. Matrix Operations
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Matrix Addition
print(A + B)
# Matrix Multiplication
print(A @ B)
# Element-wise Multiplication
print(A * B)
📌 Use Cases:
✔ Matrix operations → Used in machine learning & AI
✔ Dot product → Essential in neural networks
7. Statistical Functions in NumPy
arr = np.array([10, 20, 30, 40, 50])
# Mean
print(np.mean(arr)) # 30.0
# Median
print(np.median(arr)) # 30.0
# Standard Deviation
print(np.std(arr)) # 14.14
# Variance
print(np.var(arr)) # 200.0
📌 Use Cases:
✔ Mean & Median → Measure of central tendency
✔ Standard Deviation & Variance → Measure of spread
8. Reshaping & Flattening NumPy Arrays
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Reshape (2x3 to 3x2)
reshaped = arr.reshape((3, 2))
print(reshaped)
# Flatten (convert to 1D array)
flattened = arr.flatten()
print(flattened)
📌 Reshaping is crucial for:
✔ Data preprocessing
✔ Feeding input into machine learning models
9. Saving & Loading NumPy Arrays
arr = np.array([1, 2, 3, 4])
# Save array
np.save("my_array.npy", arr)
# Load array
loaded_arr = np.load("my_array.npy")
print(loaded_arr)
📌 Why Save Arrays?
✔ Avoid recomputation
✔ Store large datasets efficiently
10. Summary
✔ NumPy is the backbone of numerical computing in Python.
✔ ndarrays are faster than Python lists.
✔ Supports mathematical, statistical, and matrix operations.
✔ Essential for data science, AI, and machine learning.
📌 Next Steps:
✅ Learn Pandas for data manipulation
✅ Use NumPy with Scikit-learn for machine learning
✅ Work on real-world datasets
Need hands-on exercises? Let me know!