Module 19 · Big data with Spark · Lesson 62 of 66

Big data and lambda expressions

What changes when data doesn't fit on one computer, and the small functions Spark is built on.

In this lesson · about 15 minutes

  • Explain local versus distributed processing, Hadoop and Spark
  • Write lambda expressions
  • Use map, filter and reduce on plain Python lists

Everything so far ran on one machine, with the data in memory. That works up to a few gigabytes. Beyond that, a laptop runs out of memory, and the work has to be distributed across many computers, a cluster.

  • Hadoop was the first popular system for this: it stores huge files across machines (HDFS) and processes them with MapReduce, writing to disk between steps.
  • Spark does the same kind of work but keeps data in memory, which can make it up to 100 times faster. It runs on Hadoop clusters, in the cloud (Databricks, Amazon EMR) or on a laptop for practice.

Spark's original core is the RDD (resilient distributed dataset): a collection split into pieces across the cluster. You work with it by passing small functions to methods like map and filter, so first, the tool for writing small functions.

Lambda expressions

A lambda is a function written in one line, without a name: lambda num: num ** 2 does the same as a def that returns num ** 2. Lambdas are handy when a function is only needed once, as an argument to another function.

Exercise

Write some lambdas

Write three lambdas: cube returns its number cubed; is_long returns True if a string has more than 5 characters; last returns the last item of a list or string.

Exercise

map, filter and reduce

Given nums = list(range(1, 11)): make squares, the list of each number squared, with map; evens, the even numbers, with filter; and total, the sum of all the numbers, with reduce from functools. Use lambdas for all three.

Code that runs before yours
nums = list(range(1, 11))

Exercises solved: 0 of 2

Tip: solve each exercise first. Press Check to test your code.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.