liveBook · Manning

NumPy

As explained in Python Concurrency with asyncio

NumPy is a Python library for numerical computing with multidimensional arrays, matrices, and vectorized mathematical operations.

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

How Python Concurrency with asyncio covers it

How deep it goes
Explains arrays, axes, reshaping, performance, GIL release, profiling, and benchmarking, but defers detailed implementation guarantees.
Examples
Demonstrates row means over a reshaped four-billion-point array and distributes row calculations through a thread pool.
What it assumes
Assumes familiarity with Python threads, asyncio, and the GIL while introducing NumPy’s array-oriented computation through examples.
What you can do after
Apply it via NumPy array operations, then evaluate threaded alternatives with profiling and benchmarks.
How it connects
Connects NumPy with multidimensional arrays, matrices, vectorization, the GIL, multithreading, asyncio, profiling, and benchmarking.

Also explained in

Compare how 7 books explain NumPy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.