Introduction To: Information Retrieval
Introduction To: Information Retrieval
Introduction to
Information Retrieval
CS276: Information Retrieval and Web Search
Text Classification 1
Prep work
This lecture presumes that you’ve seen the 124 coursera
lecture on Naïve Bayes, or equivalent
Will refer to NB without describing it
Introduction to Information Retrieval Ch. 13
Standing queries
The path from IR to text classification:
You have an information need to monitor, say:
Unrest in the Niger delta region
You want to rerun an appropriate query periodically to find new news
items on this topic
You will be sent new documents that are found
I.e., it’s not ranking but classification (relevant vs. not relevant)
Such queries are called standing queries
Long used by “information professionals”
A modern mass instantiation is Google Alerts
Standing queries are (hand-written) text classifiers
Introduction to Information Retrieval
3
Introduction to Information Retrieval Ch. 13
Spam filtering
Another text classification task
From: "" <[email protected]>
Subject: real estate is the only way... gem oalvgkay
=================================================
Click Below to order:
http://www.wholesaledaily.com/sales/nmd.htm
Introduction to Information Retrieval Sec. 13.1
Categorization/Classification
Given:
A representation of a document d
Issue: how to represent text documents.
Usually some type of high-dimensional space – bag of words
A fixed set of classes:
C = {c1, c2,…, cJ}
Determine:
The category of d: γ(d) ∈ C, where γ(d) is a classification
function
We want to build classification functions (“classifiers”).
Introduction to Information Retrieval Sec. 13.1
Document Classification
“planning
Test language
Data: proof
intelligence”
A Verity topic
A complex classification rule
Note:
maintenance issues
(author, etc.)
Hand-weighting of
terms
γ(
whimsical and romantic while laughing at the
)=c
conventions of the fairy tale genre. I would
recommend it to just about anyone. I've seen it
several times, and I'm always happy to see it
again whenever I have a friend who hasn't
seen it yet.
Introduction to Information Retrieval
great 2
γ(
love 2
recommend 1 )=c
laugh 1
happy 1
... ...
The Naive Bayes classifier
The Naive Bayes classifier is a probabilistic classifier.
We compute the probability of a document d being in a class c
as follows:
17
Taking the log
Multiplying lots of small probabilities can result in floating
point underflow.
Since log(xy) = log(x) + log(y), we can sum log probabilities
instead of multiplying probabilities.
Since log is a monotonic function, the class with the highest
score does not change.
So what we usually compute in practice is:
18
Naive Bayes classifier
Classification rule:
Simple interpretation:
Each conditional parameter log is a weight that indicates
how good an indicator tk is for c.
The prior log is a weight that indicates the relative
frequency of c.
The sum of log prior and term weights is then a measure of how
much evidence there is for the document being in the class.
We select the class with the most evidence.
1 19
9
Parameter estimation take 1: Maximum likelihood
20
The problem with maximum likelihood estimates: Zeros
21
The problem with maximum likelihood estimates: Zeros (cont)
22
To avoid zeros: Add-one smoothing
Before:
B is the number of different words (in this case the size of the
vocabulary: |V | = M)
23
To avoid zeros: Add-one smoothing
24
Naive Bayes: Training
25
Naive Bayes: Testing
26
Exercise
27
Example: Parameter estimates
28
Example: Classification
29
Time complexity of Naive Bayes
Lave: average length of a training doc, La: length of the test doc, Ma:
number of distinct terms in the test doc, training set, V : vocabulary,
set of classes
is the time it takes to compute all counts.
is the time it takes to compute the parameters from the
counts.
Generally:
Test time is also linear (in the length of the test document).
Thus: Naive Bayes is linear in the size of the training set (training) and
the test document (testing). This is optimal.
30
Introduction to Information Retrieval
Features
Supervised learning classifiers can use any sort of
feature
URL, email address, punctuation, capitalization,
dictionaries, network features
In the bag of words view of documents
We use only word features
we use all of the words in the text (not a subset)
Introduction to Information Retrieval Sec.13.5
Evaluating Categorization
Evaluation must be done on test data that are
independent of the training data
Sometimes use cross-validation (averaging results over
multiple training and test splits of the overall data)
Easy to get good performance on a test set that was
available to the learner during training (e.g., just
memorize the test set)
Introduction to Information Retrieval Sec.13.6
Evaluating Categorization
Measures: precision, recall, F1, classification
accuracy
Classification accuracy: r/n where n is the
total number of test docs and r is the number
of test docs correctly classified
Micro- vs. Macro-Averaging
If we have more than one class, how do we combine
multiple performance measures into one quantity?
Macroaveraging: Compute performance for each
class, then average.
Microaveraging: Collect decisions for all classes,
compute contingency table, evaluate.
3
6
Micro- vs. Macro-Averaging: Example
3
7
Introduction to Information Retrieval Sec.13.6
Results
Introduction to Information Retrieval
Introduction to Information Retrieval
SpamAssassin
Naïve Bayes has found a home in spam
filtering
Paul Graham’s A Plan for Spam
Widely used in spam filters
But many features beyond words:
black hole lists, etc.
particular hand-crafted text patterns
Introduction to Information Retrieval
SpamAssassin Features:
Basic (Naïve) Bayes spam probability
Mentions: Generic Viagra
Regex: millions of (dollar) ((dollar) NN,NNN,NNN.NN)
Phrase: impress ... girl
Phrase: ‘Prestigious Non-Accredited Universities’
From: starts with many numbers
Subject is all capitals
HTML has a low ratio of text to image area
Relay in RBL, http://www.mail-abuse.com/enduserinfo_rbl.html
RCVD line looks faked
http://spamassassin.apache.org/tests_3_3_x.html
Introduction to Information Retrieval
Government
Science
Arts
45
Sec.14.1
Government
Science
Arts
46
Sec.14.1
Is this
similarity
hypothesis
true in
general?
Government
Science
Arts
Definition of centroid
1
m (c) = å v (d)
| Dc | d Î Dc
48
Sec.14.2
Rocchio classification
Rocchio forms a simple representative for
each class: the centroid/prototype
Classification: nearest prototype/centroid
It does not guarantee that classifications are
consistent with the given training data
49
Sec.14.2
Rocchio classification
Little used outside text classification
It has been used quite effectively for text
classification
But in general worse than Naïve Bayes
Again, cheap to train and test documents
50
Sec.14.3
To classify a document d:
Define k-neighborhood as the k nearest
neighbors of d
Pick the majority class label in the k-
neighborhood
51
Sec.14.3
P(science| )?
Government
Science
Arts
52
Sec.14.3
Nearest-Neighbor Learning
Learning: just store the labeled training examples D
Testing instance x (under 1NN):
Compute similarity between x and all examples in D.
Assign x the category of the most similar example in D.
Does not compute anything beyond storing the examples
Also called:
Case-based learning
Memory-based learning
Lazy learning
Rationale of kNN: contiguity hypothesis
53
Sec.14.3
k Nearest Neighbor
Using only the closest example (1NN)
subject to errors due to:
A single atypical example.
Noise (i.e., an error) in the category label of
a single training example.
More robust: find the k examples and
return the majority category of these k
k is typically odd to avoid ties; 3 and 5
are most common
54
Sec.14.3
Government
Science
Arts
56
3 Nearest Neighbor vs. Rocchio
Nearest Neighbor tends to handle polymorphic
categories better than Rocchio/NB.
57
Sec.14.3
kNN: Discussion
No feature selection necessary
No training necessary
Scales well with large number of classes
Don’t need to train n classifiers for n classes
Classes can influence each other
Small changes to one class can have ripple effect
May be expensive at test time
In most cases it’s more accurate than NB or Rocchio
58
Let’s test our intuition
Can a bag of words always be viewed as a vector
space?
What about a bag of features?
Can we always view a standing query as a region in a
vector space?
What about Boolean queries on terms?
What do “rectangles” equate to?
5
9
Sec.14.6
61
Sec.14.6
62
Summary: Representation of
Text Categorization Attributes
Representations of text are usually very
high dimensional
High-bias algorithms that prevent
overfitting should generally work best in
high-dimensional space
For most text categorization tasks, there
are many relevant features and many
irrelevant ones
63
Which classifier do I use for a given
text classification problem?
Is there a learning method that is optimal for all text
classification problems?
No, because there is a tradeoff between bias and
variance.
Factors to take into account:
How much training data is available?
How simple/complex is the problem? (linear vs. nonlinear
decision boundary)
How noisy is the data?
How stable is the problem over time?
For an unstable problem, its better to use a simple and robust
classifier. 64