10-405/605/805�ML FROM LARGE DATASETS
Outline
MAP-REDUCE REVIEW
4
2004
of crappy machines
≅ “corpus transformation”
≅ “something you do after a sort”
infrastructure to write your own scalable code (e.g., monitoring jobs, robustness against failing machines/disks, etc)
5
KenChurch 1994 | Jeff & Sanjay 2004 |
Word count | Distributed word count |
Bigram count | |
Mutual information for phrases | |
… | |
Grep | Distributed grep |
Concordance / inverted index | Distributed inverted index |
Sorting by alphabetical, rhyming, … | Distributed sort |
pipelines of Unix™ tools to “transform” large data
+ sort
sequences of map-reduce steps
WordCount in MapReduce
6
bash-3.2$ head -1 ../data/brown_nolines.txt
The Fulton County Grand Jury said Friday an investigation of Atlanta's recent primary election produced "no evidence" that any irregularities took place.
bash-3.2$ py -m fire wc_nano.py WordCount \ map_reduce
--src ../data/brown_nolines.txt --dst wc.txt
bash-3.2$ head -5 wc.txt
('the', 69969)
('fulton', 17)
('county', 160)
('grand', 52)
('jury', 68)
wc_nano.py
hz_nano.py
Why MapReduce Scales on Cheap Machines
7
bash-3.2$ head -1 ../data/brown_nolines.txt
The Fulton County Grand Jury said Friday an investigation of Atlanta's recent primary election produced "no evidence" that any irregularities took place.
bash-3.2$ py -m fire wc_nano.py WordCount \ map_reduce
--src ../data/brown_nolines.txt --dst wc.txt
bash-3.2$ head -5 wc.txt
('the', 69969)
('fulton', 17)
('county', 160)
('grand', 52)
('jury', 68)
Better:
Why does sorting words help?
Why does sorting words help?
https://www.youtube.com/watch?v=q6ChwZTjfXE – thx Tom Murphy VII
Constant-Memory Word Count in Map-Reduce
10
Constant-Memory Word Count in Map-Reduce
11
itertools.groupby
all lazy!
Map-Reduce Can Be�Constant Memory�and Parallel
Map-Reduce in Parallel
13
Counting logic
Mapper Machine 1
Partition/Sort
Spill 1
Spill 2
Spill 3
…
Counting logic
Mapper Machine 2
Partition/Sort
Spill 1
Spill 2
Spill 3
…
Logic to combine counter updates
Reducer Machine 1
Merge Spill Files
combine counter updates
Reducer Machine 2
Merge Spill Files
Spill n
Key ideas in distributed map reduce
15
Key ideas in distributed map reduce
16
Key ideas in distributed map reduce
17
18
2004
of crappy machines
≅ “corpus transformation”
≅ “something you do after a sort”
Takeaways for MapReduce
Outline
SPARK AND POLARS
Spark: A Dataflow Language for MapReduce
22
Classic PySpark examples: wordcount
23
Spark examples: wordcount
24
Classic spark examples: batch logistic regression
25
reduce is an action – it produces a numpy vector
p.x and w are vectors, from the numpy package
p.x and w are vectors, from the numpy package. Python overloads operations like * and + for vectors.
Spark examples: batch logistic regression
26
Important note: p.x, w are stored in memory as numpy vectors/matrices. Numpu is not just “syntactic sugar”.
Spark examples: batch logistic regression
27
dataset of points is cached in cluster memory to reduce i/o
we don’t need to scan the data on disk in each iteration!
Spark examples: batch logistic regression
28
gradient computation is done in parallel, locally on each worker, using data kept in memory.
summing the gradient vectors is done in parallel, and intermediate sums are set across the network
updating the weights is done on the local machine
The lambda function, including the current value of w, is broadcast to the workers
Spark logistic regression example
29
Takeaways for Spark
30
What is Polars?
31
Polars vs Spark
32
filter() is a transformation
count() is an action
Recap: Spark has transformations and actions
Polars has LazyFrames:
a LazyFrame
a LazyFrame
a LazyFrame
33
Polars has LazyFrames:
a LazyFrame
a LazyFrame
a LazyFrame
Polars also has eager DataFrames
a DataFrame
a DataFrame
a DataFrame
pl.read_csv
34
Polars also has eager DataFrames
a DataFrame
a DataFrame
a DataFrame
pl.read_csv
eager pl.DataFrames are kept in memory and operations are performed immediately
lazy pl.LazyFrames are plans, usually based on scanning from disk
LazyFrames Are Query Plans�That Can be Optimized
35
convert to lowercase then filter
filter then convert to lowercase
Parallelism in Polars
36
c1 | c2 | … | | | | | | | cN |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
… | … | … | .. | … | … | … | … | … | … |
| | | | | | | | | |
Row Group 1
Row Group 2
Row Group 3
1 |
2 |
3 |
4 |
5 |
2 |
4 |
6 |
8 |
10 |
1 Mb
Not row-by-row or column-by-column: chunks are one column in a group of rows
Vector processing by chunk
Pros and Cons of Polars
37
Outline
SAMPLE WORKFLOW:�COMPARING COUNTS IN TWO CORPORA
Two corpora: conservative political community, liberal community (2010)�Goal: find words most characteristic of each community
Naïve Bayes:
One corpus is “foreground” and one is “background”
PySpark code
Same Method for Comparing Bigrams
Problem: many of the common bigrams don’t seem to have meaning as a phrase….
Scoring bigrams as phrases
| explanation |
word_v | #distinct words |
phrase_v | #distinct phrases |
word_n | #words in corpus |
phrase_n | #phrases in corpus |
xy | phrase |
x | first word |
y | second word |
As a phrase “x y”
As a random co-occurrence of x and y
unix™ poetry example
Comparing Phrases
Filtered for high “phrasiness”
Comparing bigrams and compute filter by phrasiness
In Spark Polars
Warmup: wordcount
Polars subroutines-in-chains
# drop first token from line
# tokens[0:2], tokens[2:4], …
# list of all bigrams
Similar for bigrams
Computing ‘phrasiness’
Computing ‘phrasiness’
Sorting Phrases by Foreground/Background and Filtering by Phraseness
Outline
AN ITERATIVE WORKFLOW:�K-MEANS
Background: Expectation-Maximization
59
Expectation-Maximization
60
EM is a kind of coordinate ascent:
Expectation-Maximization
61
In EM there is typically
Then do coordinate descent on w, Z to maximize
–log Pr(X| w, Z)
An example from Ron Bekkerman�k-means in Map-Reduce
62
Example: k-means clustering
63
k-means Clustering
64
centroids
k-means in Polars
65
k-means in Polars
Spherical k-means in Polars
docs are short, vocab is large
Aside: Are Two Entity Names the Same?
Possible solutions:
Aside: Are Two Entity Names the Same?
k-means in Polars
70
assign doc to cluster by similarity
docs and clusters are vectors: dimension is #words, each components is a weight
O(corpus): the big one
O(vocab * k)
O(#docs)
71
Initialize Centroids
72
each cluster has just one document
73
Assign docs to closest centroid
74
75
Recompute centroids�
76
Recompute centroids: alternative
77
????
Recompute centroids: alternative
78
create groups with same ‘cluster_id’ and compute the L2 norm of the cluster
'broadcast’ the computed cluster norm to every row with the same ‘cluster_id’
divide w by the cluster norm
Recompute centroids: alternative
79
I don’t like “over” because it hides a group_by and group_by / join operations can be expensive
80
81
Can you be too lazy?
82
assignmentt = f(centroidst-1)
centroidst = g(assignmentt) =
g(f(centroidst-1))
centroidsn =
g(f(g(f(…)))
This can lead to big problems:
If assignment_df doesn’t fit in memory consider caching the computation:
CLUSTER-PARALLEL K-MEANS
83
Parallelizing k-means
84
Parallelizing k-means
85
Parallelizing k-means
86
k-means on MapReduce
87