1
Applied Data Analysis (CS401)
Lectures 10+11
Handling text data
22 Nov 2023
Robert West
Looking back at ADA so far…
2
Announcements
3
4
Give us feedback on this lecture here: https://go.epfl.ch/ada2023-lec10-feedback
Textual data
5
Nov 2022: the dawn of a new era
6
Outline
7
Typical task 1: document retrieval
8
Document retrieval
9
Typical task 2: document classification
10
Document classification
11
Typical task 3: sentiment analysis
12
Sentiment analysis
13
Typical task 4: topic detection
14
Topic detection
15
Feature vectors
Positions don’t correspond to
meaningful features
16
Feature vectors
17
Bag of words
18
Tom Mitchell (CMU)
An extra reason for sparsity: Zipf’s law
The probability of observing a word scales inversely�with its frequency rank:�p(wi) ∝ 1/i (where wi is the i-th most frequent word)
A famous power law
19
On what axes would you need to draw this plot in order to make it look like a straight line?
Bag-of-words matrix
20
words
docs
… or are you really?
21
Bag of tricks for bags of words
22
Character encoding
23
Language identification
24
25
Commercial break
Looking for friends?
Tokenization
26
Tokenization
27
Stopword removal
28
Don’t throw out the baby with the bathwater!
29
Word normalization: casefolding
30
Word normalization: Stemming
31
Word normalization: Lemmatization
32
Social media
33
A real tweet:
“ikr smh he asked fir yo last name so he can add u on fb lololol”
Tokens vs. n-grams
34
Tokens vs. n-grams
35
Postprocessing the BOW matrix
36
words
docs
Inverse document frequency
37
Inverse document frequency
38
TF-IDF matrix
39
words
docs
Row normalization of TF-IDF matrix
40
Column normalization
41
Bag of tricks for bags of words
Stay tuned!
Next week: Part 2
42
43
Applied Data Analysis (CS401)
Lectures 10+11
Handling text data
22 Nov 2023
Robert West
Announcements
44
Recap
45
Let me open my bag of tricks for bags of words for you! But only if you were good children...
words
docs
Reminder: bag-of-words matrix
Revisiting the 4 typical tasks
46
words
docs
Typical task 1: document retrieval
47
words
docs
Typical task 1: document retrieval
48
Typical task 2: document classification
49
words
docs
Typical task 3: sentiment analysis
50
words
docs
Regularization
51
x
x
minimize
Regularization
52
Which curve resulted from adding a regularization term to the loss function?
Typical task 4: topic detection
53
words
docs
Typical task 4: topic detection
54
TF-IDF
≈
A
B
words
docs �
docs
“topics”
“topics”
words
Typical task 4: topic detection
55
Typical task 4: topic detection
You already know how to efficiently
compute this, from your linear algebra
class: singular-value decomposition (SVD)
56
Typical task 4: topic detection
57
58
Commercial break
LDA: probabilistic topic modeling
59
60
Topic inference in LDA
61
Question:
62
TF-IDF
words
docs
Sparsity in TF-IDF matrix
63
“Word vectors”
64
words
contexts
M
“Word vectors”
65
words
contexts
M
Beyond bags of words
66
From words to texts
67
Contextualized word vectors
68
BERT in a nutshell
context-�ualized word vectors
doc vector
Inside the black box: some nasty neural network
69
<START>
my
ass
likes
carrots
[1.00,0.70,0.90,0.50,0.06,…]
[0.54,0.75,0.56,0.45,0.09,…]
[0.44,0.76,0.77,0.31,0.82,…]
[0.91,0.62,0.53,0.75,0.74,…]
[0.92,0.37,0.25,0.49,0.24,…]
[0.85,0.62,0.71,0.11,0.58,…]
[0.49,0.25,0.22,0.36,0.75,…]
[0.61,0.87,0.73,0.96,0.52,…]
[0.58,0.02,0.01,0.92,0.76,…]
[0.35,0.72,0.64,0.26,0.49,…]
[0.53,0.42,0.64,0.26,0.01,…]
<START>
he
’s
such
an
ass
NLP pipeline
70
NLP pipeline
71
Today’s trend: generative language models
72
73
Give us feedback on this lecture here: https://go.epfl.ch/ada2023-lec10-feedback