Understand the idea
Production selects GaussianNB for continuous inputs, BernoulliNB for binary flags and OHE–BernoulliNB for categorical inputs. It does not expose arbitrary mixed inputs or CategoricalNB.
Python skill: Uses continuous measurement likelihoods.
Meet the syntax
GaussianNB()
BernoulliNB()
Pipeline([("encode", OneHotEncoder(...)), ("model", BernoulliNB())])GaussianNB()- Uses continuous measurement likelihoods.
BernoulliNB()- Uses presence and absence of binary indicators.
OneHotEncoder(...)- Turns named categories into indicators before the production Bernoulli path; learn its schema inside fitting.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
from sklearn.naive_bayes import BernoulliNB
flags=['chocolate','fruity','caramel','peanutyalmondy','nougat','crispedricewafer','hard','bar','pluribus']
model=BernoulliNB().fit(train[flags],train.popular)
answer=model.predict(test[flags])
This practice: Read and run the Python. Next: Change · Match Naive Bayes to feature types.
Given data · candy_class
85 observations. One candy product in the survey. The dataframe df is supplied afresh for each Run.
Download source CSV · Source and original dictionary
| competitorname | chocolate | fruity | caramel | peanutyalmondy | nougat | crispedricewafer | hard | bar | pluribus | sugarpercent | pricepercent | winpercent | popular |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 100 Grand | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0.732 | 0.86 | 66.9717 | 50% or above |
| 3 Musketeers | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0.604 | 0.511 | 67.6029 | 50% or above |
| One dime | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.011 | 0.116 | 32.2611 | below 50% |
| One quarter | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.011 | 0.511 | 46.1165 | below 50% |
| Air Heads | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.906 | 0.511 | 52.3415 | 50% or above |
| Almond Joy | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0.465 | 0.767 | 50.3475 | 50% or above |
| Baby Ruth | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 0 | 0.604 | 0.767 | 56.9145 | 50% or above |
| Boston Baked Beans | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0.313 | 0.511 | 23.4178 | below 50% |
Column meanings and units
sugarpercent: sugar percentile. pricepercent: price percentile. winpercent: percentage of survey matchups won. Ingredient, bar and multipack fields are 0/1 flags.
Percentile ranks are neither physical sugar percentages nor currency prices. Ratios of percentiles do not measure economic value. Chocolate and fruit flags can overlap; non-chocolate is not synonymous with fruit. The ML class target is defined by winpercent ≥ 50.
| Column | Stored type |
|---|---|
| competitorname | str |
| chocolate | int64 |
| fruity | int64 |
| caramel | int64 |
| peanutyalmondy | int64 |
| nougat | int64 |
| crispedricewafer | int64 |
| hard | int64 |
| bar | int64 |
| pluribus | int64 |
| sugarpercent | float64 |
| pricepercent | float64 |
| winpercent | float64 |
| popular | str |
Supplied setup · available if you need to inspect it
This code runs before your editor on every Run. These are the objects your exercise uses.
from sklearn.model_selection import train_test_split
train,test=train_test_split(df,test_size=.2,random_state=42,stratify=df.popular)Your task · Follow
Fit BernoulliNB to Candy’s binary flags and predict held-away rows.
Hint 1 — Think
The declared target must stay separate from the presence/absence indicators.
Hint 2 — Tools
BernoulliNB and an explicit flags list.
Hint 3 — Approach
Fit only the nine permitted flags to the training popular labels and predict the same test schema.
Explained solution
from sklearn.naive_bayes import BernoulliNB
flags=['chocolate','fruity','caramel','peanutyalmondy','nougat','crispedricewafer','hard','bar','pluribus']
model=BernoulliNB().fit(train[flags],train.popular)
answer=model.predict(test[flags])
The binary likelihood matches the feature meanings and excludes the target-derived winpercent source.
Helpful prior knowledge: Gaussian Naive Bayes · Encode categories without inventing order These links are guidance, not locks.
Sources and API context
Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.