s LearnSqueak.com

SMALLTALK LAB

Naive Bayes in Squeak: Train a Tiny Categorical Classifier

Predict walk or stay from two weather clues. Build the class prior, smooth each conditional count, test one withheld pair, and normalize the two class scores.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.

01 - Represent labeled observations

Each row has a temperature, ground condition and observed decision. The first two fields are inputs. The last field is the class to predict.

Independent Workspace expression - select all, then Print it
| rows |
rows := #((warm dry walk) (cold wet stay)).
{rows first first. rows first second. rows first third}

Expected: #(warm dry walk)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Represent labeled observations
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Read the input and class of the second row.

Download this checkpoint

02 - Estimate the class prior

Half the six training records are walks. Before seeing any inputs, that fraction is the estimated walk probability.

Independent Workspace expression - select all, then Print it
| rows |
rows := #((warm dry walk) (warm dry walk) (cold dry walk)
 (cold wet stay) (cold wet stay) (warm wet stay)).
(rows count: [:row | row third = #walk]) / rows size

Expected: 1/2

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Estimate the class prior
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The other half are stays, so both priors are equal here.

Download this checkpoint

03 - Count within a class

Of the three walk records, two are warm and none are wet. Count each feature only within the selected class.

Independent Workspace expression - select all, then Print it
| rows group |
rows := #((warm dry walk) (warm dry walk) (cold dry walk)
 (cold wet stay) (cold wet stay) (warm wet stay)).
group := rows select: [:row | row third = #walk].
{group size. group count: [:row | row first = #warm].
 group count: [:row | row second = #wet]}

Expected: #(3 2 0)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Count within a class
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Without smoothing, the zero wet count would force every wet walk score to zero.

Download this checkpoint

04 - Turn counts into comparable scores

For each feature, add one to the count and two to its denominator because it has two allowed values. Multiply both likelihoods by the prior, assuming features independent within the class.

Independent Workspace expression - select all, then Print it
| rows score |
rows := #((warm dry walk) (warm dry walk) (cold dry walk)
 (cold wet stay) (cold wet stay) (warm wet stay)).
score := [:temperature :ground :class | | group matchesT matchesG |
 group := rows select: [:row | row third = class].
 matchesT := group count: [:row | row first = temperature].
 matchesG := group count: [:row | row second = ground].
 (group size / rows size)
  * ((matchesT + 1) / (group size + 2))
  * ((matchesG + 1) / (group size + 2))].
{score value: #warm value: #wet value: #walk.
 score value: #warm value: #wet value: #stay}

Expected: {3/50. 4/25}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Turn counts into comparable scores
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Warm and wet favors stay despite a nonzero walk score.

Download this checkpoint

05 - Hold out a pair before scoring

This checkpoint removes the cold/dry walk record before fitting, leaving five training records. Cold/dry never appears in the smaller training set. Walk narrowly wins, 3/40 versus 9/125. One held-out prediction does not estimate overall accuracy.

Independent Workspace expression - select all, then Print it
| rows score |
rows := #((warm dry walk) (warm dry walk)
 (cold wet stay) (cold wet stay) (warm wet stay)).
score := [:temperature :ground :class | | group matchesT matchesG |
 group := rows select: [:row | row third = class].
 matchesT := group count: [:row | row first = temperature].
 matchesG := group count: [:row | row second = ground].
 (group size / rows size)
  * ((matchesT + 1) / (group size + 2))
  * ((matchesG + 1) / (group size + 2))].
(score value: #cold value: #dry value: #walk) >
 (score value: #cold value: #dry value: #stay)

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Hold out a pair before scoring
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Put the removed record back and compare the scores. The counts and class priors change.

Download this checkpoint

06 - Normalize the two scores

Divide each positive class score by their sum. This makes a two-class distribution under model assumptions, not a calibrated real-world confidence claim.

Independent Workspace expression - select all, then Print it
| rows score walk stay |
rows := #((warm dry walk) (warm dry walk) (cold dry walk)
 (cold wet stay) (cold wet stay) (warm wet stay)).
score := [:temperature :ground :class | | group matchesT matchesG |
 group := rows select: [:row | row third = class].
 matchesT := group count: [:row | row first = temperature].
 matchesG := group count: [:row | row second = ground].
 (group size / rows size)
  * ((matchesT + 1) / (group size + 2))
  * ((matchesG + 1) / (group size + 2))].
walk := score value: #cold value: #wet value: #walk.
stay := score value: #cold value: #wet value: #stay.
{walk / (walk + stay). stay / (walk + stay)}

Expected: {1/7. 6/7}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Normalize the two scores
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Add both outputs. The result should be exactly one.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Naive Bayes in Squeak: Train a Tiny Categorical Classifier native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The complete static checkpoints appear above.

Reason about correctness and limits

The example assumes the features are independent conditional on class, not independent overall. Duplicating a feature can spuriously double its influence.

This small example only accepts warm/cold and dry/wet as meaningful query features. In a real app, validate categories and use an independently labeled test set.

Three practice challenges

Change the prior

If four of six examples were walks, what is the new walk prior?

Show solution

4/6 = 2/3.

Allow more categories

Suppose temperature also permits mild. What does the add-one denominator become?

Show solution

Class size plus three for temperature; ground still uses plus two.

Handle a tie

What should an application do when both scores are equal?

Show solution

Return an explicit tie or apply a documented tie-breaker, rather than claiming high certainty.

Further reading

Manning, Raghavan and Schütze on Naive Bayes. The Workspace examples above are original.

Continue learning

First Dictionary | Probability Lab | Document Search

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.