ANALYSIS TOOL BOX

Predictive Analytics

CreateBayesClassifier

Train, evaluate, and visualize a Naive Bayes classification model.

naive-bayesclassificationmachine-learningprobabilisticfeature-importance

Introduction

Before reaching for a heavier classification model, it's worth knowing how far a fast, interpretable probabilistic baseline gets you — and whether the added complexity is actually earning its keep. CreateBayesClassifier trains a Naive Bayes model end-to-end across five scikit-learn variants (Gaussian, multinomial, Bernoulli, complement, and categorical), reports error rates, and produces three diagnostic plots — a normalized confusion matrix, feature discriminability scores, and a training-vs-test error comparison — so you can judge fit quality and overfitting risk in one pass.

Teaching Note

The "naïve" in Naive Bayes refers to the assumption that all predictor variables are conditionally independent given the class label. In other words, knowing the value of one feature tells you nothing additional about any other feature, once you know the class. This assumption almost never holds exactly in practice — yet Naive Bayes classifiers are remarkably robust to its violation.

The key insight is that even when features are correlated, the model can still rank classes correctly (produce the right argmax prediction) as long as the conditional probability estimates are in the right relative order. Errors in the probability magnitudes cancel out across features more often than they accumulate.

Naive Bayes is a useful first model to reach for because:

  1. Speed: Training is a single pass over the data to compute class priors and per-feature conditional statistics. Scoring is O(n_features).
  2. Data efficiency: It performs well with small datasets where complex models would overfit.
  3. Interpretability: The class-conditional means (GaussianNB) or log-probability tables (MultinomialNB) are directly inspectable.
  4. Calibrated probabilities: When assumptions are met, the posterior probabilities are well-calibrated, which is useful for decision-making under uncertainty.
  5. Baseline value: A Naive Bayes model that outperforms a more complex one is a strong signal to investigate whether feature engineering can replace model complexity.

Parameters

ParameterTypeDefaultDescription
dataframerequiredThe input pandas DataFrame containing training and testing data.
outcome_variablerequiredThe name of the target column to be predicted (must be categorical).
list_of_predictor_variablesrequiredA list of column names used as features for the model.
naive_bayes_type'gaussian'Which Naive Bayes variant to use. One of 'gaussian', 'multinomial', 'bernoulli', 'complement', or 'categorical'. Defaults to 'gaussian'.
alpha1.0Laplace/Lidstone smoothing parameter for 'multinomial', 'bernoulli', 'complement', and 'categorical' variants. Has no effect for 'gaussian'. Defaults to 1.0.
var_smoothing1e-09Portion of the largest variance across all features added to variances for numerical stability in 'gaussian' mode. Has no effect for other variants. Defaults to 1e-9.
test_size0.2The proportion of the dataset reserved for testing. Defaults to 0.2.
random_seed412Random state for reproducible train/test splits. Defaults to 412.
filter_nullsFalseIf True, drops all rows containing any NaN values across the selected columns before splitting. Defaults to False.
print_model_training_performanceFalseIf True, prints Training Error Rate, Test Error Rate, and a full Classification Report to the console. Defaults to False.
data_source_for_plotNoneOptional source citation appended to every plot caption. Defaults to None.
plot_model_test_performanceTrueIf True, renders a normalized confusion matrix heatmap on the test set. Defaults to True.
heatmap_color_palette'Blues'A seaborn/matplotlib colormap name for the confusion matrix. Defaults to 'Blues'.
figure_size_for_model_test_performance_plot(8, 6)Dimensions (width, height) for the confusion matrix plot. Defaults to (8, 6).
title_for_model_test_performance_plot'Model Performance'Title text for the confusion matrix plot.
subtitle_for_model_test_performance_plot'The predicted values vs. the actual values in the test dataset.'Subtitle text for the confusion matrix plot.
caption_for_model_test_performance_plotNoneOptional caption text for the confusion matrix plot.
title_y_indent_for_model_test_performance_plot1.09Vertical position of the title in axes-fraction coordinates. Defaults to 1.09.
subtitle_y_indent_for_model_test_performance_plot1.05Vertical position of the subtitle in axes-fraction coordinates. Defaults to 1.05.
caption_y_indent_for_model_test_performance_plot-0.215Vertical position of the caption in axes-fraction coordinates. Defaults to -0.215.
x_indent_for_model_test_performance_plot-0.115Horizontal starting position for the confusion matrix plot text. Defaults to -0.115.
plot_feature_importanceTrueIf True, renders a horizontal bar chart of feature discriminability scores. For 'gaussian', this is the standard deviation of class-conditional means across classes. For all other variants, this is the peak-to-peak range of log-probabilities across classes. Defaults to True.
top_n_to_highlight3Number of top features to emphasize in highlight_color. Defaults to 3.
highlight_color'#b0170c'Hex color for the top-N feature bars. Defaults to '#b0170c'.
fill_transparency0.8Alpha transparency for all feature bars. Defaults to 0.8.
figure_size_for_feature_importance_plot(8, 6)Dimensions (width, height) for the discriminability plot. Defaults to (8, 6).
title_for_feature_importance_plot'Feature Discriminability'Title text for the discriminability plot.
subtitle_for_feature_importance_plot'Shows how useful each feature is for distinguishing between classes.'Subtitle text for the discriminability plot.
caption_for_feature_importance_plotNoneOptional caption text for the discriminability plot.
title_y_indent_for_feature_importance_plot1.15Vertical position of the title. Defaults to 1.15.
subtitle_y_indent_for_feature_importance_plot1.1Vertical position of the subtitle. Defaults to 1.1.
caption_y_indent_for_feature_importance_plot-0.15Vertical position of the caption. Defaults to -0.15.
plot_training_and_test_performanceTrueIf True, renders a bar chart comparing training vs. test error rates. Defaults to True.
training_bar_color'#3a86ff'Hex color for the training bar. Defaults to '#3a86ff'.
test_bar_color'#b0170c'Hex color for the test bar. Defaults to '#b0170c'.
figure_size_for_performance_comparison_plot(7, 5)Dimensions (width, height) for the comparison chart. Defaults to (7, 5).
title_for_performance_comparison_plotNoneTitle text for the comparison chart. Defaults to 'Training vs. Test Error Rate'.
subtitle_for_performance_comparison_plotNoneSubtitle text for the comparison chart.
caption_for_performance_comparison_plotNoneOptional caption text for the comparison chart.
title_y_indent_for_performance_comparison_plot1.1Vertical position of the title. Defaults to 1.10.
subtitle_y_indent_for_performance_comparison_plot1.05Vertical position of the subtitle. Defaults to 1.05.
caption_y_indent_for_performance_comparison_plot-0.15Vertical position of the caption. Defaults to -0.15.
x_indent_for_performance_comparison_plot-0.115Horizontal starting position for comparison chart text. Defaults to -0.115.

Returns

The fitted scikit-learn Naive Bayes model object (GaussianNB, MultinomialNB, BernoulliNB, ComplementNB, or CategoricalNB, depending on naive_bayes_type).

Example

python
from analysistoolbox.predictive_analytics import CreateBayesClassifier
from sklearn.datasets import load_iris
import pandas as pd

# Classify iris species using GaussianNB (continuous features)
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['species'] = iris.target

model = CreateBayesClassifier(
    df,
    outcome_variable='species',
    list_of_predictor_variables=iris.feature_names.tolist()
)