Skip to content

naive Bayes

learning

Level 2 of 3

Also called the naive Bayes classifier and simple Bayes.

test yourself

Six questions, four to pick from and two for paper. A point such as 2 8 t is a triangle at 2 across, 8 up (s is a square, c a circle). Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Points 2 8 t, 7 8 s, 6 2 c, 3 2 t and 8 5 s, and a new point at 5 across, 4 up. The smoothing count is 1. How many points does naive Bayes count?

024681002468100-10x 0-3x 4-6x 7-10y 0-3y 4-6y 7-10tsc000000000000000000000
the answer, and why

the answer is C

One for each of the 5 points, each added once to its class's row.

if you chose another

  • That counts the classes, each scored once after the counting.
  • That counts each point twice, once for each band; it is one point read once.
  • That is the smoothing count; it is added to counts, never counted.

question 2

On the same points, what do the squares score?

024681002468100-10x 0-3x 4-6x 7-10y 0-3y 4-6y 7-10tsc000000000000000000000
the answer, and why

the answer is C

The squares at 7 8 and 8 5 are 2 of 5. Neither is in band 4 to 6 across, so 0 + 1 over 2 + 3. One is in band 4 to 6 up, so 1 + 1 over 5. 2/5 × 1/5 × 2/5 = 0.032.

if you chose another

  • That is the triangles' score, 2/5 × 1/5 × 1/5.
  • That is the circle's score, 1/5 × 2/4 × 1/4.
  • That is the squares' share of the points alone, 2 of 5.

question 3

Five triangles, 1 9 t, 8 9 t, 9 7 t, 9 2 t and 8 1 t, and one square at 5 8. A new point at 4 across, 9 up, with no smoothing. What do the triangles score?

024681002468100-10x 0-3x 4-6x 7-10y 0-3y 4-6y 7-10ts00000000000000
the answer, and why

the answer is C

No triangle stands in band 4 to 6 across, so one share is 0 of 5, and 0 times anything is 0.

if you chose another

  • That is their score with a smoothing count of 1.
  • That is their share in band 7 to 10 up alone, 3 of 5.
  • That is their share of all the points alone, 5 of 6.

question 4

Which is true of naive Bayes on n points?

the answer, and why

the answer is A

Every point is added once to its class's row, so the count depends on n alone.

this tests best, worst and average, on the learn page

if you chose another

  • The smoothing count is added to counts already made; it counts nothing.
  • Where the points stand changes nothing; each is counted once.
  • Every point belongs to some class, so every point is counted.

pen and paper

question 5about 12 minutes

Six points, 2 8 t, 7 8 s, 6 2 c, 3 2 t, 8 5 s and 1 1 t. A new point at 5 across, 4 up, with a smoothing count of 1.

  1. Count each class's points, and its points in the new point's band across and band up.
  2. Score each class, and say what the new point becomes.
  3. Count the points counted, and check them against nn.
a hint

The new point is in band 4 to 6 across and band 4 to 6 up. Add 1 to each band count, and 3 to the bottom of each band share. A class's share of all 6 points is not smoothed.

a worked answer

Three triangles, none in either band: 3/6 × 1/6 × 1/6 = 0.014. Two squares, none across and the one at 8 5 up: 2/6 × 1/5 × 2/5 = 0.027. One circle, at 6 2, across but not up: 1/6 × 2/4 × 1/4 = 0.021. The squares score highest, so the new point becomes a square. The run has 6 points counted, and nn is 6.

Why does naive Bayes count exactly nn points, on every set of points?
  1. the move: see the counting

    the line:
    each point goes onceinto its class’s row\begin{gathered} \text{each point goes once} \\ \text{into its class's row} \end{gathered}
    pointclassband acrossband up
    2 8triangle0 to 37 to 10
    7 8square7 to 107 to 10
    6 2circle4 to 60 to 3
    3 2triangle0 to 30 to 3
    Four points, each read once, its class and its two bands. Four counts.

    why it is allowed: The run reads every point in turn, once, and adds 1 to its class's points and to its two bands. The table shows four.

  2. the move: one for each point

    the line:
    C=1+1+⋯+1C = 1 + 1 + \dots + 1

    why it is allowed: CC is the points counted. By line 1, one for each of the nn points.

  3. the move: add the ones

    the line:
    C=nC = n

    why it is allowed: By line 2, nn ones add up to nn. For the four points, 4.

  4. the move: any set, any smoothing

    the line:
    the same for every setand every smoothing count\begin{gathered} \text{the same for every set} \\ \text{and every smoothing count} \end{gathered}

    why it is allowed: In line 1, how many points are counted never depends on where they stand or on the smoothing count, only on their being there.

  5. the move: a ceiling

    the line:
    C≤nC \le n

    why it is allowed: By line 3, CC is nn, so it is at most nn. Bounds still matter: they name how fast the count grows, as in section 3's grid.

  6. the move: read it as a bound

    the line:
    C∈O(n)C \in O(n)

    why it is allowed: By line 5, CC is at most 1 times nn.

  7. the move: a floor

    the line:
    C≥nC \ge n

    why it is allowed: By line 3, CC is at least nn too.

  8. the move: read it as a bound

    the line:
    C∈Ω(n)C \in \Omega(n)

    why it is allowed: By line 7, CC never drops below 1 times nn.

  9. the move: ceiling and floor together

    the line:
    C∈Θ(n)C \in \Theta(n)

    why it is allowed: Lines 6 and 8 bound CC by the same growth, nn.

try it on small inputs: over every set of 4 points, wherever they stand, the average is C=4C = 4

this tests where the formula comes from, on the learn page

what a full answer includes
  • The table of counts for each class.
  • The three scores, each with its smoothing.
  • Six points counted matching nn.

question 6about 10 minutes

Take section 5's four points, 2 8 t, 7 8 s, 6 2 c and 3 2 t, and the new point at 5 across, 4 up.

  1. With no smoothing, what does each class score, and why?
  2. With a smoothing count of 1, what does each score now?
  3. Which answer uses what the points show, and which ignores it?
a hint

Look for each class's empty band.

a worked answer

With no smoothing every class scores 0. No triangle and no square stands in band 4 to 6 across, and the circle is not in band 4 to 6 up. The tie goes to the class with more points, the triangles, on no evidence at all. With a smoothing count of 1, the triangles score 2/4 × 1/5 × 1/5 = 0.02. The squares score 1/4 × 1/4 × 1/4 = 0.016. The circle scores 1/4 × 2/4 × 1/4 = 0.031. The circle wins, as the only class with a point in one of the new point's bands.

this tests how it works, on the learn page

what a full answer includes
  • Each class's empty band.
  • The three smoothed scores.
  • A 0 that hides real evidence, turned small.