Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

Naive Bayes Classifier

Also Naive Bayes · Multinomial Naive Bayes

A probabilistic text classifier that predicts the class c maximizing p_c times the product of the word probabilities p_wc over the words of the document, assuming that the words are independent given the class.

Formula#

Derivation. By Bayes’ Theorem, Pr⁡(c∣d)=Pr⁡(d∣c) Pr⁡(c)/Pr⁡(d)\Pr(c \mid d) = \Pr(d \mid c)\,\Pr(c) / \Pr(d). The denominator Pr⁡(d)\Pr(d) is the same for all classes and drops out of the arg⁡max⁡\arg\max. The naive assumption is that the words of dd are independent given the class, so Pr⁡(d∣c)=∏w∈dPr⁡(w∣c)\Pr(d \mid c) = \prod_{w \in d} \Pr(w \mid c) (conditional independence). Together:

Pr⁡(c∣d)  ∝  pc⋅∏w∈dpwc.\Pr(c \mid d) \;\propto\; p_c \cdot \prod_{w \in d} p_{wc}.

Training (learning step) on labelled documents, by relative frequencies:

pc=#training documents of class c#training documents,pwc=nwcnc,p_c = \frac{\#\text{training documents of class } c}{\#\text{training documents}}, \qquad p_{wc} = \frac{n_{wc}}{n_c},

where nwcn_{wc} is the number of occurrences of ww in all training documents of class cc and nc=∑w∈Vnwcn_c = \sum_{w \in V} n_{wc} is the total number of word occurrences in class cc.

Prediction in log space. log⁡\log is strictly increasing, so

c^(d)=arg⁡max⁡c  (log⁡pc+∑w∈Vnw(d)⋅log⁡pwc).\hat c(d) = \arg\max_{c}\; \Big(\log p_c + \sum_{w \in V} n_w(d) \cdot \log p_{wc}\Big).

With the matrix M∈R∣C∣×(∣V∣+1)M \in \mathbb{R}^{|C| \times (|V|+1)} whose row cc is (log⁡pw1c,…,log⁡pw∣V∣c,log⁡pc)(\log p_{w_1 c}, \dots, \log p_{w_{|V|} c}, \log p_c) and the document vector x=(nw1(d),…,nw∣V∣(d),1)x = (n_{w_1}(d), \dots, n_{w_{|V|}}(d), 1), all class scores are one matrix-vector product M⋅xM \cdot x, and c^(d)=arg⁡max⁡(M⋅x)\hat c(d) = \arg\max(M \cdot x) (exam WS 19/20, Task 4.3).

Properties#

  • Zero probabilities. If a word ww of dd never occurs in the training documents of class cc, then pwc=0p_{wc} = 0 and the score of cc is 00, whatever the other words say. If every class has such a word, all scores are 00. Smoothing fixes this, and it is also needed for the log-space version (log⁡0\log 0 is undefined).
  • Naive Bayes is a Linear Classifier. For two classes X,YX, Y it predicts XX iff ∑w∈Vnw(d) (log⁡pwX−log⁡pwY)  >  log⁡pY−log⁡pX,\sum_{w \in V} n_w(d)\,\big(\log p_{wX} - \log p_{wY}\big) \;>\; \log p_Y - \log p_X, i.e. iff w⋅x>bw \cdot x > b with x=(nw(d))w∈Vx = (n_w(d))_{w \in V}, weights wi=log⁡(pwiX/pwiY)w_i = \log(p_{w_i X} / p_{w_i Y}) and threshold b=log⁡(pY/pX)b = \log(p_Y / p_X). The decision boundary is a hyperplane in the space of word-count vectors.
  • Document length and the prior. Repeating a document kk times raises the word part of the score to the kk-th power but leaves pcp_c unchanged: sc(dk)=pc⋅(∏w∈dpwc)ks_c(d^k) = p_c \cdot \big(\prod_{w \in d} p_{wc}\big)^k. With equal priors the prediction is invariant under repetition; with unequal priors the word evidence outweighs the prior for long enough documents, so the prediction can change (exam WS 17/18, Task 4.2–4.3).
  • Rounding. A product of many probabilities <1< 1 underflows to 00 in floating point. The log-space sum avoids this.
  • Ties must be broken by a fixed rule (e.g. by the larger class id, exam WS 17/18, Task 4).

Example#

Sources#

  • Not covered by a WS 25/26 lecture. Source: the former Information Retrieval exams.
  • Exam WS 17/18, Task 4 (not published) (prediction with for-loops, document repetition)
  • Exam WS 16/17, Task 4.3–4.4 (not published) (Naive Bayes as a linear classifier)
  • Exam WS 19/20, Task 4.2–4.3 (not published) (smoothing, prediction as a matrix-vector product)
  • Exam WS 21/22, Task 4.3 (not published) (training on the numbers 0..15)
  • Exam WS 22/23, Task 4.1 (not published) (predict returning class and probability)

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only