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:
NumericModeMixinKernel density estimation.
Estimates the density of the training data with a brute-force sum over every training row, following scikit-learn’s
KernelDensityinterface and cuML’s implementation. Input is converted to float32; the fit set must be finite.Differences from scikit-learn:
bandwidthmust be a number;'scott'and'silverman'raiseNotImplementedError. 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:
algorithmmust be'auto'orNone, andatol,rtol,breadth_firstandleaf_sizemust keep their defaults; other values raiseValueError.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'raisesValueErrorfor 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. Onlyp = 2is supported; any other value raisesNotImplementedError.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default=None) – Numeric mode (
'fast'is Apple only);Noneuses the package default.
- Variables:
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 nosampleeither).save(path)Save the fitted model to
pathas an.npzfile.score(X[, y])Total log density of
X: the sum of score_samples().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 nosampleeither).
- save(path)[source]#
Save the fitted model to
pathas an.npzfile.Stores the float32 training data (the whole fitted state of a brute-force KDE), the weights if any,
bandwidth,kernel,metricand the fitted sizes.mojolearn.host_model(path)scores from the file on a machine without a GPU. An unfitted model raisesRuntimeError.- Parameters:
path (str or path-like)