Naive Bayes

The Paper Zoo includes a lightweight spflow.zoo.NaiveBayes wrapper that builds a Naive Bayes model from standard SPFlow leaf modules.

Overview

NaiveBayes supports two common modes:

  • Density estimation with a single output channel, represented as leaf -> Product.

  • Classification with one output channel per class and a learned or fixed class prior, represented as leaf -> Product -> Sum.

This makes it easy to reuse existing SPFlow leaves while keeping the familiar Naive Bayes factorization.

Minimal Example

import torch
from spflow.meta.data.scope import Scope
from spflow.modules.leaves import Bernoulli
from spflow.zoo import NaiveBayes

leaf = Bernoulli(
    scope=Scope([0, 1]),
    probs=torch.tensor([[[0.25]], [[0.75]]]),
)
model = NaiveBayes(leaf)

x = torch.tensor([[0.0, 1.0], [1.0, 0.0]])
ll = model.log_likelihood(x)

Classifier Example

import torch
from spflow.meta.data.scope import Scope
from spflow.modules.leaves import Bernoulli
from spflow.zoo import NaiveBayes

leaf = Bernoulli(
    scope=Scope([0]),
    probs=torch.tensor([[[0.8], [0.2]]]),
)
model = NaiveBayes(leaf, num_classes=2, class_prior=torch.tensor([0.75, 0.25]))

x = torch.tensor([[1.0], [0.0]])
probs = model.predict_proba(x)

API Reference

class spflow.zoo.NaiveBayes(leaf_modules, *, num_classes=1, class_prior=None, learnable_prior=False)[source]

Bases: Module, Classifier

Naive Bayes model built from SPFlow leaves and a product root.

Density estimation is represented by leaf layer -> Product. Classification is represented by leaf layer -> Product -> Sum where the product captures p(x | y) across output channels and the root sum stores the class prior.

log_likelihood(data, cache=None)[source]

Compute log likelihood P(data | module).

Computes log probability of input data under this module’s distribution. Uses log-space for numerical stability. Results should be cached for efficiency.

Parameters:
  • data (Tensor) – Input data of shape (batch_size, num_features). NaN values indicate missing values to marginalize over.

  • cache (Cache | None, optional) – Cache for intermediate computations. Defaults to None.

Returns:

Log-likelihood of shape (batch_size, out_features, out_channels).

Return type:

Tensor

Raises:

ValueError – If input data shape is incompatible with module scope.

log_posterior(data, cache=None)[source]
Return type:

Tensor

marginalize(marg_rvs, prune=True, cache=None)[source]

Structurally marginalize out specified random variables from the module.

Computes a new module representing the marginal distribution by integrating out the specified variables from the structure. For data-level marginalization, use NaNs in log_likelihood inputs.

Parameters:
  • marg_rvs (list[int]) – Random variable indices to marginalize out.

  • prune (bool, optional) – Whether to prune unnecessary modules during marginalization. Defaults to True.

  • cache (Cache | None, optional) – Cache for intermediate computations. Defaults to None.

Returns:

Marginalized module, or None if all variables are marginalized out.

Return type:

Module | None

Raises:

ValueError – If marginalization variables are not in the module’s scope.

maximum_likelihood_estimation(data, *, targets=None, bias_correction=True, nan_strategy=None)[source]
Return type:

None

predict_proba(data)[source]

Predict class probabilities for input data.

Parameters:

data (Tensor) – Input data tensor.

Return type:

Tensor

Returns:

Class probability predictions. Each row corresponds to a data point, and each column corresponds to a class.

property feature_to_scope: ndarray

Mapping from output features to their respective scopes.

Returns:

2D-array of scopes. Each row corresponds to an output feature,

each column to a repetition.

Return type:

np.ndarray[Scope]

property n_out: int