Naive Bayes
Feature Importance
Machine Learning
Data Science
Classification

How to get feature Importance in naive bayes?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Naive Bayes classifiers do not expose feature importance in the same way that tree models expose feature_importances_. That does not mean interpretability is impossible; it means you need to derive importance from the model's learned probabilities or from a separate evaluation method such as permutation importance.

Why Naive Bayes is different

Naive Bayes predicts with class priors and class-conditional feature probabilities. It does not learn one global coefficient per feature the way logistic regression does, so there is no single built-in importance number that works across all Naive Bayes variants.

The useful question is usually: how much does a feature push the prediction toward one class versus another?

Multinomial and Bernoulli Naive Bayes

For text classification and other count or binary features, you can inspect the learned log probabilities. In scikit-learn, feature_log_prob_ stores the log probability of each feature under each class.

For binary classification, a simple importance measure is the difference in log probabilities between the two classes.

python
1import numpy as np
2from sklearn.feature_extraction.text import CountVectorizer
3from sklearn.naive_bayes import MultinomialNB
4
5texts = [
6    "great movie great acting",
7    "excellent plot and acting",
8    "terrible movie boring plot",
9    "boring and bad acting",
10]
11labels = [1, 1, 0, 0]
12
13vectorizer = CountVectorizer()
14X = vectorizer.fit_transform(texts)
15model = MultinomialNB()
16model.fit(X, labels)
17
18feature_names = np.array(vectorizer.get_feature_names_out())
19log_odds = model.feature_log_prob_[1] - model.feature_log_prob_[0]
20
21important = sorted(zip(feature_names, log_odds), key=lambda item: abs(item[1]), reverse=True)
22print(important[:5])

A large positive value means the feature is much more associated with class 1. A large negative value means the feature is much more associated with class 0.

Gaussian Naive Bayes

For continuous features, the model learns means and variances per class. A feature tends to be more informative when its class means are far apart relative to the within-class spread.

python
1import numpy as np
2from sklearn.naive_bayes import GaussianNB
3
4X = np.array([
5    [1.0, 10.0],
6    [1.2, 9.5],
7    [4.8, 10.1],
8    [5.1, 9.8],
9])
10y = np.array([0, 0, 1, 1])
11
12model = GaussianNB()
13model.fit(X, y)
14
15mean_gap = np.abs(model.theta_[1] - model.theta_[0])
16print(mean_gap)

mean_gap is not a universal importance metric, but it is a useful first signal for which continuous features separate the classes.

Permutation importance

If you want a model-agnostic answer, permutation importance is often the most defensible method. Shuffle one feature, score the model again, and measure how much performance drops.

python
1from sklearn.inspection import permutation_importance
2from sklearn.model_selection import train_test_split
3from sklearn.metrics import accuracy_score
4
5X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
6model = GaussianNB()
7model.fit(X_train, y_train)
8
9result = permutation_importance(model, X_test, y_test, n_repeats=10, random_state=0)
10print(result.importances_mean)

This method answers a different question: how much does the model rely on this feature for predictive performance on held-out data?

Which approach should you use

If you want class-specific interpretation, inspect the class-conditional probabilities or log-odds. If you want a model-performance view that works across model types, use permutation importance.

For text classifiers, the log-probability difference is usually the most intuitive. For continuous features, separation of class means plus permutation importance is often more useful.

Common Pitfalls

A common mistake is looking for feature_importances_ on a Naive Bayes model and assuming the model cannot be interpreted when that attribute is missing.

Another issue is treating raw probability values as directly comparable across variants without considering the underlying model form. Multinomial, Bernoulli, and Gaussian Naive Bayes learn different kinds of parameters.

It is also easy to confuse correlation with importance. A highly predictive feature can look weak if another correlated feature already carries most of the signal.

Summary

  • Naive Bayes does not expose tree-style feature importance by default.
  • For Multinomial or Bernoulli models, inspect class-conditional log probabilities or log-odds differences.
  • For Gaussian models, class-mean separation can give a useful first signal.
  • Permutation importance is a model-agnostic way to measure reliance on each feature.
  • Choose the interpretation method based on whether you want class-level explanation or predictive-impact explanation.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.