Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Tiago Rodrigues Antao

Rating 3.5 AI initial rating

Master these effective techniques to reduce costs and run times, handle huge datasets, and implement complex machine learning applications efficiently in Python. Fast Python for Data Science is a hands-on guide to writing Python code that can process more data, faster, and with less resources. It takes a holistic approach to Python performance, showing you how your code, libraries, and computing architecture interact and can be optimized together. Written for experienced practitioners, Fast Python for Data Science dives right into practical solutions for improving computation and storage efficiency. You'll experiment with fun and interesting examples such as rewriting games in lower-level Cython and implementing a MapReduce framework from scratch. Finally, you'll go deep into Python GPU computing and learn how modern hardware has rehabilitated some former antipatterns and made counterintuitive ideas the most efficient way of working.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, architecture-aware guide for experienced Python developers who need to make data-heavy code faster and leaner—not by switching languages, but by profiling, choosing better data structures, and exploiting concurrency, compiled extensions, and GPUs. Read it if you already write Python daily and want a systematic toolkit for large datasets. 【Book Arc】 - **Opening (~0%–10%)**: Sets the stakes—Python is slow by design (dynamic typing, GC, CPython), and scaling hardware is neither cheap nor simple. Frames optimization as a judgment call balancing compute cost, complexity, and energy, and previews the book's four parts. - **Early (~10%–32%)**: Pure-Python performance fundamentals. Profiling with cProfile and line profilers, taming I/O with local caches, choosing lists vs. sets vs. dicts by complexity, estimating memory footprints, and using generators for lazy pipelines. - **Middle (~32%–48%)**: Concurrency and parallelism. Builds an asyncio server scaffold, then a MapReduce engine, then a threaded/concurrent version—confronting the GIL, futures, and when threading helps versus when multiprocessing is required. - **Late (~48%–70%)**: Hardware-aware numerics and libraries. NumPy and NumExpr exploit CPU, memory, storage, and network characteristics; pandas optimization builds on NumPy and extends to Cython and Arrow. - **Late (~70%–90%)**: Data persistence and scale-out. Parquet for columnar storage, Zarr for huge on-disk arrays, strategies for datasets larger than memory, and Dask for distributed workflows. - **Ending (~90%–100%)**: GPU computing—many simple cores suit modern data science—covering both familiar-interface libraries and lower-level approaches, plus the counterintuitive patterns modern hardware has rehabilitated. 【Key Takeaways】 - **Profile before you optimize** (Early): Start with cProfile for a fast, high-level view; escalate to line profiling only when you need per-line detail. The book's opening anecdote—a list search that took hours until converted to a set—shows how routine profiling prevents trivial bottlenecks from becoming outages. - **Data structure choice is the cheapest win** (Early): List, set, and dictionary complexity differences can change runtimes by orders of magnitude at near-zero development cost. Understand the memory overhead of each representation, including list over-allocation and the "lots of tiny objects" trap. - **Laziness beats eagerness for big data** (Early): Generators and lazy constructs (e.g., `range` vs. `list(range(...))`) avoid materializing huge collections, cutting both time and memory—an essential habit for pipelining datasets that don't fit comfortably in RAM. - **Python's parallelism is constrained, so pick the right tool** (Middle): The GIL limits threaded CPU parallelism; asyncio reduces wait times for I/O-bound work, threads suit some concurrent I/O patterns, and multiprocessing is needed to use all cores. The MapReduce build makes these trade-offs concrete. - **Rewrite only the hot 10%** (Early–Middle): When pure Python isn't enough, move the small critical section to Cython or a lower-level language—over 90% of core scientific libraries still run Python. This keeps maintainability while buying speed where it matters. - **Hardware awareness changes algorithm design** (Late): NumExpr and NumPy optimization exploit CPU caches, memory bandwidth, and vectorization; understanding the machine underneath explains why some "slow" patterns are fast in practice. - **Storage format is a performance decision** (Late): Parquet's columnar layout and Zarr's chunked on-disk arrays enable efficient processing of datasets larger than memory—persistence choices belong in your optimization plan, not after it. - **Modern hardware rehabilitates former antipatterns** (Ending): GPU computing's many-simple-cores model makes some counterintuitive approaches the most efficient ones today; the book closes by showing how hardware shifts rewrite the rules. 【Reading Tips】 - **Deep-read Chapters 2–3** if you're new to profiling or concurrency; they deliver the highest return per page and underpin everything later. - **Skim the asyncio server scaffold** if you already know asyncio—extract the GIL and futures discussion instead, which is the real payload. - **Treat the MapReduce build as a lab, not a recipe**: the author explicitly says it's one solution among many; focus on the techniques and trade-offs, not the exact code. - **Read pandas/NumPy/Arrow together** (Chapter 7 builds on Chapter 4), and pair Parquet/Zarr with the "larger than memory" discussion as one unit. - **Take away a decision framework**, not a checklist: the book repeatedly stresses there are no one-size-fits-all recipes—match the technique to your data volume, budget, and team. 【Coverage Limits】 These excerpts cover the book's structure, early profiling/data-structure material, and the concurrency chapters in detail, but the NumPy/NumExpr, pandas/Arrow, Parquet/Zarr, Dask, and GPU chapters are represented mainly by table-of-contents and preface summaries—specific techniques there are not covered in depth.
Page 6
The memory footprint of some alternative representations 35 Using arrays as a compact representation alternative to lists 37 Systematizing what we have learn...
View in text
Excerpt 2
rd, but it will probably be a few orders of magnitude lower. The somewhat prevailing wisdom that what works for those companies is also a good fit for the re...
View in text
Excerpt 3
t range(1000000) 189 ns ± 22.6 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops ➥ each) This gives you the run time of several runs of the function th...
View in text
Excerpt 4
riter) asyncio to call accept_requests for each connection. Our server async def main(): will be listening on the local server = await asyncio.start_server(...
View in text
Excerpt 5
. As such, we use the signal library and instruct each pool process (signal.SIG_IGN) to ignore the interrupt signal (signal.SIGINT). We want our main thread...
View in text
Excerpt 6
n-Python implementation typically in C or Fortran, probably making use of vectorized CPU operations or even running over a GPU. At this stage, we will not be...
View in text
Excerpt 7
import pstats profiling code from within. import pyximport The pstats module processes import numpy as np the output of the profiler. from PIL import Image p...
View in text
Excerpt 8
risons with the Cython one. Running the Python version at a resolution of 1000 × 1000 for 200 generations on my computer takes slightly less than 1000 s, whi...
View in text
Tags
AI categories
PythonDataTechnology
ISBN: 1617297933
Publish Year: 2023
Language: English
Pages: 304
File Format: PDF
File Size: 8.9 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…