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,ClassifierNaive Bayes model built from SPFlow leaves and a product root.
Density estimation is represented by
leaf layer -> Product. Classification is represented byleaf layer -> Product -> Sumwhere the product capturesp(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.
- 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_likelihoodinputs.- 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: