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.5.0.tar.gz (12.2 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.5.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for xnb-0.5.0.tar.gz
Algorithm Hash digest
SHA256 f436d6a50eb62e5f6a25864a57e067a0a1a3dafa31abea0765482a6ceae5e16b
MD5 ba0c41a3b3ff546e9201551c4b960f75
BLAKE2b-256 eceb4759b817b8596bc87250c88c197e54c14ee17b9022ab08af3846173d7918

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xnb-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e02439b65ff54447e97145d34646ad886123a530debe1e1fdfe9acfa71d72d9
MD5 b814ad75334a3b5339aeafcc6e63e714
BLAKE2b-256 82369a332eb311b75569683aef4327eeafc136e940093f56fe45243772bf227d

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