Understand the idea
GaussianNB models each feature’s class-conditional density and combines evidence using a conditional-independence assumption. Posterior class probabilities and probability densities are different quantities.
Python skill: Creates Naive Bayes for continuous features with class-conditional Gaussian likelihoods.
Meet the syntax
GaussianNB()
model.class_prior_
model.predict_proba(X_test)GaussianNB()- Creates Naive Bayes for continuous features with class-conditional Gaussian likelihoods.
model.class_prior_- Reads the fitted probability assigned to each class before feature evidence.
model.predict_proba(X_test)- Returns class-aligned probabilities under the model’s likelihood assumptions.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
answer=pd.DataFrame(model.predict_proba(X_test),columns=model.classes_,index=X_test.index)
priors=model.class_prior_
This practice: Read and run the Python. Next: Change · Gaussian Naive Bayes.
Naive Bayes · from idea to workflow
Question: Which class best explains these measurements or flags?
Mechanism: Combine feature likelihoods under conditional independence and class priors.
Watch for: Correlated evidence can be counted twice; continuous and binary features need appropriate likelihoods.
Interpret or debug · self-review: Identify whether Gaussian or Bernoulli assumptions match the input. Explain why correlated indicators can create overconfident evidence.
Guided full workflow: Apply this model with a baseline, training validation and one final evaluation →
Independent full workflow: ML-X11 · ML-X12 · ML-X13. First practise complete regression and classification in F11–F12; check readiness in W-K2–W-K3.
Given data · COV90
90 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.
| x1 | x2 | label |
|---|---|---|
| -0.878151 | -0.683719 | A |
| -2.05616 | -0.327603 | A |
| 1.3031 | 1.29443 | A |
| -0.995383 | -0.240343 | A |
| -0.654128 | -0.314893 | A |
| -2.112 | -0.509527 | A |
| -1.49746 | 0.375701 | A |
| -1.09822 | -0.763825 | A |
Column meanings and units
Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.
Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.
| Column | Stored type |
|---|---|
| x1 | float64 |
| x2 | float64 |
| label | 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
X=df[['x1','x2']]
y=df.label
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2,random_state=42,stratify=y)
from sklearn.naive_bayes import GaussianNB
model=GaussianNB().fit(X_train,y_train)
Your task · Follow
Inspect class priors and posterior probabilities.
Hint 1 — Think
Posterior columns and prior entries both follow fitted class order.
Hint 2 — Tools
predict_proba, classes_ and class_prior_.
Hint 3 — Approach
Label posterior probabilities by test row and class, then inspect the fitted priors.
Explained solution
answer=pd.DataFrame(model.predict_proba(X_test),columns=model.classes_,index=X_test.index)
priors=model.class_prior_
The labelled table distinguishes row-specific posterior evidence from the model’s overall class-prior frequencies.
Helpful prior knowledge: Labels and probabilities 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.