Course Lessons
Lesson 2 of 4
NumPy Fundamentals
Master NumPy arrays, vectorized operations, and mathematical functions for efficient numerical computing in Python.
30 minutes
NumPy Fundamentals
NumPy is the foundation of scientific computing in Python, providing powerful array operations and mathematical functions.
What is NumPy?
- N-dimensional arrays: Efficient storage and operations
- Vectorization: Fast operations without loops
- Mathematical functions: Comprehensive library
- Foundation: Base for Pandas, Scikit-learn, and more
Creating Arrays
NumPy arrays are more efficient than Python lists:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
zeros = np.zeros((3, 4)) # 3x4 array of zeros
ones = np.ones((2, 3)) # 2x3 array of ones
range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
Array Properties
Understand your arrays:
- shape: Dimensions of the array
- dtype: Data type of elements
- size: Total number of elements
- ndim: Number of dimensions
Array Operations
Vectorized operations are fast and clean:
- Element-wise arithmetic: +, -, *, /
- Broadcasting: Operations on arrays of different shapes
- Aggregations: sum(), mean(), std(), min(), max()
- Universal functions: sqrt(), exp(), log(), sin()
Indexing and Slicing
Access and modify array elements:
arr[0] # First element
arr[-1] # Last element
arr[1:4] # Slice
arr[arr > 5] # Boolean indexing
Array Reshaping
Change array dimensions:
- reshape(): Change shape without copying data
- flatten(): Convert to 1D array
- transpose(): Swap axes
Best Practices
- Use vectorized operations instead of loops
- Choose appropriate data types for memory efficiency
- Leverage broadcasting for cleaner code
- Pre-allocate arrays when size is known
Code Example
import numpy as np
# Creating arrays
temperatures = np.array([72, 75, 68, 71, 73, 69, 70])
print(f"Shape: {temperatures.shape}, Type: {temperatures.dtype}")
# Array operations (vectorized)
celsius = (temperatures - 32) * 5/9
print(f"Celsius: {celsius.round(1)}")
# Statistical operations
print(f"Mean: {temperatures.mean():.1f}°F")
print(f"Std Dev: {temperatures.std():.1f}°F")
print(f"Range: {temperatures.min()}°F - {temperatures.max()}°F")
# Boolean indexing
hot_days = temperatures[temperatures > 72]
print(f"Hot days (>72°F): {hot_days}")
# 2D arrays (matrices)
sales_data = np.array([
[120, 130, 125], # Week 1
[115, 140, 135], # Week 2
[130, 145, 150], # Week 3
[125, 138, 142] # Week 4
])
print(f"Sales shape: {sales_data.shape}") # (4, 3)
print(f"Total sales: ${sales_data.sum()}")
print(f"Average per week: {sales_data.mean(axis=1).round(1)}")
print(f"Average per day: {sales_data.mean(axis=0).round(1)}")
# Array manipulation
flat_sales = sales_data.flatten()
reshaped = sales_data.reshape(6, 2)
print(f"Reshaped: {reshaped.shape}")
# Mathematical operations
prices = np.array([10.99, 15.50, 8.75])
quantities = np.array([5, 3, 7])
revenue = prices * quantities
print(f"Revenue per product: ${revenue}")
print(f"Total revenue: ${revenue.sum():.2f}")