MixedNB#

class skbn.MixedNB(categorical_features=None, bernoulli_features=None, gaussian_features=None, var_smoothing=1e-09, alpha=1.0)#

Mixed Naive Bayes classifier for heterogeneous data.

This classifier is designed to handle datasets with a mix of continuous (Gaussian), categorical, and binary (Bernoulli) features. It internally uses scikit-learn’s GaussianNB, CategoricalNB, and BernoulliNB on the respective feature subsets.

Features are classified and handled as follows: 1. User-defined: The user can explicitly specify which features are

categorical or Bernoulli using the constructor parameters.

  1. Auto-detection: For features not specified by the user, the classifier will attempt to infer the type during fit: - Features with exactly two unique values are treated as Bernoulli. - Integer features with more than two unique values are treated as

    Categorical. Note: These features must be encoded as non-negative integers (0, 1, 2, …).

    • Floating-point features are treated as Gaussian.

Read more in the User Guide.

Parameters:
categorical_featuresarray-like of shape (n_categorical_features,), default=None

A list of indices for the features that should be treated as categorical. If None, categorical features are inferred from data where dtype is integer and the number of unique values is greater than 2. .. warning:

Categorical features must be encoded as non-negative integers.
bernoulli_featuresarray-like of shape (n_bernoulli_features,), default=None

A list of indices for the features that should be treated as Bernoulli. If None, Bernoulli features are inferred from data where the number of unique values is exactly 2.

var_smoothingfloat, default=1e-9

Portion of the largest variance of all Gaussian features that is added to variances for calculation stability. Passed to GaussianNB.

alphafloat, default=1.0

Additive (Laplace/Lidstone) smoothing parameter. Passed to CategoricalNB and BernoulliNB.

Attributes:
classes_ndarray of shape (n_classes,)

Class labels known to the classifier.

class_log_prior_ndarray of shape (n_classes,)

Log probability of each class (smoothed).

n_features_in_int

Number of features seen during fit.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen during fit. Defined only when X has feature names that are all strings.

feature_types_dict

A dictionary mapping feature type (‘gaussian’, ‘categorical’, ‘bernoulli’) to the indices of the features of that type.

estimators_dict

A dictionary containing the fitted Naive Bayes estimators for each feature type.

See also

GaussianNB

Naive Bayes classifier for Gaussian features.

CategoricalNB

Naive Bayes classifier for categorical features.

BernoulliNB

Naive Bayes classifier for multivariate Bernoulli models.

Examples

>>> import numpy as np
>>> from skbn.mixed_nb import MixedNB
>>> # Data: [Gaussian, Bernoulli, Categorical (3 cats)]
>>> X = np.array([
...     [0.5, 0, 0],
...     [-1.2, 1, 1],
...     [0.6, 1, 2],
...     [-0.1, 0, 0],
...     [2.5, 1, 1],
...     [-3.0, 0, 2]
... ])
>>> y = np.array([0, 1, 1, 0, 1, 0])
>>> clf = MixedNB()
>>> clf.fit(X, y)
MixedNB()
>>> clf.predict([[-0.8, 1, 1]])
array([1])

Methods

fit(X, y)

Fit the Mixed Naive Bayes classifier according to X, y.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

predict(X)

Perform classification on an array of test vectors X.

predict_log_proba(X)

Return log-probability estimates for the test vector X.

predict_proba(X)

Return probability estimates for the test vector X.

score(X, y[, sample_weight])

Return accuracy on provided data and labels.

set_params(**params)

Set the parameters of this estimator.

set_score_request(*[, sample_weight])

Configure whether metadata should be requested to be passed to the score method.

fit(X, y)#

Fit the Mixed Naive Bayes classifier according to X, y.

Parameters:
Xarray-like of shape (n_samples, n_features)

Training vectors, where n_samples is the number of samples and n_features is the number of features.

yarray-like of shape (n_samples,)

Target values.

Returns:
selfobject

Returns the instance itself.

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

predict(X)#

Perform classification on an array of test vectors X.

Parameters:
Xarray-like of shape (n_samples, n_features)

The input samples.

Returns:
Cndarray of shape (n_samples,)

Predicted target values for X.

predict_log_proba(X)#

Return log-probability estimates for the test vector X.

Parameters:
Xarray-like of shape (n_samples, n_features)

The input samples.

Returns:
Carray-like of shape (n_samples, n_classes)

Returns the log-probability of the samples for each class.

predict_proba(X)#

Return probability estimates for the test vector X.

Parameters:
Xarray-like of shape (n_samples, n_features)

The input samples.

Returns:
Carray-like of shape (n_samples, n_classes)

Returns the probability of the samples for each class.

score(X, y, sample_weight=None)#

Return accuracy on provided data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MixedNB#

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

Examples using skbn.MixedNB#

MixedNB Equivalence with GaussianNB

MixedNB Equivalence with GaussianNB

Solving the XOR Problem: AnDE vs Naive Bayes

Solving the XOR Problem: AnDE vs Naive Bayes

Analytic View: 2D Slice of the 3D XOR Problem

Analytic View: 2D Slice of the 3D XOR Problem

MixedNB Equivalence with BernoulliNB

MixedNB Equivalence with BernoulliNB

MixedNB Equivalence with CategoricalNB

MixedNB Equivalence with CategoricalNB

3D Voxel-Cloud Visualization: XOR Structure

3D Voxel-Cloud Visualization: XOR Structure

The Power of Mixed Data: Context-Dependent Logic

The Power of Mixed Data: Context-Dependent Logic

Handling Mixed Data Types with MixedNB

Handling Mixed Data Types with MixedNB