NumPy Arrays Fundamentals
NumPy arrays are the heart of scientific computing - fast, memory-efficient, and mathematically powerful multidimensional arrays.
| Function | Creates | Use Case |
|---|---|---|
np.array() |
From lists | Basic arrays |
np.zeros() |
Zero arrays | Matrix initialization |
np.ones() |
Ones arrays | Weight initialization |
np.arange() |
Sequences | Index arrays |
np.linspace() |
Even spacing | Plotting data |
Hello NumPy World
🚀
python numpy_intro.py
[1 2 3]
📈 Array Created!
Shape: (3,)
Dtype: int64
Memory: 24 bytes
Speed: 100x faster than lists
Dtype: int64
Memory: 24 bytes
Speed: 100x faster than lists
import numpy as np
# Create basic array
arr = np.array([1, 2, 3])
print("Array:", arr)
print("Shape:", arr.shape)
print("Type:", arr.dtype)
# Common array creation
zeros = np.zeros(5)
ones = np.ones((3, 3))
range_arr = np.arange(0, 10, 2)
linspace = np.linspace(0, 1, 5)
print("Zeros:", zeros)
print("3x3 Ones:\n", ones)
print("Range:", range_arr)
print("Linspace:", linspace)
Vectorized Operations
Universal Functions (ufunc)
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]
print(a * 2) # [2 4 6]
print(np.sqrt(a)) # [1. 1.41 1.73]
Advanced Indexing
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4]) # [20 30 40]
print(arr[::2]) # [10 30 50]
print(arr[[0, 2, 4]]) # [10 30 50]
Reshaping Arrays
flat = np.arange(12)
matrix = flat.reshape(3, 4)
print("Matrix:\n", matrix)
print("Transposed:\n", matrix.T)
Statistical Functions
data = np.random.randn(1000)
print("Mean:", np.mean(data))
print("Std:", np.std(data))
print("Min/Max:", np.min(data), np.max(data))
Multidimensional Arrays
[[1 2 3] [4 5 6] [7 8 9]]
🔢 3x3 Matrix
1
2
3
4
5
6
7
8
9
import numpy as np
# 2D Matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Matrix shape:", matrix.shape)
print("Row 1:", matrix[0])
print("Column 2:\n", matrix[:, 1])
print("Element [1,2]:", matrix[1, 2])
# Matrix operations
print("Sum all:", np.sum(matrix))
print("Row sums:", np.sum(matrix, axis=1))
print("Column sums:", np.sum(matrix, axis=0))
Broadcasting & Linear Algebra
# Broadcasting: arrays of different shapes
a = np.array([1, 2, 3])
b = 2
print("Broadcasting:", a * b) # [2 4 6]
# Dot product
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print("Matrix product:\n", np.dot(A, B))
# Eigenvalues
eigenvals = np.linalg.eigvals(A)
print("Eigenvalues:", eigenvals)
# Solve linear system Ax = b
x = np.linalg.solve(A, np.array([10, 20]))
print("Solution x:", x)
🔍 Broadcasting Example
a = np.array([[1,2,3], [4,5,6]])b = np.array([10, 20, 30])a + b → [[11 22 33], [14 25 36]]
Random Numbers & Statistics
# Random arrays
np.random.seed(42) # Reproducible
rand_normal = np.random.randn(3, 3) # Normal distribution
rand_uniform = np.random.rand(3, 3) # Uniform [0,1]
rand_int = np.random.randint(0, 100, (3, 3))
print("Normal:\n", rand_normal)
print("Uniform:\n", rand_uniform)
# Advanced statistics
data = np.random.randn(10000)
print("Percentiles:", np.percentile(data, [25, 50, 75]))
print("Correlation:", np.corrcoef(np.random.randn(50), np.random.randn(50)))
Performance Optimization
Memory Views & Slicing
# Views (no copy)
arr = np.arange(10)
view = arr[::2]
view[0] = 999
print("Original modified:", arr) # [999 1 2 999 4 ...]
# Vectorized vs loops (100x faster)
data = np.arange(1000000)
%timeit np.sum(data) # Fast
%timeit sum(data.tolist()) # Slow
Universal Functions
# Compile for speed
from numba import jit
@jit(nopython=True)
def fast_sum(arr):
total = 0
for i in arr:
total += i
return total
Production Applications
Machine Learning
TensorFlow, PyTorch
TensorFlow, PyTorch
Data Science
Pandas, Matplotlib
Pandas, Matplotlib
Scientific Computing
Physics, Engineering
Physics, Engineering
Production Projects
1. Image Processing Pipeline
Load, filter, transform, and analyze images with NumPy arrays
2. Financial Time Series
Moving averages, volatility, correlations, risk metrics
3. Monte Carlo Simulations
1M+ path simulations for pricing derivatives and risk analysis
4. Neural Network from Scratch
Forward/backward pass, optimization, matrix operations
Scientific Computing Expert!
⚡ Production NumPy Applications:
🧠 ML Model Training
Matrix ops, gradients
Matrix ops, gradients
📈 Data Analysis
Statistics, aggregation
Statistics, aggregation
🖼️ Computer Vision
Image processing
Image processing
💰 Algorithmic Trading
Real-time analytics
Real-time analytics
🚀 Production Speed
100x faster than pure Python
100x faster than pure Python
Power machine learning, data science, and scientific research! 🚀