NumPy provides multidimensional arrays, matrices, and vectorized mathematical operations for data science and machine learning. Its C- and Fortran-backed implementation often outperforms plain Python arrays. section 7.5.2
NumPy helps solve numerical workloads by:
- Applying functions such as mean across an array axis without writing explicit Python loops. section 7.5.2
- Potentially releasing Python’s GIL during low-level operations, making some CPU-bound matrix workloads suitable for multithreading. section 7.5 section 7.5.2
- Requiring profiling and benchmarking before accepting the complexity of threaded execution. section 7.5.2
Row means in a large matrix
Create a one-dimensional array with np.arange, reshape it into 50 rows, and compute each row’s mean with np.mean(matrix, axis=1). A threaded variant submits one row at a time through ThreadPoolExecutor and asyncio. section 7.5.2
Listing 7.18 Means of a large matrix with NumPy
import numpy as np import time data_points = 4000000000 rows = 50 columns = int(data_points / rows) matrix = np.arange(data_points).reshape(rows, columns) s = time.time() res = np.mean(matrix, axis=1) e = time.time() print(e - s)
Inspect the runnable matrix example showing array creation, reshaping, timing, and axis-based means. section 7.5.2






