# Explainable Class–Specific Naive–Bayes Classifier
<table>
<tr>
<th>Repositories</th>
<th>Badges</th>
</tr>
<tr>
<td>
<a href="https://github.com/sorul/xnb">
<picture>
<img alt="Github" src="docs/images/github.png" width="30" height="30">
</picture>
</a>
<a href="https://pypi.org/project/xnb/">
<picture>
<img alt="PYPI" src="docs/images/pypi.png" width="30" height="30">
</picture>
</a>
</td>
<td>
<img alt="Testing & Coverage." src="https://github.com/sorul/xnb/actions/workflows/testing_coverage.yml/badge.svg?branch=master">
<img alt="Codecov.io" src="https://codecov.io/github/sorul/xnb/coverage.svg?branch=master">
</a>
</td>
</tr>
</table>
## 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:
```python
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. '''
xnb = XNB(
show_progress_bar = True # optional
)
xnb.fit(
x_train,
y_train,
bw_function = BWFunctionName.HSILVERMAN, # optional
kernel = Kernel.GAUSSIAN, # optional
algorithm = Algorithm.AUTO, # optional
n_sample = 50 # optional
)
''' 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
```
Raw data
{
"_id": null,
"home_page": null,
"name": "xnb",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0.0,>=3.9.0",
"maintainer_email": null,
"keywords": "KDE, naive bayes, classification, feature selection, machine learning, explicability, kernel density, class-specific",
"author": "Cayetano Romero Vargas",
"author_email": "cromerovargas2d@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/79/dc/5eec587a638f06cb54d97b6ed21731f5ed37bc8d585fc16000c2144136c3/xnb-0.2.3.tar.gz",
"platform": null,
"description": "# Explainable Class\u2013Specific Naive\u2013Bayes Classifier\n\n<table>\n <tr>\n <th>Repositories</th>\n <th>Badges</th>\n </tr>\n <tr>\n <td>\n <a href=\"https://github.com/sorul/xnb\">\n <picture>\n <img alt=\"Github\" src=\"docs/images/github.png\" width=\"30\" height=\"30\">\n </picture>\n </a>\n <a href=\"https://pypi.org/project/xnb/\">\n <picture>\n <img alt=\"PYPI\" src=\"docs/images/pypi.png\" width=\"30\" height=\"30\">\n </picture>\n </a>\n </td>\n <td>\n <img alt=\"Testing & Coverage.\" src=\"https://github.com/sorul/xnb/actions/workflows/testing_coverage.yml/badge.svg?branch=master\">\n <img alt=\"Codecov.io\" src=\"https://codecov.io/github/sorul/xnb/coverage.svg?branch=master\">\n </a>\n </td>\n </tr>\n</table>\n\n\n\n## Description\nExplainable Naive Bayes (XNB) classifier includes two important\nfeatures: \n\n1) The probability is calculated by means of Kernel Density Estimation (KDE).\n\n2) The probability for each class does not use all variables,\nbut **only those that are relevant** for each specific class.\n\nFrom the point of view of the classification performance,\nthe XNB classifier is comparable to NB classifier.\nHowever, the XNB classifier provides the subsets of relevant variables for each class,\nwhich contributes considerably to explaining how the predictive model is performing.\nIn addition, the subsets of variables generated for each class are usually different and with remarkably small cardinality.\n\n## Installation\n\nFor example, if you are using pip, yo can install the package by:\n```\npip install xnb\n```\n\n## Example of use:\n\n```python\nfrom xnb import XNB\nfrom xnb.enums import BWFunctionName, Kernel, Algorithm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.datasets import load_iris\nimport pandas as pd\n\n''' 1. Read the dataset.\nIt is important that the dataset is a pandas DataFrame object with named columns.\nThis way, we can obtain the dictionary of important variables for each class.'''\niris = load_iris()\ndf = pd.DataFrame(iris.data, columns=iris.feature_names)\ndf['target' ] = iris.target\nx = df.drop('target', axis=1)\ny = df['target'].replace(\n to_replace= [0, 1, 2], value = ['setosa', 'versicolor', 'virginica']\n)\nx_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=0)\n\n''' 2. By calling the fit() function,\nwe prepare the object to be able to make the prediction later. '''\nxnb = XNB(\n show_progress_bar = True # optional\n)\nxnb.fit(\n x_train,\n y_train,\n bw_function = BWFunctionName.HSILVERMAN, # optional\n kernel = Kernel.GAUSSIAN, # optional\n algorithm = Algorithm.AUTO, # optional\n n_sample = 50 # optional\n)\n\n''' 3. When the fit() function finishes,\nwe can now access the feature selection dictionary it has calculated. '''\nfeature_selection = xnb.feature_selection_dict\n\n''' 4. We predict the values of \"y_test\" using implicitly the calculated dictionary. '''\ny_pred = xnb.predict(X_test)\n\n# Output\nprint('Relevant features for each class:\\n')\nfor target, features in feature_selection.items():\n print(f'{target}: {features}')\nprint(f'\\n-------------\\nAccuracy: {accuracy_score(y_test, y_pred)}')\n```\nThe output is:\n```\nRelevant features for each class:\n\nsetosa: {'petal length (cm)'}\nvirginica: {'petal length (cm)', 'petal width (cm)'}\nversicolor: {'petal length (cm)', 'petal width (cm)'}\n\n-------------\nAccuracy: 1.0\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Explainable Naive Bayes (XNB) classifier. Using KDE for feature selection and Naive Bayes for prediction.",
"version": "0.2.3",
"project_urls": null,
"split_keywords": [
"kde",
" naive bayes",
" classification",
" feature selection",
" machine learning",
" explicability",
" kernel density",
" class-specific"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "aa736f3cbf743f9b102a53c3ed4a2195e5f4f767a263347f373f91be45d03906",
"md5": "4b103948f513bceb7211e4d0aa8326b4",
"sha256": "76a155e6793accaa8a3990a4cfac229086da4c76412a5cf4e52098dddd4a2fee"
},
"downloads": -1,
"filename": "xnb-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4b103948f513bceb7211e4d0aa8326b4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0.0,>=3.9.0",
"size": 11306,
"upload_time": "2024-10-02T10:27:10",
"upload_time_iso_8601": "2024-10-02T10:27:10.849309Z",
"url": "https://files.pythonhosted.org/packages/aa/73/6f3cbf743f9b102a53c3ed4a2195e5f4f767a263347f373f91be45d03906/xnb-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "79dc5eec587a638f06cb54d97b6ed21731f5ed37bc8d585fc16000c2144136c3",
"md5": "d455c4ea5e09bc1ed670f7f23497ba05",
"sha256": "0dd9c792887ba907bb46175ff35263cee27facba41b548dc545df5d09d5b89a7"
},
"downloads": -1,
"filename": "xnb-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "d455c4ea5e09bc1ed670f7f23497ba05",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0.0,>=3.9.0",
"size": 11563,
"upload_time": "2024-10-02T10:27:12",
"upload_time_iso_8601": "2024-10-02T10:27:12.084642Z",
"url": "https://files.pythonhosted.org/packages/79/dc/5eec587a638f06cb54d97b6ed21731f5ed37bc8d585fc16000c2144136c3/xnb-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-02 10:27:12",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "xnb"
}