mojolearn.KernelDensity#

class mojolearn.KernelDensity(*, bandwidth=1.0, algorithm='auto', kernel='gaussian', metric='euclidean', atol=0, rtol=0, breadth_first=True, leaf_size=40, metric_params=None)[source]#

Bases: NumericModeMixin

Kernel density estimation.

Estimates the density of the training data with a brute-force sum over every training row, following scikit-learn’s KernelDensity interface and cuML’s implementation. Input is converted to float32; the fit set must be finite.

Differences from scikit-learn:

  • bandwidth must be a number; 'scott' and 'silverman' raise NotImplementedError. Compute them yourself (scikit-learn: n ** (-1/(d+4)) and (n * (d+2) / 4) ** (-1/(d+4))) and pass the result.

  • There is no tree: algorithm must be 'auto' or None, and atol, rtol, breadth_first and leaf_size must keep their defaults; other values raise ValueError.

  • score_samples() returns float32. A row that no training point reaches under a compact kernel scores -3.4028235e+38 (cuML’s value) where scikit-learn returns -inf.

  • For the 'cosine' kernel in an even number of dimensions the normalization differs from scikit-learn’s and cuML’s, whose constant is incorrect there (NaN at d = 4).

  • sample() is not available.

Parameters:
  • bandwidth (float, default=1.0) – Kernel bandwidth; must be positive.

  • algorithm ({‘auto’, None}, default=’auto’) – Accepted for compatibility; the search is always brute force.

  • kernel (str, default=’gaussian’) – 'gaussian', 'tophat', 'epanechnikov', 'exponential', 'linear' or 'cosine'.

  • metric (str, default=’euclidean’) – 'euclidean' / 'l2', 'sqeuclidean', 'l1' / 'cityblock' / 'manhattan', 'chebyshev', 'cosine' or 'minkowski'. As in cuML, the density normalization is correct only for the Euclidean metric. 'cosine' raises ValueError for an all-zero row in the training or query data. Other names raise an error.

  • atol, rtol (float, default=0) – Must be 0.

  • breadth_first (bool, default=True) – Must be True.

  • leaf_size (int, default=40) – Must be 40.

  • metric_params (dict or None, default=None) – At most one entry, whose value is the Minkowski p, as in cuML. Only p = 2 is supported; any other value raises NotImplementedError.

  • numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default=None) – Numeric mode ('fast' is Apple only); None uses the package default.

Variables:
  • n_features_in_ (int) – Number of features seen in fit().

  • n_samples_fit_ (int) – Number of training rows.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> kde = mojolearn.KernelDensity(bandwidth=0.5).fit(X)
>>> log_density = kde.score_samples(X[:10])
__init__(*, bandwidth=1.0, algorithm='auto', kernel='gaussian', metric='euclidean', atol=0, rtol=0, breadth_first=True, leaf_size=40, metric_params=None)[source]#

Methods

fit(X[, y, sample_weight])

Store the training data.

get_params([deep])

load(path)

Load a model written by save().

numeric_mode_used()

The numeric mode this estimator runs in.

sample([n_samples, random_state])

Not implemented; always raises NotImplementedError (cuML has no sample either).

save(path)

Save the fitted model to path as an .npz file.

score(X[, y])

Total log density of X: the sum of score_samples().

score_samples(X)

Log density at each row of X.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y=None, sample_weight=None)[source]#

Store the training data.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training rows, converted to float32; every value must be finite.

  • y (ignored)

  • sample_weight (array-like of shape (n_samples,), default=None) – Finite, non-negative per-row weights with a positive sum.

Returns:

self

Raises:

ValueError – For a non-finite value or invalid sample_weight.

score_samples(X)[source]#

Log density at each row of X.

Parameters:

X (array-like of shape (n_queries, n_features)) – Query rows, converted to float32, with the fitted feature count.

Returns:

log_density (Array of float32, shape (n_queries,)) – Rows no training point reaches under a compact kernel get -3.4028235e+38.

score(X, y=None)[source]#

Total log density of X: the sum of score_samples().

The sum is taken in a fixed order, so it is reproducible.

Parameters:
  • X (array-like of shape (n_queries, n_features))

  • y (ignored)

Returns:

float

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

Not implemented; always raises NotImplementedError (cuML has no sample either).

save(path)[source]#

Save the fitted model to path as an .npz file.

Stores the float32 training data (the whole fitted state of a brute-force KDE), the weights if any, bandwidth, kernel, metric and the fitted sizes. mojolearn.host_model(path) scores from the file on a machine without a GPU. An unfitted model raises RuntimeError.

Parameters:

path (str or path-like)

classmethod load(path)[source]#

Load a model written by save().

The returned model can score. Arrays are read at their saved dtype and never cast; a file that does not match raises ValueError.

Parameters:

path (str or path-like)

Returns:

KernelDensity