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.
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.
| rows |
rows := #((warm dry walk) (cold wet stay)).
{rows first first. rows first second. rows first third}Expected: #(warm dry walk)

Try it: Read the input and class of the second row.
02 - Estimate the class prior
Half the six training records are walks. Before seeing any inputs, that fraction is the estimated walk probability.
| 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 sizeExpected: 1/2

Try it: The other half are stays, so both priors are equal here.
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.
| 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)

Try it: Without smoothing, the zero wet count would force every wet walk score to zero.
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.
| 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}

Try it: Warm and wet favors stay despite a nonzero walk score.
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.
| 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

Try it: Put the removed record back and compare the scores. The counts and class priors change.
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.
| 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}

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

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.