03 Text Classification - Chunk 01 (s.1-30)
Overview
This chunk opens the Text Classification lecture (slides adapted from Zhao Rui & Chen Zhenghua of NTU and Josh Starmer of StatQuest) (s.1). It recaps document representation (Document-Term Matrix, TF, TF-IDF, LSA, NMF, LDA) and announces the move to classification, starting with Naive Bayes (s.2). A “Pre Study” block (s.3-12) then revises the probability needed: Concept - Probability Basics, Concept - Conditional Probability, Concept - Density Estimation, Concept - Argmax and Concept - Maximum Likelihood Estimation (with a Gaussian example). The “Bayes Theorem” block (s.13-24) introduces Concept - Bayes Rule, the evolution of posteriors, Concept - Maximum A Posteriori and MAP vs MLE, classification with Bayes rule and the Concept - Curse of Dimensionality, then the Concept - Conditional Independence assumption that defines Concept - Naive Bayes. The final block (s.25-30) applies Naive Bayes to text: class priors (Concept - Prior Probability), MLE parameter estimation with a “mega-document”, the new-word (zero-probability) problem and Concept - Additive Smoothing.
Detailed notes
1. Title and introduction (s.1-2)
- Title: Text Classification; slides adapted from Zhao Rui & Chen Zhenghua (NTU) and Josh Starmer (StatQuest) (s.1).
- Introduction (s.2):
- Document representation: methods “we already discussed”: Document-Term Matrix; TF, TF-IDF, etc.; Latent Semantic Analysis (LSA).
- Other methods: Non-negative Matrix Factorization (NMF); Latent Dirichlet Allocation (LDA); deep-learning-based models (to be discussed later).
- “Now, we’ll talk about classification”: you already know some classifiers such as KNN from other courses, so the lecture focuses on “more involved algorithms starting with Naive Bayes”.
2. Pre Study (s.3-12)
- s.3 is a section divider “Pre Study”.
- s.4: a tweet by Josh Wills (3 May 2012): “Data Scientist (n.): Person who is better at statistics than any software engineer and better at software engineering than any statistician.” Motivates why the statistics background is needed (no further explanation on slide).
Basics of probability (s.5)
- : probability that happens. : probability that happens given that happens (conditional probability).
- Rules (see Formulas): complement, disjunction, conjunction, independence, total probability.
Conditional probability (s.6)
- Example: If the patient has the symptom of toothache, then conclude cavity with probability , where .
- To compute it: (as written on the slide).
Added context
The slide divides by ; the standard definition of divides by , i.e. by here. This is flagged as a possible slide typo, but it is reproduced as printed.
Density estimation (s.7)
- Density estimation task: construct an estimate of an unobservable underlying probability density function, based on some observed data.
- Data: sample drawn i.i.d. (independent identically distributed) from set according to some distribution : .
- Problem: find a distribution out of a set that best estimates the true distribution .
Argmax / Argmin (s.8)
- argmax = the argument of the maximum: the set of points of at which the given function attains its maximum value (slide text is cut off at the right edge; “points of the [domain] … which” is the evident wording; UNCLEAR (slide 8) for the exact cut-off word).
- and example .
Maximum-Likelihood Estimation, MLE (s.9-11)
- Likelihood: probability of observing the sample under distribution , which, given the independence assumption, is (s.9).
- MLE principle: select the distribution maximizing the sample probability: (Likelihood) (Log-Likelihood) (s.9).
- s.10: given training data , MLE finds the best hypothesis that maximizes the likelihood of the training data: . Closing question: “What if you have some ideas about your hypothesis/parameters?” (leads to priors / MAP).
- Example: Gaussian distribution (s.11):
- Task: find the most likely Gaussian distribution given a sequence of real-valued observations: 3.23, 1.23, 0.55, 1.23, …
- Normal density (bell-curve figure with mean at the centre and marked as the spread).
- Log-likelihood: .
- Solution (set derivatives to zero): ; .
3. Bayes theorem (s.12-18)
- s.12: section divider “Bayes Theorem”.
- Bayes’ Rule (s.13): by definition of conditional probability, . Terms (colour-coded on the slide):
- : prior probability of hypothesis (blue)
- : posterior probability of given evidence (dark red)
- : likelihood of given (orange)
- : prior probability of evidence
- The rule is labelled “Reverse Probability” next to a portrait of Thomas Bayes (the slide does not caption the portrait; identification as Bayes is implied by the slide title).
- Example: H6751 Student Model (s.14). Given: taking H6751 causes you to stay in the library 80% of the time; prior probability of any student taking H6751 is 1/100; prior probability of any student staying in the library is 1/10. Question: if a student stays in the library, what is the probability they took H6751? Evidence = stay in library; hypothesis = taking H6751. The slide leaves "" blank for the lecture to fill in.
Added context
Applying Bayes’ rule: . This calculation is not shown on the slide.
- Evolution of posterior probabilities (s.15-17): a three-step bar-chart animation, x-axis “hypotheses”, y-axis the probability.
- s.15: , the prior: a low, flat (uniform) bar across all hypotheses.
- s.16: : after seeing one piece of data the mass concentrates on a narrower, taller block.
- s.17: : after more data the distribution is a tall narrow peak on one hypothesis.
- Takeaway: as evidence accumulates, the posterior sharpens around the best hypotheses.
- Maximum A Posteriori (MAP) (s.18): find the most probable hypothesis given the training data: ( is constant over and drops out). The term is boxed in red: “Prior encodes the knowledge/preference”.
- MAP vs MLE (s.19): MLE maximizes the likelihood ; MAP maximizes the posterior . Question posed: “When will MLE and MAP give the same results?” (answer is not on the slide).
Added context
They coincide when the prior is uniform (all hypotheses equally likely), since then is constant in the argmax. Not stated on the slides.
4. Classification with Bayes rule and Naive Bayes (s.20-23)
- Classification using Bayes rule (s.20): the example is (the features, red arrow). Given multiple attribute values, what is the most probable value (class)?
- .
- Problem: too much data needed to estimate when is large: the Curse of Dimensionality.
- Naive Bayes classifier: the idea (s.21): is hard to estimate for high-dimensional . Use the conditional independence assumption: all attributes are conditionally independent; the assumption is often violated in practice, yet even then it usually works well. Successful applications: classification of text documents, diagnosis.
- Conditional independence (s.22): chain rule . The Naive Bayes assumption: attributes are independent given the class: , , …, , hence (highlighted in red).
- Naive Bayes classifier (s.23): based on Bayes’ rule + the assumption of conditional independence: .
5. Text classification using Naive Bayes (s.24-30)
- s.24: section divider “Text Classification using Naive Bayes”.
- Text classification task (s.25): given the text of a newsgroup article, guess which newsgroup it is taken from. Naive Bayes works well on this application. Key issue: how do we represent examples? What are the attributes? Figure: three groups (Group A, Group B, Group C) drawn as stacks of cartoon documents in different colours (purple, white, green) = the candidate newsgroups/classes.
- s.26: Class : binary classification (+/-) or multiple classes possible, (). Then asks: “How about attributes?” (answered next slides: words).
- Example (s.27): 1000 training documents of which someone has classified 700 as “dislikes” () and 300 as “likes” (). Document 1 is “This is a very interesting document”. . Priors: , . Open question: how to estimate ? Each word position is an attribute.
- Parameter estimation (s.28): learning by maximum likelihood estimate: simply count frequencies in the data: . Numerator (red arrow): count of the specific word in the mega-doc; denominator (orange arrow): count of total words in the mega-doc. Procedure: create a mega-document for class by concatenating all the docs in this class; compute the frequency of word in the mega-document.
- New word problem (s.29): what if some words do not exist in a certain category ? Then , and the predicted likelihood is zero: . “How to solve it?” (answer on the next slide).
- Additive smoothing (s.30): , where is the smoothing parameter (red arrow) and the vocabulary size (orange arrow). It is a weighted estimation of the relative frequency and the uniform probability .
Figures & diagrams
- s.1 Plain title slide with credits.
- s.3, s.12, s.24 Blue divider slides: “Pre Study”, “Bayes Theorem”, “Text Classification using Naive Bayes”.
- s.4 Screenshot of Josh Wills’ tweet defining “Data Scientist” (1,626 retweets, 1,294 likes); humorous motivation for statistics + engineering.
- s.5 Formula slide: probability rules (complement, disjunction, conjunction, independence, total probability).
- s.6 Text-formula slide for the cavity/toothache conditional probability.
- s.7 Definition slide with red key terms (Density Estimation, i.i.d., Problem).
- s.8 Formulas for argmax and .
- s.9-10 Likelihood, log-likelihood, formulas.
- s.11 Bell curve with at the centre and marked; Gaussian density, log-likelihood and the closed-form solutions for , .
- s.13 Bayes rule with colour-coded boxes (prior blue, likelihood orange, posterior red) and a portrait labelled “Reverse Probability”.
- s.15-17 Bar charts: prior (flat) -> (block) -> (narrow peak); posterior sharpens with data.
- s.18 MAP derivation with the prior boxed: “Prior encodes the knowledge/preference”.
- s.20 Features vector with arrow “features”; “Curse of Dimensionality” in red.
- s.25 Three groups of cartoon documents (Group A, B, C) = newsgroups as classes.
- s.28 Count formula annotated with arrows (word count in mega-doc / total words in mega-doc).
- s.30 Additive-smoothing formula annotated with arrows (, ).
flowchart TD B["Bayes rule<br/>p(h|D) = p(D|h)p(h)/p(D)"] --> M["MAP: argmax p(D|h)p(h)"] M --> C["Classification: argmax p(h) p(d1..dn|h)"] C --> P["Problem: p(d1..dn|h) needs too much data<br/>(curse of dimensionality)"] P --> I["Conditional independence assumption"] I --> NB["Naive Bayes: argmax p(h) * prod p(dj|h)"] NB --> E["Estimate p(w|h) by counting in mega-doc (MLE)"] E --> Z["Unseen word -> p = 0 -> whole product 0"] Z --> S["Additive smoothing (count+alpha)/(total+alpha*V)"]
flowchart LR L["MLE<br/>argmax P(D|h)"] -- "multiply by prior P(h)" --> A["MAP<br/>argmax P(D|h)P(h)"] A -- "uniform prior" --> L
Key terms
- Concept - Probability Basics
- Concept - Conditional Probability
- Concept - Density Estimation
- Concept - Argmax
- Concept - Maximum Likelihood Estimation
- Concept - Likelihood
- Concept - Prior Probability
- Concept - Bayes Rule
- Concept - Maximum A Posteriori
- Concept - Curse of Dimensionality
- Concept - Conditional Independence
- Concept - Naive Bayes
- Concept - Additive Smoothing
- Earlier-deck concepts referenced on s.2: Concept - TF-IDF, Concept - Bag of Words, Concept - Latent Semantic Analysis (LSA)
Formulas
- Complement: (s.5)
- Disjunction: (s.5)
- Conjunction: (s.5)
- Independence: (s.5)
- Total probability: (s.5; as printed on the slide)
- Likelihood: ; MLE: (s.9)
- (s.10, s.19)
- Gaussian MLE: , (s.11)
- Bayes: (s.13)
- (s.18)
- Naive Bayes: (s.23)
- MLE word probability: (s.28)
- Additive smoothing: (s.30)
Likely exam questions
- State Bayes’ rule and name each term. (s.13) : posterior = likelihood x prior / evidence prior.
- Discuss the difference between MLE and MAP. (s.19) MLE maximizes (likelihood, no prior); MAP maximizes (includes the prior knowledge/preference). They coincide for a uniform prior (added context).
- Explain the Naive Bayes classifier. (s.21-23) Bayes rule + conditional independence of attributes given the class: .
- What is the conditional independence assumption and is it realistic? (s.21-22) Attributes are independent given the class; often violated in practice, but NB usually still works well.
- Why is hard to estimate? (s.20) Needs too much data when is large: curse of dimensionality.
- How are NB parameters estimated for text? (s.28) MLE by counting: build a mega-document per class and take word count / total words in it.
- What is the new-word (zero-frequency) problem and how is it solved? (s.29-30) A word unseen in a class gives probability 0, zeroing the whole product; solved by additive smoothing .
- Explain the roles of and in additive smoothing. (s.30) = smoothing parameter; = vocabulary size; result is a weighted mix of relative frequency and uniform probability .
- Compute priors in the likes/dislikes example. (s.27) , .
- Derive MLE for a Gaussian. (s.11) Maximize the log-likelihood; derivatives give = sample mean, = mean squared deviation.
Common confusions
- Likelihood vs posterior: (likelihood, MLE) is not (posterior, MAP) (s.13, s.19).
- Conditional independence vs independence: NB assumes attributes independent given the class, not unconditionally (s.22).
- Prior vs evidence : both are “prior probabilities” on s.13 but is constant across and drops out of the argmax (s.18).
- Likelihood of document vs class prior: is the class prior estimated from document counts; word probabilities are estimated separately from the mega-document (s.27-28).
- Log-likelihood: taking logs turns the product into a sum without changing the argmax (s.9).
- Smoothing when : reduces to the plain MLE estimate of s.28 (derived from the formula).
Slide coverage
| Slide | Content | Covered in |
|---|---|---|
| 1 | Title, credits (NTU, StatQuest) | Detailed notes 1 |
| 2 | Introduction: document representation, move to classification, NB | Detailed notes 1 |
| 3 | Divider: Pre Study | Detailed notes 2 |
| 4 | Josh Wills data scientist tweet | Detailed notes 2; Figures |
| 5 | Basics of probability | Detailed notes 2; Formulas |
| 6 | Conditional probability (cavity/toothache) | Detailed notes 2 |
| 7 | Density estimation | Detailed notes 2 |
| 8 | Argmax/Argmin | Detailed notes 2 |
| 9 | MLE: likelihood, log-likelihood | Detailed notes 2; Formulas |
| 10 | MLE: hypothesis | Detailed notes 2 |
| 11 | Gaussian MLE example | Detailed notes 2; Figures |
| 12 | Divider: Bayes Theorem | Detailed notes 3 |
| 13 | Bayes’ rule | Detailed notes 3; Figures |
| 14 | H6751 student example | Detailed notes 3 |
| 15 | Posterior evolution: prior | Detailed notes 3; Figures |
| 16 | Posterior evolution: P(h given D1) | Detailed notes 3; Figures |
| 17 | Posterior evolution: P(h given D, D2) | Detailed notes 3; Figures |
| 18 | Maximum A Posteriori | Detailed notes 3 |
| 19 | MAP vs MLE | Detailed notes 3 |
| 20 | Classification using Bayes rule; curse of dimensionality | Detailed notes 4 |
| 21 | Naive Bayes classifier idea | Detailed notes 4 |
| 22 | Conditional independence | Detailed notes 4 |
| 23 | Naive Bayes classifier formula | Detailed notes 4 |
| 24 | Divider: Text classification using NB | Detailed notes 5 |
| 25 | Newsgroup classification task | Detailed notes 5; Figures |
| 26 | Classes and attributes | Detailed notes 5 |
| 27 | Likes/dislikes example | Detailed notes 5 |
| 28 | Parameter estimation (mega-doc) | Detailed notes 5 |
| 29 | New word problem | Detailed notes 5 |
| 30 | Additive smoothing | Detailed notes 5 |
Previous: (none, first chunk) | Next: 03 Text Classification - Chunk 02 (s.31-60) | Up: 03 Text Classification - Index