User Guide#

This guide describes how each model of scikit-bayes is defined and estimated, and how to choose its parameters.

Introduction#

Bayesian network classifiers#

Bayesian network classifiers (BNCs) compute class probabilities with Bayes’ theorem. The simplest is Naive Bayes, which assumes the features independent given the class:

\[P(y|\mathbf{x}) \propto P(y) \prod_{i=1}^{n} P(x_i|y)\]

The assumption rarely holds, but Naive Bayes scales well and is a sound baseline. When the features depend strongly on each other, as in a parity problem like XOR, its probabilities are wrong.

What the library adds to scikit-learn#

The Naive Bayes classes of scikit-learn each take one type of feature, so a table with continuous, categorical and binary columns has to be split with a ColumnTransformer and the probabilities of the parts combined by hand; skbn.MixedNB takes the whole table. Beyond Naive Bayes, scikit-learn has no Bayesian network classifier that models dependencies between features. scikit-bayes adds the AnDE family (AODE, A2DE), TAN and KDB, and the models that learn discriminative weights on top of them, such as ALR and WeightedAnDE. Every estimator passes the scikit-learn estimator checks, so all of them work in pipelines, cross-validation and grid search.

MixedNB: Naive Bayes for mixed data#

skbn.MixedNB handles datasets with heterogeneous feature types by internally combining scikit-learn’s specialized Naive Bayes estimators.

A table of patients may hold the age (continuous, Gaussian), the sex (binary, Bernoulli) and the education level coded 0 to 3 (categorical). With scikit-learn this takes three Naive Bayes classifiers, one per type of feature, and a sum of their log-probabilities. MixedNB fits one model on the whole table.

Feature types#

MixedNB types every feature not declared by the user during fit(), from its training values. Every other estimator of the library uses the same rule through its class-level MixedNB, so a feature has the same type as a child and as a parent:

  • Ignored: constant features, which carry no information.

  • Bernoulli: features with exactly 2 unique values.

  • Categorical: non-negative integer-valued features with 3 to 20 unique values.

  • Gaussian: everything else, that is, fractional or negative values and integer values with more than 20 distinct values (ages, counts, amounts).

The rule only looks at the values, so declaring the types is the safe choice whenever they are known, either as indices or as a boolean mask:

from skbn import MixedNB

# Force features 2 and 3 to be categorical
clf = MixedNB(categorical_features=[2, 3])

# Force feature 1 to be Bernoulli
clf = MixedNB(bernoulli_features=[1])

# The same as the first one, with a boolean mask
clf = MixedNB(categorical_features=[False, False, True, True])

Each type is estimated as follows:

  • Categorical features are label-encoded on the values seen in training, so the codes may be any numbers (negative, sparse or non-integer). A value never seen in training contributes nothing to the prediction, as a missing value would.

  • Bernoulli features are binarised as x > t. When the training values lie on both sides of 0, t = 0 as in BernoulliNB; otherwise t is the midpoint of their range, so that two values such as {1, 2} stay apart.

  • Gaussian features with zero variance in training are ignored.

Labels and pandas categories#

Every estimator of the library also takes columns of labels: pandas category columns, whatever their categories, and columns of strings (in a DataFrame, or in a NumPy object or string array). Such a column is categorical whatever its values, even a category column with more than 20 integer categories; it may be declared Bernoulli but not Gaussian, and it is ignored when constant. Its labels are coded by their position among the sorted labels seen in training, so the model is exactly the one fitted on these codes declared categorical. The order of the categories of a category dtype is not used, and its categories absent from the training rows get no estimate. A label never seen in training is an unseen category, and categories_ holds the labels of each column.

import pandas as pd
from skbn import MixedNB

X = pd.DataFrame({
    "age": [23.0, 45.5, 31.2, 52.8, 38.1, 27.4],
    "plan": pd.Categorical(["basic", "pro", "pro", "basic", "pro", "basic"]),
    "city": ["Madrid", "Paris", "Paris", "Rome", "Madrid", "Rome"],
})
clf = MixedNB().fit(X, [0, 1, 1, 0, 1, 0])
print(clf.feature_types_)
# {'gaussian': [0], 'categorical': [1, 2], 'bernoulli': []}

A column that holds labels in fit must hold labels in predict (strings and categories may be swapped), and the other columns must hold numbers. Missing values (NaN, None or pandas missing values) are supported natively in both fit and predict: during training each conditional is estimated from the rows where its variables are observed, and during prediction missing attributes are marginalised out.

Example#

import numpy as np
from skbn import MixedNB

# Features: [Gaussian, Bernoulli, Categorical]
X = np.array([
    [1.5, 0, 0],
    [2.3, 1, 1],
    [0.8, 1, 2],
    [1.1, 0, 0],
    [3.2, 1, 1],
    [-0.5, 0, 2]
])
y = np.array([0, 1, 1, 0, 1, 0])

clf = MixedNB(alpha=1.0)
clf.fit(X, y)

# Inspect detected types
print(clf.feature_types_)
# {'gaussian': [0], 'categorical': [2], 'bernoulli': [1]}

# Predict
print(clf.predict([[1.0, 1, 1]]))  # [1]
print(clf.predict_proba([[1.0, 1, 1]]))  # [[0.14 0.86]]

Parameters#

  • alpha: Laplace smoothing of the categorical and Bernoulli features. Default: 1.0

  • var_smoothing: variance smoothing of the Gaussian features. Default: 1e-9

  • categorical_features: indices or boolean mask of the categorical features

  • bernoulli_features: indices or boolean mask of the Bernoulli features

  • gaussian_features: indices or boolean mask of the Gaussian features

skbn.WeightedNB adds discriminative exponent weights to skbn.MixedNB (WANBIA-C at weight_level=1, weighted CCBN on Naive Bayes at weight_level=3), trained by maximising the conditional log-likelihood with L-BFGS-B. Like TAN and KDB, both MixedNB and WeightedNB expose conditional_table(feature) and to_dot(), and WeightedNB exposes prior_weights_, feature_weights_ and weight_table(feature).

The AnDE family#

The Averaged n-Dependence Estimators (AnDE) relax the independence assumption by conditioning every feature on the class and on n “super-parent” features.

Where Naive Bayes fails#

Consider the XOR problem:

X1

X2

Y

-1

-1

0

-1

+1

1

+1

-1

1

+1

+1

0

Looking at X1 alone: P(X1|Y=0) = P(X1|Y=1) (symmetric distributions). Looking at X2 alone: P(X2|Y=0) = P(X2|Y=1) (symmetric distributions).

Naive Bayes cannot learn this problem, and its accuracy stays at chance level. AnDE models P(X2 | Y, X1) instead of P(X2 | Y), and the dependence on X1 is what separates the classes.

Super-parents#

An SPnDE (Super-Parent n-Dependence Estimator) conditions every child feature on the class and on n parent features:

\[P(y, \mathbf{x}) = P(Y^*) \prod_{i \in children} P(x_i | Y^*)\]

where \(Y^* = (y, x_{p1}, x_{p2}, ..., x_{pn})\) is the augmented super-class. AnDE averages the SPnDEs of every set of n parents.

AnDE#

skbn.AnDE is the standard generative model described by Webb et al. [1] [2].

\[P(y|\mathbf{x}) \propto \sum_{m} P_m(y, \mathbf{x})\]

Main parameters:

  • n_dependence: order of dependence. 0 is Naive Bayes (skbn.MixedNB with its defaults: var_prior_weight shrinks the variances of the components that have super-parents, and the one of order 0 has none), 1 is AODE and 2 is A2DE.

  • n_bins: number of bins of the continuous super-parents.

  • strategy: how those bins are placed ('uniform', 'quantile', 'kmeans', or 'mdlp', which also chooses how many: see Supervised discretization).

Example:

from skbn import AnDE

# AODE (n=1)
clf = AnDE(n_dependence=1, n_bins=5)
clf.fit(X, y)

# A2DE (n=2): more components, and more data needed per estimate
clf = AnDE(n_dependence=2, n_bins=5)

With mean="geometric" the components are pooled with the geometric mean, \(P(y|\mathbf{x}) \propto \prod_{m} P_m(y, \mathbf{x})^{1/|M|}\). Every component then counts the same, so a few components that give a class a probability close to zero rule it out. This pool of SPnDEs is not the AnJE of Zaidi et al. [4], which is described below.

WeightedAnDE#

skbn.WeightedAnDE learns discriminative weights on top of AnDE by maximising the conditional log-likelihood with L-BFGS-B. The weights start at, and are regularized towards, the generative model (w = 1). Two parameters define the model:

  • mean: how the components are pooled ("arithmetic" or "geometric").

  • weight_scope: what each weight multiplies.

    • "component": a whole SPnDE. With the arithmetic mean the weights are mixture coefficients, \(\log \sum_m w_m P_m(y, \mathbf{x})\); with the geometric mean they scale the log-joints, \(\sum_m w_m \log P_m(y, \mathbf{x})\).

    • "factor": every factor of every SPnDE, as in the weighted CCBN of Zaidi et al. [3]: \(P_m^w = \prod_f \theta_f^{w_f}\).

weight_level sets the variables that index each weight:

Level

Component Weights

Factor Weights [3]

1

Component

Factor

2

Component × parent value

Factor × value

3

Component × class

Factor × value × class

4

Component × class × parent value

Factor × value × class × parents

The factor levels are the nested parameterizations of Zaidi et al. [3] (Sec. 5.4), applied inside each SPnDE. Arithmetic component weights at level 3 are the per-SPODE, per-class weights of EODE-CLL [5]. The other component levels follow the same nesting at the level of the component; they are a choice of this library, not of the literature.

Component weights are cheap (one weight per component and sample) but only decide how much each SPnDE is trusted: they cannot correct a dependence between children that every SPnDE shares, such as a duplicated attribute. Factor weights can, at the cost of about n_features times more weights. With the geometric mean, and with modular=True (each component fitted on its own conditional log-likelihood), the objective is convex.

average decides what the arithmetic mean pools: the weighted joints \(P^w_m(y, \mathbf{x})\), as AnDE does ("joint", the default), or the components’ posteriors \(P^w_m(y | \mathbf{x})\) ("posterior"). The two coincide for the geometric mean. Factor weights at level 4, fitted with modular=True and pooled with average="posterior", are the AnDEw of FedAnDE: one weighted CCBN per SPnDE, trained on its own data and averaged, which is what lets each component be fitted across sites.

from skbn import WeightedAnDE

# One weight per SPODE and class
clf = WeightedAnDE(n_dependence=1, weight_level=3)

# Weighted CCBN inside each SPODE
clf = WeightedAnDE(n_dependence=1, weight_scope="factor", weight_level=3)

# AnDE^w of FedAnDE
clf = WeightedAnDE(
    n_dependence=1, weight_scope="factor", weight_level=4,
    modular=True, average="posterior",
)
clf.fit(X, y)

AnJE and ALR#

skbn.AnJE and skbn.ALR follow Zaidi et al. [4]. Instead of SPnDEs they use n-joins, the joint distributions \(P(x_\alpha | y)\) of every subset \(\alpha\) of n_join attributes. AnJE pools them with the geometric-mean weight of the paper:

\[P(y|\mathbf{x}) \propto P(y) \prod_{\alpha} P(x_\alpha | y)^{\frac{(n-1)!(a-n)!}{(a-1)!}}\]

ALR (ALRn in the paper) learns one weight per n-join, value and class by maximising the conditional log-likelihood, a convex problem:

\[\log P(y|\mathbf{x}) = w_y \log P(y) + \sum_{\alpha} w_{y,\alpha,x_\alpha} \log P(x_\alpha | y) - \log Z(\mathbf{x})\]

A1JE is Naive Bayes, and AnJE uses the same statistics as A(n-1)DE, so AnJE(n_join=2) is comparable to AnDE(n_dependence=1). Both models are defined on discrete attributes: continuous attributes are discretized into n_bins bins. The paper uses MDL discretization instead, which is strategy="mdlp" (see Supervised discretization). Probabilities are m-estimates with m = alpha. A value never seen in training says nothing about the class, so the n-joins that contain it are left out for that sample.

weight_level=3 (the default) is the model of the paper. Levels 1 (one weight per n-join) and 2 (per n-join and value) tie the weights following the nesting of [3]. The other defaults reproduce the settings of the code released with the paper: weights start at w = 1, there is no penalty, and L-BFGS-B keeps 5 corrections and runs until the relative reduction of the objective falls below 1e7 machine epsilons (its factr = 1e7) or for 104 iterations, with the test on the projected gradient disabled. The starting point is not the AnJE but the unaveraged product of all n-joins, which counts every attribute once per join that holds it and is overconfident. On large samples the weights correct it; on small ones the training set is fitted before they do, and the probabilities stay close to 0 and 1 (the labels are much less affected). l2_reg adds the penalty towards w = 1 that the paper suggests for small datasets: it limits the overfitting, but it pulls towards that same starting point, not towards the AnJE, so it does not make the probabilities of a small sample reliable; AnJE, WeightedAnDE or a weighted network do better there. max_iter and tol stop the optimizer earlier.

from skbn import ALR, AnJE

clf = AnJE(n_join=2)
clf = ALR(n_join=2)  # as in Zaidi et al. (2016)
clf.fit(X, y)

Gaussian children#

By the chain rule every (n+1)-join is a product of the factors of an SPnDE, \(P(x_\alpha | y) = P(x_P | y)\, P(x_j | y, x_P)\) for each \(j \in \alpha\) and \(P = \alpha \setminus \{j\}\). Averaging these orders and weighting the joins as in AnJE gives a log-linear pool of the factors of the SPnDEs of order n,

\[\log P(y|\mathbf{x}) \propto \log P(y) + \frac{1}{|M|} \sum_{P} \Big[ \tfrac{d}{n+1} \log P(\mathbf{x}_P | y) + \tfrac{d}{(n+1)(d-n)} \sum_{j \notin P} \log P(x_j | y, \mathbf{x}_P) \Big].\]

skbn.GaussianAnJE builds AnJE on these factors, and skbn.GaussianALR puts one weight per factor, value, class and parent instantiation on them, as ALRn does on the n-joins. Since the factors are those of skbn.AnDE, both models handle mixed data as HAODE does: only the super-parents are discretized, and continuous children keep their Gaussian conditionals. n_dependence is the order of the SPnDEs, so GaussianAnJE(n_dependence=1) is the counterpart of AnJE(n_join=2). Probabilities are Laplace estimates on the augmented states, and the super-parent factor is divided by its total within the class so that every factor is conditional on the class. On discrete data the two versions coincide as the smoothing vanishes, and GaussianALR at w = 1 is GaussianAnJE.

from skbn import GaussianALR, GaussianAnJE

clf = GaussianAnJE(n_dependence=1)  # A2JE with Gaussian children
clf = GaussianALR(n_dependence=1)  # ALR^2 with Gaussian children
clf.fit(X, y)

TAN: Tree Augmented Naive Bayes#

skbn.TAN [6] relaxes the independence assumption in a different way from AnDE: instead of averaging many models, it learns a single network in which every attribute has the class and at most one other attribute as parents.

\[P(y, \mathbf{x}) = P(y) \, P(x_r | y) \prod_{i \neq r} P(x_i | y, x_{\pi(i)})\]

Where \(r\) is the root of the tree and \(\pi(i)\) the parent of attribute \(i\).

Learning the tree#

The structure is learned with the Chow-Liu procedure [7]:

  1. Compute the conditional mutual information \(I(A_i; A_j | C)\) between every pair of attributes.

  2. Build the maximum weight spanning tree of the complete graph with these weights.

  3. Choose a root (root) and direct every edge away from it.

The choice of root does not change which attributes are connected, only the direction of the edges.

Mixed data#

TAN handles mixed data as the AnDE family does. Continuous attributes are discretized (n_bins, strategy) to compute the mutual information and when they act as parents; as children they remain continuous and are modeled by Gaussian distributions. Every conditional \(P(x_i | y, x_{\pi(i)})\) is Naive Bayes on the augmented class \((y, x_{\pi(i)})\), with the estimates of skbn.MixedNB, so discrete children use Laplace smoothing (alpha).

Main parameters:

  • root: index of the root attribute. Default: 0

  • alpha: Laplace smoothing of the discrete attributes. Default: 1.0

  • n_bins: number of bins of the continuous attributes

  • strategy: how those bins are placed ('uniform', 'quantile', 'kmeans', 'mdlp')

Example:

from skbn import TAN

clf = TAN()
clf.fit(X, y)

clf.parents_                   # parent of each attribute (-1 for the root)
clf.conditional_mutual_info_   # edge weights used to learn the tree
clf.edges_                     # the arcs as (parent, child) pairs
print(clf.to_dot())            # the network, for Graphviz or networkx
clf.conditional_table(0)       # P(x_0 | class, parents), with their values
clf.cut_points_                # the bins of the continuous attributes

KDB: K-dependence Bayesian classifier#

skbn.KDB [8] generalizes Naive Bayes and TAN with a single parameter: every attribute has the class and at most k other attributes as parents.

\[P(y, \mathbf{x}) = P(y) \prod_{i} P(x_i | y, \mathbf{x}_{\Pi(i)}), \qquad |\Pi(i)| \leq k\]

Where \(\Pi(i)\) is the set of attribute parents of attribute \(i\). With k=0 the model is Naive Bayes: skbn.MixedNB with the same var_prior_weight, which is 1 by default in KDB and 0 in MixedNB.

Learning the structure#

The structure is learned greedily [8]:

  1. Compute the mutual information \(I(A_i; C)\) between every attribute and the class, and the conditional mutual information \(I(A_i; A_j | C)\) between every pair of attributes.

  2. Add the attributes to the network in decreasing order of \(I(A_i; C)\).

  3. Each attribute takes as parents the \(\min(k, t)\) attributes, among the \(t\) already added, with the highest \(I(A_i; A_j | C)\).

Mixed data is handled as in TAN: continuous attributes are discretized (n_bins, strategy) for the structure and when they act as parents, and every conditional \(P(x_i | y, \mathbf{x}_{\Pi(i)})\) is Naive Bayes on the augmented class \((y, \mathbf{x}_{\Pi(i)})\).

Choosing k#

Larger values of k capture interactions between more attributes, but the number of cells of the conditional probability tables grows exponentially with k, so they need more data to be estimated. k=1 (the default) gives a network of the same complexity as TAN; k=2 or more pays off with large training sets. Choose k by cross-validation.

Main parameters:

  • k: maximum number of attribute parents. Default: 1

  • alpha: Laplace smoothing of the discrete attributes. Default: 1.0

  • n_bins: number of bins of the continuous attributes

  • strategy: how those bins are placed ('uniform', 'quantile', 'kmeans', 'mdlp')

Example:

from skbn import KDB

clf = KDB(k=2)
clf.fit(X, y)

clf.feature_order_             # order in which the attributes were added
clf.parents_                   # attribute parents of each attribute
clf.mutual_info_               # I(A_i; C), used to order the attributes
clf.conditional_mutual_info_   # I(A_i; A_j | C), used to choose parents
clf.edges_                     # the arcs as (parent, child) pairs
print(clf.to_dot())            # the network, for Graphviz or networkx
clf.conditional_table(0)       # P(x_0 | class, parents), with their values
clf.cut_points_                # the bins of the continuous attributes

Weighted TAN and KDB#

skbn.WeightedTAN and skbn.WeightedKDB are the weighted conditionally Bayesian network (wCCBN) of Zaidi et al. [3] on the networks of TAN and KDB. The structure and the probabilities are learned as in skbn.TAN and skbn.KDB and stay fixed; every factor of the joint, the class prior and the conditional of every attribute, gets an exponent weight:

\[\log P^w(y | \mathbf{x}) = w_y \log P(y) + \sum_{i} w_{i, x_i, y, \mathbf{x}_{\Pi(i)}} \log P(x_i | y, \mathbf{x}_{\Pi(i)}) - \log Z(\mathbf{x})\]

The weights maximise the conditional log-likelihood of the training set with L-BFGS-B, a convex problem. They start at, and are regularized towards (l2_reg), the generative model w = 1, which gives the predictions of TAN or KDB. weight_level ties the weights with the nesting of the factor weights of skbn.WeightedAnDE (see its table above): one weight per factor (1), per factor and value (2), per factor, value and class (3), or per factor, value, class and parent instantiation (4). Level 4, one weight per parameter of the network, is the wCCBN of the paper and the default. The value of a Gaussian child is not discretized, so its weights ignore the value. Only the combinations seen in training get a weight; any other one, such as a parent value never seen, keeps the generative weight 1.

The weights help when the data break the independence assumptions of the network. A tree or k parents explain only part of the dependencies between correlated attributes: the conditional of the copy of an attribute given that attribute is close to 1 for a discrete copy, but a continuous copy is Gaussian given the bin of its parent and still repeats part of the evidence. The generative model then counts that evidence several times and is overconfident; the weights scale the repeated conditionals down. With few samples per weight, the finer levels overfit unless l2_reg is large enough; level 1 or 2 is the safer choice on small datasets.

Every evaluation of the objective visits each factor of each training sample for every class, \(O(n \, (d + 1) \, |C|)\) operations, and the fit keeps these log-likelihoods in memory, about \(12\, n\, (d + 1)\, |C|\) bytes (54 MB for 60,000 samples, 24 attributes and 3 classes). Predictions cost about as much as those of TAN and KDB.

A weighted network is a single component, so it is the geometric skbn.WeightedAnDE with weight_scope="factor" reduced to one SPnDE: WeightedKDB(k=0) and WeightedAnDE(n_dependence=0, weight_scope="factor", mean="geometric") fit the same weights (with var_prior_weight=0; otherwise the Gaussian variances of the two Naive Bayes models differ). WeightedAnDE weights the factors of \(d\) SPnDEs, about \(d\) times more weights and work for the same number of attributes. skbn.ALR is the same idea on the n-joins of AnJE instead of the conditionals of a network.

from skbn import WeightedKDB, WeightedTAN

clf = WeightedTAN()  # wCCBN on the TAN structure
clf = WeightedKDB(k=2, weight_level=1, l2_reg=10.0)  # one weight per factor
clf.fit(X, y)

clf.learned_weights_   # the weights, 1 for the generative model
clf.prior_weights_     # those of the class prior
clf.feature_weights_   # those of every attribute: below 1, it is discounted
clf.weight_table(0)    # those of x_0, with the values they belong to

Supervised discretization#

By default the models discretize their continuous attributes with KBinsDiscretizer (n_bins, strategy), which does not look at the class. The entropy discretization of Fayyad and Irani (1993), the usual choice in the AnDE and ALR literature, places the cut-points where the class changes instead. It splits each attribute recursively at the cut-point of least class entropy and stops when the gain no longer pays for the cut under the MDL criterion, so the number of bins is not fixed and n_bins is not used. An attribute without any accepted cut becomes constant, and the models then ignore it wherever it was to be discretized.

Every model takes it as strategy="mdlp". It applies where the model discretizes: to every continuous attribute in skbn.AnJE and skbn.ALR, and to the super-parents and the structure in the other models, whose continuous children stay Gaussian.

from skbn import ALR, TAN

# ALR^2 with the discretization of Zaidi et al. (2016)
clf = ALR(n_join=2, strategy="mdlp")

# TAN whose tree is learned on MDL intervals, as in Friedman et al. (1997)
clf = TAN(strategy="mdlp")
clf.fit(X, y)

Discretized children#

By default a continuous attribute is discretized only where it is a parent, and keeps its values and a Gaussian conditional as a child, as in HAODE. The original AODE, TAN and KDB are defined on discrete attributes instead, and are run on continuous data after discretizing it. AnDE, TAN, KDB and their weighted versions do that with continuous_children="discretized": every continuous attribute is replaced by its bin, of n_bins and strategy, as a child and as a parent, and the other attributes are left as they are.

from skbn import AnDE, WeightedTAN

# AODE on MDL intervals, as in Webb et al. (2005)
clf = AnDE(strategy="mdlp", continuous_children="discretized")

# The weighted TAN on the same intervals
clf = WeightedTAN(strategy="mdlp", continuous_children="discretized")
clf.fit(X, y)

Which one fits better depends on the data, and cross-validation decides. A Gaussian conditional needs few samples and suits attributes that are close to Gaussian within each class; it is wrong for skewed or multimodal ones, and a model with many such children gives overconfident probabilities. Bins make no assumption on the shape and cost a parameter per bin. In 5-fold cross-validation repeated twice (accuracy / log loss):

Model

iris

wine

breast cancer

AnDE()

0.957 / 0.107

0.975 / 0.140

0.941 / 0.376

AnDE(strategy="mdlp", continuous_children="discretized")

0.927 / 0.171

0.980 / 0.077

0.956 / 0.190

TAN()

0.960 / 0.096

0.955 / 0.394

0.957 / 0.232

TAN(strategy="mdlp", continuous_children="discretized")

0.927 / 0.177

0.963 / 0.112

0.949 / 0.189

On the 150 samples of iris the Gaussians win; on wine and on the 30 skewed measurements of the breast cancer data the intervals halve the log loss.

The same discretization is available as a transformer, skbn.MDLPDiscretizer, together with skbn.DecisionTreeDiscretizer, which uses the thresholds of a decision tree fitted on each attribute, with at most n_bins leaves. In a pipeline they discretize every attribute they are given, so the model that follows has no continuous children; continuous_children="discretized" gives the same model without the pipeline, and leaves the columns of labels alone.

Warning

Declare the discretized attributes as categorical_features in the model that follows a discretizer. The models type an undeclared attribute from its values, and integer codes with more than 20 distinct values are taken as a continuous attribute and discretized again. MDL discretization produces that many intervals on large samples.

from sklearn.pipeline import make_pipeline
from skbn import AnDE, MDLPDiscretizer

# A1DE on discretized attributes only (here X has 10 columns)
clf = make_pipeline(
    MDLPDiscretizer(),
    AnDE(n_dependence=1, categorical_features=range(10)),
)
clf.fit(X, y)

Choosing a model#

The table sums up the sections above, with d attributes and n training samples. The generative models are fitted by counting; the weighted ones then run L-BFGS-B on the conditional log-likelihood, starting at the generative model.

Model

What it assumes or captures

Training cost

When to use it

MixedNB / WeightedNB

Features independent given the class; WeightedNB adds discriminative exponent weights (WANBIA / wCCBN) to discount correlated or duplicated features.

Counting (one estimate per feature and class); WeightedNB then runs L-BFGS-B on a convex objective.

A fast baseline that scales well; WeightedNB corrects overconfidence from correlated features without learning a graph.

AnDE, n_dependence=1 (AODE)

Every child conditioned on the class and one super-parent, averaged over the d super-parents.

Counting, d components.

The usual default: it captures pairwise dependencies and its estimates need little data.

AnDE, n_dependence=2 (A2DE)

Every child conditioned on the class and two super-parents.

Counting, \(d(d-1)/2\) components, each estimated on more cells.

Large training sets; orders of 3 or more rarely pay for their cost.

WeightedAnDE

The SPnDEs of AnDE with discriminative weights on each component or on each factor.

AnDE, then L-BFGS-B; a step costs one weight per component, or per factor, for each sample.

To learn how much each SPnDE is trusted (component weights), or to correct a dependence between children that every SPnDE shares, such as a duplicated attribute (factor weights). Factor weights overfit small datasets with a weak l2_reg.

AnJE / ALR

Joint distributions of every subset of n_join attributes given the class; ALR adds one weight per n-join, value and class (a convex problem). Continuous attributes are discretized.

Counting of the \(\binom{d}{n}\) n-joins; ALR then runs L-BFGS-B with one weight per n-join touched by each sample.

Discrete data, or data discretized with MDLPDiscretizer as in Zaidi et al. [4]. ALR needs large samples: on small ones its probabilities are overconfident, with or without l2_reg.

GaussianAnJE / GaussianALR

The same pool as AnJE, written on the factors of the SPnDEs of order n_dependence: only the super-parents are discretized and continuous children stay Gaussian.

Counting as AnDE of the same order; GaussianALR then runs L-BFGS-B with one weight per factor, value, class and parent instantiation.

Mixed data where continuous attributes should not lose their values as children, as in HAODE.

TAN

A single network in which every attribute has the class and at most one other attribute as parents (a tree).

Conditional mutual information of every pair of attributes, time growing with \(n d^2\), then counting.

A single network whose structure can be read (parents_), instead of an average of many; KDB with k=1 has the same complexity.

KDB

A single network in which every attribute has the class and at most k other attributes as parents.

As TAN, plus the mutual information with the class; the tables grow exponentially with k.

Interactions between more than two attributes; k=2 or more pays off with large training sets. Choose k by cross-validation.

WeightedTAN / WeightedKDB

The network of TAN or KDB with an exponent weight on the class prior and on every conditional (see Weighted TAN and KDB).

TAN or KDB, then L-BFGS-B; a step costs one weight per factor for each sample and class.

Correlated or duplicated attributes that the network counts several times. On small samples the weights can overfit: compare with the generative model by cross-validation.

Choosing the parameters#

Order of dependence#

AnDE of order n has one component for every set of n attributes, \(\binom{d}{n}\) of them over d attributes, and each component estimates its children on more cells as n grows.

  • n=1 (AODE) is the usual default: d components, and its estimates need little data.

  • n=2 (A2DE) has \(d(d-1)/2\) components (1,225 with 50 attributes) and pays off on large training sets. Components whose parents are constant in training share their tables, so the cost grows with the informative attributes. On a laptop, A2DE fits 90% of Census-Income (41 attributes) in about 9 s and of Covertype (54 attributes, 520,000 rows) in about 40 s.

  • n>=3 rarely pays for its cost, which grows combinatorially.

Discretization#

The super-parents of AnDE and the attributes of AnJE, ALR, TAN and KDB are discretized when continuous:

  • 'quantile' (default): bins of equal frequency, robust to outliers.

  • 'uniform': bins of equal width.

  • 'kmeans': bins around the clusters of one-dimensional k-means.

  • 'mdlp': cuts where the class changes, as many as the MDL criterion accepts (see Supervised discretization).

n_bins is usually between 3 and 10: more bins give finer cuts but fewer samples per estimate. 'mdlp' chooses the number of bins of each attribute itself: many on large samples of an informative attribute, a single one for an attribute that says nothing about the class on its own.

Regularization of the weights#

l2_reg is the strength of the penalty \(\lambda \|\mathbf{w} - 1\|^2\) added to the summed conditional log-likelihood, so its effect weakens as the training set grows. The default of skbn.WeightedAnDE, skbn.WeightedTAN and skbn.WeightedKDB is 1.0. Component weights are few and barely react to it; factor weights are many and overfit small datasets with a weak penalty, so for them values between 0.1 and 10, chosen by cross-validation, are the safe range.

Computational considerations#

  • The counting and the evaluation of the SPnDEs, of the n-joins, of the weighted objectives and of the conditionals of TAN and KDB run in compiled Numba kernels. They are compiled on the first use of each model and cached on disk: after installing or upgrading the package, the first fit of a model takes longer (on a laptop, a few seconds for MixedNB, AnJE or TAN, and 15 to 30 s for AnDE and WeightedAnDE), and later sessions load the compiled code in a fraction of a second.

  • n_jobs sets the number of threads of those kernels (1 by default, -1 for all processors). The threaded version of a kernel is compiled the first time it is used, so a model run with several threads for the first time pays that compilation once more. The weights of the weighted models are optimized by L-BFGS-B in a single thread, with the objective of ALR evaluated in n_jobs threads.

  • TAN and KDB compute the conditional mutual information of every pair of attributes: the time grows with \(n d^2\) and the matrix conditional_mutual_info_ holds \(d^2\) values (800 MB for 10,000 attributes).

  • The parameters are stored only for the augmented states and the cells seen in training, so the memory of a model is bounded by the data rather than by the cardinalities of the attributes.

  • The cost of an optimization step grows with the number of weights each sample touches: one per component with component weights, one per factor with factor weights and one per n-join in ALR.

What the models do not handle#

  • Incremental learning. There is no partial_fit: the bins of the continuous attributes, the categories and the structure are chosen on the whole training set.

  • Sparse input. The models take dense arrays and DataFrames.

Missing values and sample weights#

  • Missing values. Every classifier accepts NaN (and None or pandas missing values in label columns) in both fit and predict. During training each distribution is estimated from the rows where its variables are observed (available-case estimation). At prediction time a missing child attribute contributes nothing (it is marginalised out), and a missing parent behaves like a category never seen in training: SPnDEs whose super-parent is missing are left out of the pool, n-joins containing a missing attribute are left out, and in TAN/KDB a child with a missing parent falls back to its uninformed estimate.

  • Sample weights. Every estimator (classifiers and discretizers) accepts non-negative sample_weight in fit(X, y, sample_weight=...), used for frequency counts, Gaussian moments, discretization (MDL, decision tree, quantile and k-means), mutual information and the discriminative log-likelihood objective. Rows with weight 0 are ignored.

References#