Interfaces

Abstract base classes defining standard interfaces for SPFlow modules.

Classifier

Abstract base class for modules that support classification.

class spflow.interfaces.classifier.Classifier[source]

Bases: ABC

Abstract base class for modules that support classification.

Provides a standard interface for models that can predict class labels and class probabilities from input data.

predict(data)[source]

Predict class labels for input data.

Parameters:

data (Tensor) – Input data tensor.

Return type:

Tensor

Returns:

Predicted class labels.

abstractmethod 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.

sklearn Wrappers

scikit-learn compatible wrappers for density estimation and classification.

class spflow.interfaces.sklearn.SPFlowDensityEstimator(model=None, *, structure_learner='learn_spn', structure_learner_kwargs=None, fit_params=True, leaf='normal', leaf_out_channels=1, min_instances_slice=100, min_features_slice=2, device=None, dtype=None, channel_agg='logmeanexp', repetition_agg='logmeanexp')[source]

Bases: BaseEstimator, DensityMixin

scikit-learn compatible density estimator for SPFlow models.

Supports two workflows: - Structure learning: learn a model from data via learn_spn or learn_prometheus. - Parameter fitting: fit parameters of a provided SPFlow model via MLE.

Parameters:
  • model (Module | None) – Optional SPFlow model to fit and use for scoring/sampling.

  • structure_learner (Literal['learn_spn', 'prometheus']) – “learn_spn” or “prometheus”. Only used when model is None.

  • structure_learner_kwargs (dict[str, Any] | None) – Keyword arguments forwarded to the structure learner.

  • fit_params (bool) – If True and model is provided, run MLE (maximum_likelihood_estimation) in fit.

  • leaf (Literal['normal']) – Leaf family used when learning structure and model is None. Currently supports “normal”.

  • leaf_out_channels (int) – Output channels for the leaf module template (passed to Normal).

  • min_instances_slice (int) – Stopping criterion for structure learning (forwarded if not overridden).

  • min_features_slice (int) – Stopping criterion for structure learning (forwarded if not overridden).

  • device (str | None) – Torch device string (e.g., “cpu”, “cuda”). If None, uses model device or the active PyTorch default device.

  • dtype (Optional[Literal['float32', 'float64']]) – Torch dtype string (“float32”, “float64”) for inputs.

  • channel_agg (Literal['logmeanexp', 'logsumexp', 'first']) – How to aggregate multiple output channels into a scalar log-likelihood.

  • repetition_agg (Literal['logmeanexp', 'logsumexp', 'first']) – How to aggregate multiple repetitions into a scalar log-likelihood.

fit(X, y=None)[source]

Fit a density model.

Parameters:
  • X (Any) – Array-like of shape (n_samples, n_features).

  • y (Any | None) – Ignored. Present for scikit-learn compatibility.

Return type:

SPFlowDensityEstimator

sample(n_samples=1, *, random_state=None)[source]

Generate samples from the fitted model.

Parameters:
  • n_samples (int) – Number of samples to draw.

  • random_state (int | None) – Optional seed for deterministic sampling.

Return type:

ndarray

score_samples(X)[source]

Compute per-sample log-likelihood under the fitted model.

Return type:

ndarray

class spflow.interfaces.sklearn.SPFlowClassifier(model, *, device=None, dtype=None)[source]

Bases: BaseEstimator, ClassifierMixin

scikit-learn compatible classifier wrapper for SPFlow classifiers.

This wrapper delegates to a provided SPFlow model that implements predict_proba(torch.Tensor) -> torch.Tensor.

fit(X, y)[source]

Store class labels for sklearn compatibility.

Return type:

SPFlowClassifier

predict(X)[source]

Predict class labels using argmax over predicted probabilities.

Return type:

ndarray

predict_proba(X)[source]
Return type:

ndarray

set_score_request(*, sample_weight='$UNCHANGED$')

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.