Machine Learning with Large Datasets (Fall 2026)
Recitation 1
August 28th, 2025
Special thanks to Tian Li and Prof. Heather Miller who developed some of the material covered today.
Agenda
1. Brief Introduction to Google Colab
2. Polars Basics
3. Polars Essentials
4. Polars Limitations
3. Appendix
4. Open Q&A
Homework Setup
Programming Assignments
Written Assignments
More info on Course Website - https://10605.github.io/
To-Dos for students
Registration and Uploading Notebooks
Exporting the Homework
Polars: Background and History
Polars: Essential Data Structure API
Polars operates on a DataFrame, which is a 2D representation of data (rows and columns) that is (roughly speaking) an abstraction of a spreadsheet.
┌───────┬─────────────┐
│ sales │ customer_id │
│ --- │ --- │
│ f64 │ i64 │
╞═══════╪═════════════╡
│ 150.0 │ 24 │
│ 300.0 │ 25 │
│ 250.0 │ 26 │
└───────┴─────────────┘
Here are some of the essential operations that you can perform on a dataframe:
Polars: Lazy vs Eager Evaluation
DataFrame (Eager evaluation)
LazyFrame (Lazy Evaluation)
Polars: Streaming Engine
For larger than memory computation Polars using the streaming engine with a LazyFrame.
The streaming engine will run batches of data through the query pipeline until it encounters a sink or a pipeline blocker.
This should be your default choice for writing high-performance Polars code, it is what we will focus our attention on in this course.
Morsel driven parallelism from database research provided the inspiration for the streaming engine. Source: Leis et al. 2014
Polars: Model of a Group By
Code:
lf = pl.scan_parquet(input_path)
lf = (lf
.group_by("key")
.agg(pl.col("value").mean().alias("mean"))
)
lf.sink_parquet(output_path)
Input
┌────────┬─────┬───────┐
│ row_id ┆ key ┆ value │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ i64 │
╞════════╪═════╪═══════╡
│ r1 ┆ A ┆ 4 │
│ r2 ┆ B ┆ 2 │
│ r3 ┆ A ┆ 3 │
│ r4 ┆ C ┆ 5 │
│ r5 ┆ B ┆ 1 │
│ r6 ┆ A ┆ 2 │
└────────┴─────┴───────┘
Output
┌─────┬──────┐
│ key ┆ mean │
│ --- ┆ --- │
│ str ┆ f64 │
╞═════╪══════╡
│ C ┆ 5.0 │
│ A ┆ 3.0 │
│ B ┆ 1.5 │
└─────┴──────┘
Polars: Model of a Group By
Polars: Model of a Join
Code:
lf = pl.scan_parquet(input_path)
lf = (lf
.group_by("key")
.agg(pl.col("value").mean().alias("mean"))
)
lf.sink_parquet(output_path, engine="streaming")
Input
┌────────┬─────┬───────┐
│ row_id ┆ key ┆ value │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ i64 │
╞════════╪═════╪═══════╡
│ r1 ┆ A ┆ 4 │
│ r2 ┆ B ┆ 2 │
│ r3 ┆ A ┆ 3 │
│ r4 ┆ C ┆ 5 │
│ r5 ┆ B ┆ 1 │
│ r6 ┆ A ┆ 2 │
└────────┴─────┴───────┘
Output
┌─────┬──────┐
│ key ┆ mean │
│ --- ┆ --- │
│ str ┆ f64 │
╞═════╪══════╡
│ C ┆ 5.0 │
│ A ┆ 3.0 │
│ B ┆ 1.5 │
└─────┴──────┘
Polars: Model of a Join
Code:
build_lf = pl.scan_parquet(build_path)
probe_lf = pl.scan_parquet(probe_path)
output_lf = (
build_lf
.join(
probe_lf,
on="key",
)
)
output_lf.sink_parquet(output_path, engine="streaming")
Input build_lf
┌──────────────┬─────┐
│ build_row_id ┆ key │
│ --- ┆ --- │
│ str ┆ str │
╞══════════════╪═════╡
│ b1 ┆ A │
│ b2 ┆ A │
│ b3 ┆ A │
│ b4 ┆ B │
│ b5 ┆ B │
│ b6 ┆ C │
└──────────────┴─────┘
Input probe_lf
┌──────────────┬─────┐
│ probe_row_id ┆ key │
│ --- ┆ --- │
│ str ┆ str │
╞══════════════╪═════╡
│ p1 ┆ A │
│ p2 ┆ B │
│ p3 ┆ A │
│ p4 ┆ C │
└──────────────┴─────┘
Output
┌──────────────┬─────┬──────────────┐
│ build_row_id ┆ key ┆ probe_row_id │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞══════════════╪═════╪══════════════╡
│ b1 ┆ A ┆ p1 │
│ b1 ┆ A ┆ p3 │
│ b2 ┆ A ┆ p1 │
│ b2 ┆ A ┆ p3 │
│ b3 ┆ A ┆ p1 │
│ b3 ┆ A ┆ p3 │
│ b4 ┆ B ┆ p2 │
│ b5 ┆ B ┆ p2 │
│ b6 ┆ C ┆ p4 │
└──────────────┴─────┴──────────────┘
Polars: Model of a Join
Polars: Model of “map_batches(. , streamable=True)”
Polars: User Defined Functions
As of Fall 2026, CPython (the reference, default, and most widely used implementation of the Python language) uses a Global Interpreter Lock (GIL) for multithreading.
Polar’s Rust API circumvents this limitation. Polars also cannot optimize through UDFs.
Any user defined function will therefore often be much, much (up to 10x) slower than pure Polars
Source: https://en.wikipedia.org/wiki/File:GIL_description.gif
Polars: Cloud
Polars is actually focused on speed, not scalability. Using Polars alone you will eventually be limited by the amount of memory you have.
Polars Cloud, however, focuses on scalability.
For homework 1 we will be doing all local, in-memory processing without using much of the theory that we are learning in lecture. That will change quickly, however.
How to write fast, memory, efficient Polars code
DOs
DON’Ts
Lambda vs Regular Functions
Lambda Functions
Regular Function
Cache/Persistence
Reference - Python Polars: The Definitive Guide, Chapter 5 section "Caching Intermittent Results”
FAQs/Helpful Debugging Instructions
�
Appendix
More Polars resources
Narrow vs Wide Transformations
Narrow vs Wide Transformations
Group By Key Example