Getting Started#

Installation#

Install scikit-bayes from PyPI with pip:

pip install scikit-bayes

Installation gives the other ways (the development version, from source) and explains why the first fit of each model takes longer.

First models#

MixedNB on mixed data#

skbn.MixedNB takes Gaussian (continuous), categorical and Bernoulli (binary) features in one table, and infers the type of each one from its values:

import numpy as np
from skbn import MixedNB

# Columns: Gaussian, Bernoulli, categorical
X = np.array([
    [0.5, 0, 0],
    [-1.2, 1, 1],
    [0.6, 1, 2],
    [-0.1, 0, 0],
])
y = np.array([0, 1, 1, 0])

clf = MixedNB()
clf.fit(X, y)
print(clf.predict([[-0.5, 1, 1]]))  # [1]

A pandas DataFrame may also hold labels, as strings or category columns, which are categorical:

import pandas as pd

df = pd.DataFrame({
    "x": [0.5, -1.2, 0.6, -0.1],
    "colour": ["red", "blue", "blue", "red"],
})
print(MixedNB().fit(df, y).predict(df))  # [0 1 1 0]

AnDE on XOR#

skbn.AnDE relaxes the independence assumption of Naive Bayes, so it learns dependencies between features such as XOR, where the class depends on whether the two features have the same sign:

import numpy as np
from skbn import AnDE

X = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]])
y = np.array([0, 1, 1, 0])  # 0 for the same sign, 1 otherwise

# MixedNB predicts one class everywhere; AnDE gets all four right
clf = AnDE(n_dependence=1, n_bins=2)
clf.fit(X, y)
print(clf.predict(X))  # [0 1 1 0]

Development setup#

The development environments are managed with pixi. After installing pixi, the common tasks are:

Run tests:

pixi run test

Run linter:

pixi run lint

Build documentation:

pixi run build-doc

Activate development environment:

pixi shell -e dev

The last one opens a shell with the dependencies of the tests, the linter and the documentation.

Further reading#

  • Tutorial: one problem on real data, a table of labels (car evaluations), from the pandas DataFrame to the comparison with scikit-learn baselines, the tuning, the learned structure and weights, and the test set.

  • User guide: how each model is defined and estimated, and how to choose its parameters.

  • API reference: parameters, attributes and methods of every class.

  • Example gallery: what each model captures, shown on small datasets with plots.