Skip to main content

Explainable Naive Bayes (XNB) classifier. Using KDE for feature selection and Naive Bayes for prediction.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xnb-0.4.4.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xnb-0.4.4-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file xnb-0.4.4.tar.gz.

File metadata

  • Download URL: xnb-0.4.4.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for xnb-0.4.4.tar.gz
Algorithm Hash digest
SHA256 20594baec12ef8cd34bceb17ff54325baa2d58b78a60bc917b27701944aadf58
MD5 70c3983464f325b10a3332bd850ddbbd
BLAKE2b-256 ced151a71969aa8520ac7922425edd4cb346ae897a8bbf88e8321f9c63ce3fa9

See more details on using hashes here.

File details

Details for the file xnb-0.4.4-py3-none-any.whl.

File metadata

  • Download URL: xnb-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for xnb-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d61b40962c331d3fb02fdc34b1aed5cd8f3f0d9fc6a3b0b1ba43d78b8f1a6ed2
MD5 36cc523d7f469cc19c69b56b4145cc96
BLAKE2b-256 854b1175515ab135a3e8f3b5edd62b49b99ff8fbebf2b65484813056687466f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page