1 of 27

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.

2 of 27

Agenda

1. Brief Introduction to Google Colab

2. Polars Basics

  • Background and history
  • Essential API functions

3. Polars Essentials

  • LazyFrames
  • Streaming
  • Caching

4. Polars Limitations

  • Memory intensive operations
  • Mitigation methods
  • User defined functions (UDFs)

3. Appendix

4. Open Q&A

​

​

​

​

3 of 27

Homework Setup

Programming Assignments

  • 5 programming assignments using Polars and PyTorch
  • Polars assignments can be completed on Google Colab or on your local computer.
  • More details about the PyTorch assignments will be shared later.
  • For autograding, submit assignments on Gradescope.
  • Detailed instructions on how to submit assignment for grading will be in the writeup.

Written Assignments

  • Need to be directly submitted to Gradescope.

​

More info on Course Website - https://10605.github.io/

​

​

​

​

​

4 of 27

To-Dos for students

  • Download the assignment and (optionally) import the Jupyter Notebooks on Google Colab.
  • Configure the environment according to the instructions in the writeup.
  • After all local tests pass, hand-in the assignment on Gradescope for grading.
    • Please note that you get points if the tests on the grader pass.
    • The local tests exists to help you develop and test your code.

​

5 of 27

Registration and Uploading Notebooks

  1. Go to https://colab.research.google.com and login with your Google account.
  2. Click: Upload > Browse. Then upload the notebook on your local computer, which was downloaded from the course website or Piazza.

​

6 of 27

Exporting the Homework

​

  • After completion of assignment, export the notebook as an iPython Notebook (.ipynb file) and submit to Gradescope for grading.
  • Click: File > Download > Download .ipynb

7 of 27

Polars: Background and History

  • Released in 2020, originally intended as a Rust-rewrite of Pandas.
  • Motivated by a frustrated with the Python GIL (which means that typically only a single processor core is used when running a Python program like Pandas)
  • Up to 100x faster than Pandas on some benchmarks

​

8 of 27

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:

​

  • .with_column – adds a column from an expression (such as addition, string operation, list operation, etc.)
  • .select – similar to with_column, but allows a manipulation on existing column without adding a new column
  • .group_by().agg – aggregate data (for example, to compute a mean)
  • .join() – combine two dataframes
  • .pivot() – use a row as new comun names

9 of 27

Polars: Lazy vs Eager Evaluation

​

DataFrame (Eager evaluation)

  • Returns result of computation immediately
  • Matches behavior of Python operators you are familiar with
  • Requires all data to be held in memory before computation
  • Great for prototyping and developing
  • No query optimization

LazyFrame (Lazy Evaluation)

  • Only returns the result once it has been collected
  • Does not behave like other Python operators
  • Allows for use of streaming engine for processing of datasets larger than RAM
  • Best for mature data pipelines and high-performance code
  • Polars will optimize your queries for you

10 of 27

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

11 of 27

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 │

└─────┴──────┘

​

​

12 of 27

Polars: Model of a Group By

13 of 27

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 │

└─────┴──────┘

​

​

14 of 27

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 │

└──────────────┴─────┴──────────────┘

15 of 27

Polars: Model of a Join

16 of 27

Polars: Model of “map_batches(. , streamable=True)”

17 of 27

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

18 of 27

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.

19 of 27

How to write fast, memory, efficient Polars code

DOs

​

  • Use LazyFrames, the streaming engine, and “scan_*” / “sink_*” in order to allow for larger than memory computation
  • Avoid unnecessary .group_by and .join by using operations like the .list expression API
  • Use .map_batches(*, streamable=”True”) if applicable to limit the size of intermediate structures in memory

​

DON’Ts

​

  • Write code that will instantiate a massive intermediate structure in memory
  • Abuse UDFs like .map_elements – these run mostly on a single thread and cannot be optimized
  • Use DataFrames or vanilla .collect() (always prefer .collect(engine=”streaming) )

20 of 27

Lambda vs Regular Functions

Lambda Functions

  • Evaluates only a single expression
  • No name associated

​

​

Regular Function

  • Can have multiple expressions
  • Must have name associated

​

​

21 of 27

Cache/Persistence

  • By default, LazyFrames are recomputed each time you run an action on them. This can be expensive (in time) if you need to use a dataset more than once.
  • You can control what will be cached in memory using .collect(engine=”streaming”).
  • One of the most common performance bottlenecks arises from unknowingly re-evaluation several transformations.�This will be tested in some of the assignments.

​

​

​

​

Reference - Python Polars: The Definitive Guide, Chapter 5 section "Caching Intermittent Results”

​

22 of 27

FAQs/Helpful Debugging Instructions

  • In case tests don’t pass, try running all the cells in the notebook.
  • We recommend developing your code on a small data subset using eager mode, and then transitioning to lazy mode for the whole dataset.
  • If you see a name-not-defined error, It could be because of your code timing out on the grader.
  • Timeouts can occur due to inefficient code (for example, calling collect() on the entire dataset).
  • Local tests are not comprehensive. They are there to assist you in building your code. You only get points if the assignment runs on the autograder.

�

23 of 27

Appendix

24 of 27

More Polars resources

25 of 27

Narrow vs Wide Transformations

​

  • Each partition of the parentRDD is used by at most one partition of the childRDD.
  • Fast, No Data Shuffling

​

​

​

  • Multiple childRDD partitions may depend on a single parent RDD partition.
  • Slow, Data Shuffling Required

26 of 27

Narrow vs Wide Transformations

27 of 27

Group By Key Example