Skip to content

Commit

Permalink
Merge pull request #14 from msamsami/alpha-smoothing
Browse files Browse the repository at this point in the history
New feature: smoothing parameter
  • Loading branch information
msamsami authored Jul 15, 2023
2 parents cd17ee9 + 4e83c1e commit e7a0edc
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 201 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# WNB: General and weighted naive Bayes classifiers

![](https://img.shields.io/badge/version-v0.1.14-green)
![](https://img.shields.io/badge/version-v0.1.15-green)
![](https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9-blue)
![](https://github.com/msamsami/weighted-naive-bayes/actions/workflows/python-publish.yml/badge.svg)
[![](https://img.shields.io/pypi/v/wnb)](https://pypi.org/project/wnb/)
![](https://img.shields.io/pypi/dm/wnb)


<p>
<img src="https://raw.githubusercontent.com/msamsami/weighted-naive-bayes/main/logo.png" alt="wnb logo" width="275" />
<img src="https://raw.githubusercontent.com/msamsami/weighted-naive-bayes/main/docs/logo.png" alt="wnb logo" width="275" />
<br>
</p>

Expand Down
File renamed without changes
46 changes: 24 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@


setup(
name='wnb',
version='0.1.14',
description='Python library for the implementations of general and weighted naive Bayes (WNB) classifiers.',
keywords=['python', 'bayes', 'naivebayes', 'classifier', 'probabilistic'],
author='Mehdi Samsami',
author_email='[email protected]',
url='https://github.com/msamsami/weighted-naive-bayes',
long_description=codecs.open(path.join(path.abspath(path.dirname(__file__)), 'README.md'), encoding='utf-8').read(),
long_description_content_type='text/markdown',
packages=['wnb'],
name="wnb",
version="0.1.15",
description="Python library for the implementations of general and weighted naive Bayes (WNB) classifiers.",
keywords=["python", "bayes", "naivebayes", "classifier", "probabilistic"],
author="Mehdi Samsami",
author_email="[email protected]",
url="https://github.com/msamsami/weighted-naive-bayes",
long_description=codecs.open(
path.join(path.abspath(path.dirname(__file__)), "README.md"), encoding="utf-8"
).read(),
long_description_content_type="text/markdown",
packages=["wnb"],
classifiers=[
'Intended Audience :: Science/Research',
'Intended Audience :: Developers',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'Topic :: Scientific/Engineering :: Information Analysis',
'Topic :: Software Development :: Libraries :: Python Modules',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'License :: OSI Approved :: MIT License',
"Intended Audience :: Science/Research",
"Intended Audience :: Developers",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Scientific/Engineering :: Information Analysis",
"Topic :: Software Development :: Libraries :: Python Modules",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"License :: OSI Approved :: MIT License",
],
python_requires=">=3.7",
install_requires=['pandas==1.4.1', 'scikit-learn>=1.0.2'],
extras_require={},
install_requires=["pandas==1.4.1", "scikit-learn>=1.0.2"],
extras_require={"dev": "pytest==7.3.1"},
)
13 changes: 7 additions & 6 deletions wnb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
__version__ = "0.1.14"
__version__ = "0.1.15"
__author__ = "Mehdi Samsami"

__all__ = [
'GeneralNB',
'GaussianWNB',
'Distribution',
'ContinuousDistMixin',
'DiscreteDistMixin'
"GeneralNB",
"GaussianWNB",
"Distribution",
"ContinuousDistMixin",
"DiscreteDistMixin",
]


from ._base import ContinuousDistMixin, DiscreteDistMixin
from ._enums import Distribution
from .gnb import GeneralNB
Expand Down
55 changes: 34 additions & 21 deletions wnb/_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABCMeta
import inspect
from functools import wraps
import inspect
from numbers import Number
from typing import List, Tuple, Union
import warnings
Expand All @@ -9,10 +9,7 @@

from ._enums import Distribution

__all__ = [
'ContinuousDistMixin',
'DiscreteDistMixin'
]
__all__ = ["ContinuousDistMixin", "DiscreteDistMixin"]


def vectorize(otypes=None, excluded=None, signature=None):
Expand All @@ -21,7 +18,9 @@ def vectorize(otypes=None, excluded=None, signature=None):
"""

def decorator(func):
vectorized = np.vectorize(func, otypes=otypes, excluded=excluded, signature=signature)
vectorized = np.vectorize(
func, otypes=otypes, excluded=excluded, signature=signature
)

@wraps(func)
def wrapper(*args):
Expand All @@ -41,8 +40,11 @@ class DistMixin(metaclass=ABCMeta):
_support: Union[List[float], Tuple[float, float]] = None

@classmethod
def from_data(cls, data):
"""Creates an instance of the class from given data. Distribution parameters will be estimated from the data.
def from_data(cls, data, **kwargs):
"""Creates an instance of the class from given data. Distribution parameters will be estimated from data.
Args:
data: Input data from which distribution parameters will be estimated.
Returns:
self: An instance of the class.
Expand Down Expand Up @@ -93,28 +95,39 @@ def get_params(self) -> dict:
def support(self) -> Union[List[float], Tuple[float, float]]:
"""Returns the support of the probability distribution.
If support is a list, it represents a limited number of discrete values. If it is a tuple, it indicates a limited or unlimited range of continuous values.
If support is a list, it represents a limited number of discrete values.
If it is a tuple, it indicates a limited or unlimited range of continuous values.
"""
return self._support

def _check_support(self, x):
if (isinstance(self.support, list) and x not in self.support) or \
(isinstance(self.support, tuple) and (x < self.support[0] or x > self.support[1])):
warnings.warn("Value doesn't lie within the support of the distribution", RuntimeWarning)
if (isinstance(self.support, list) and x not in self.support) or (
isinstance(self.support, tuple)
and (x < self.support[0] or x > self.support[1])
):
warnings.warn(
"Value doesn't lie within the support of the distribution",
RuntimeWarning,
)
else:
pass

def __repr__(self) -> str:
return "".join([
"<",
self.__class__.__name__,
"(",
", ".join(
[f"{k}={v:.4f}" if isinstance(v, Number) else f"{k}={v}" for k, v in self.get_params().items()]
),
")>"
])
return "".join(
[
"<",
self.__class__.__name__,
"(",
", ".join(
[
f"{k}={v:.4f}" if isinstance(v, Number) else f"{k}={v}"
for k, v in self.get_params().items()
]
),
")>",
]
)


class ContinuousDistMixin(DistMixin, metaclass=ABCMeta):
Expand Down
5 changes: 2 additions & 3 deletions wnb/_enums.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from enum import Enum


__all__ = [
"Distribution"
]
__all__ = ["Distribution"]


class Distribution(str, Enum):
"""
Names of probability distributions.
"""

NORMAL = "Normal"
LOGNORMAL = "Lognormal"
EXPONENTIAL = "Exponential"
Expand Down
Loading

0 comments on commit e7a0edc

Please sign in to comment.