Skip to main content

Explainable Class–Specific Naive–Bayes Classifier

Test codecov.io

Description

Explainable Naive Bayes (XNB) classifier includes two important features:

  1. The probability is calculated by means of Kernel Density Estimation (KDE).

  2. The probability for each class does not use all variables, but only those that are relevant for each specific class.

From the point of view of the classification performance, the XNB classifier is comparable to NB classifier. However, the XNB classifier provides the subsets of relevant variables for each class, which contributes considerably to explaining how the predictive model is performing. In addition, the subsets of variables generated for each class are usually different and with remarkably small cardinality.

Installation

For example, if you are using pip, yo can install the package by:

pip install xnb

Example of use:

from xnb import XNB
from xnb.enums import BWFunctionName, Kernel, Algorithm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris
import pandas as pd
''' 1. Read the dataset.
It is important that the dataset is a pandas DataFrame object with named columns.
This way, we can obtain the dictionary of important variables for each class.'''
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target
x = df.drop('target', axis=1)
y = df['target'].replace(to_replace=[0, 1, 2],
                         value=['setosa', 'versicolor', 'virginica'])
x_train, x_test, y_train, y_test = train_test_split(
    x,
    y,
    test_size=0.20,
    random_state=0,
)
''' 2. By calling the fit() function,
we prepare the object to be able to make the prediction later. '''
# Initialize and fit the XNB model
xnb = XNB(
    show_progress_bar=False,
    bw_function=BWFunctionName.HSILVERMAN,
    kernel=Kernel.GAUSSIAN,
    algorithm=Algorithm.AUTO,
    n_sample=50,
)

# Fit the model
xnb.fit(x_train, y_train)
''' 3. When the fit() function finishes,
we can now access the feature selection dictionary it has calculated. '''
feature_selection = xnb.feature_selection_dict
''' 4. We predict the values of "y_test" using implicitly the calculated dictionary. '''
y_pred = xnb.predict(x_test)

# Output
print('Relevant features for each class:\n')
for target, features in feature_selection.items():
  print(f'{target}: {features}')
print(f'\n-------------\nAccuracy: {accuracy_score(y_test, y_pred)}')

The output is:

Relevant features for each class:

setosa: {'petal length (cm)'}
virginica: {'petal length (cm)', 'petal width (cm)'}
versicolor: {'petal length (cm)', 'petal width (cm)'}

-------------
Accuracy: 1.0

Links

GitHub PyPI

Release files for xnb 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xnb 0.5.1
File Size Uploaded
xnb-0.5.1.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xnb 0.5.1
File Interpreter ABI Platform
xnb-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.5 kB

Release files / xnb-0.5.1.tar.gz

Download URL xnb-0.5.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
759763e0362631fc2c35b2adb3196a101f610fde72ebfc1d301d3e81522efdbc
BLAKE2b-256 checksum
How to use checksums
48eb430925b36186e35048b94078033e112d773a9f853278d44e6fd0cce97472
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / xnb-0.5.1-py3-none-any.whl

Download URL xnb-0.5.1-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
649bb6878976c6b3d2a7e2d3f533dbbbbb9a42a3fdeede16a019737cb9bc13df
BLAKE2b-256 checksum
How to use checksums
58e61f3645305f780e9ce5da6243d0aba789c432ef0d38d409c0f12da86e2dd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page