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.
What the syntax does
model.class_prior_- Reads the fitted probability assigned to each class before feature evidence.
Supplied for this practice
These objects are prepared afresh before your editor runs.
X- Feature dataframe for the declared population, preserving row indices.
X = df[['x1', 'x2']] y- Target series, aligned with
X.y = df.label X_train/X_test/y_train/y_test- Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with
X_train. Final-test targets, aligned withX_test.X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y) model- Already fitted on
X_train,y_train. Fitted estimator or pipeline for the requested model family. Fitted onX_train,y_train; reuse without refitting.
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
priors- Fitted class prior probabilities in the estimator’s class order.
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 |
- Store posterior class probabilities in
answer, indexed byX_testrows and labelled by model.classes_. - Store the fitted class
priorsinpriors, in that same class order.
answerpriorsHints
Think: Posterior columns and prior entries both follow fitted class order.
Tools: predict_proba, classes_ and class_prior_.
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.