Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessonsQUESTIONS · MODELS · EVIDENCE
Class distributions · ML-C15 · 25–40 MIN

Gaussian Naive Bayes

Combine class priors with continuous feature evidence.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeReason about the Python
  3. TransferExplain the Python result
  4. ApplyBuild a guided full workflow

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.

Combine class priors with continuous feature evidence.Class AClass BGaussian NB: independent feature likelihoods within a class.
Schematic · Combine class priors with continuous feature evidence.Scroll the diagram horizontally if needed.

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.

COV90 · first 8 prepared rows
x1x2label
-0.878151-0.683719A
-2.05616-0.327603A
1.30311.29443A
-0.995383-0.240343A
-0.654128-0.314893A
-2.112-0.509527A
-1.497460.375701A
-1.09822-0.763825A

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.

Input schema
ColumnStored type
x1float64
x2float64
labelstr
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.

Your task · Follow

Inspect class priors and posterior probabilities.

answerpriors

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.