Block 11: NumPy Intro & Array Creation
Install NumPy and understand why it's faster than Python lists.
Concepts
- import numpy as np
- np.array() from lists
- Comparing list operations vs vectorized NumPy
- ndarray properties: shape, ndim, dtype, size
Code Examples
Array creation and properties
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.shape, arr.ndim, arr.dtype, arr.size)
List vs NumPy
lst = [1, 2, 3]
arr = np.array(lst)
print(lst * 2) # [1,2,3,1,2,3]
print(arr * 2) # [2,4,6]
Exercise
Convert [1,2,3,4,5] to an array. Print shape, ndim, dtype. Compare speed: sum a list of 1 million numbers with Python vs NumPy (use time module).
Solution
import numpy as np
import time
arr = np.array([1, 2, 3, 4, 5])
print('Shape:', arr.shape, 'ndim:', arr.ndim, 'dtype:', arr.dtype)
# Speed comparison
lst = list(range(1_000_000))
np_arr = np.arange(1_000_000)
t0 = time.perf_counter()
sum(lst)
t1 = time.perf_counter()
print(f'List sum: {t1-t0:.4f}s')
t0 = time.perf_counter()
np_arr.sum()
t1 = time.perf_counter()
print(f'NumPy sum: {t1-t0:.4f}s')Practice Problems
Hint: np.array([[1,2],[3,4]])
Hint: np.array([1.0, 2.0, 3.0])
Application
NumPy is the foundation of pandas, scikit-learn, and scientific Python. Almost all numeric data in data science flows through NumPy arrays.
Case Study
A financial model processes 1M price points. Using Python lists takes 30 seconds; vectorized NumPy completes in 0.3 seconds. The 100x speedup enables real-time dashboards.
Visualization
Create a bar chart comparing Python list sum vs NumPy sum execution time for array sizes 10^4, 10^5, 10^6. Use matplotlib to visualize the speedup.
Homework
Explain broadcasting in your own words (even a rough first idea). We'll verify it later.