Menu

Deep learning for NeuroImaging in Python.

Note

This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the gallery for the big picture.

class nidl.losses.yaware_infonce.KernelMetric(kernel='gaussian', bandwidth: str | float | list[float] | ndarray = 'scott')[source]

Bases: BaseEstimator

Interface for fast kernel computation.

It computes the weighting matrix between input samples based on Kernel Density Estimation (KDE) weighting scheme [1, 2]. Concretely, it computes the following weighting matrix between ( x1, …, xn ): .. math:

W_{i,j} = K\left( H^{-\frac{1}{2}} (xi-xj) \right)

with K a kernel such that:

  1. ( K(x) geq 0 ),

  2. ( int K(x) , dx = 1 ),

  3. ( K(x) = K(-x) ),

and ( H ) is the bandwidth in the KDE estimation of ( p(X) ).

H is symmetric definite positive and it can be automatically computed based on Scott’s method or Silverman’s method if required. In that case, the bandwidth is computed as a scaled version of the covariance matrix of the data. If the bandwidth is set to a scalar, it is used as a diagonal matrix: .. math:

H = \mathrm{diag}(\text{scalar})
Parameters:

kernel : {‘gaussian’, ‘epanechnikov’, ‘exponential’, ‘linear’, ‘cosine’}, default=’gaussian’

The kernel applied to the distance between samples.

bandwidth : str or int or float or list of float, default=”scott”

The method used to calculate the estimator bandwidth:

  • If bandwidth is str, must be ‘scott’ or ‘silverman’. Bandwidth is a scaled version of the diagonal term in the data covariance matrix.

  • If bandwidth is scalar (float or int), it sets the bandwidth to H=diag([bandwidth for _ in range(n_features)])

  • If bandwidth is a list of float, it sets the bandwidth to H=diag(bandwidth) and it must be of length equal n_features

  • If bandwidth is a 2d array, it must be of shape (n_features, n_features)

References

[1] Rosenblatt, M. (1956). “Remarks on some nonparametric estimates of a

density function”. Annals of Mathematical Statistics.

[2] Parzen, E. (1962). “On estimation of a probability density function and

mode”

fit(X)[source]

Computes the bandwidth in the kernel density estimator of p(X).

Parameters:

X : array of shape (n_samples, n_features)

Input data used to estimate the bandwidth (based on covariance matrix).

Returns:

self : KernelMetric

pairwise(X)[source]
Parameters:

X : array of shape (n_samples, n_features)

Input data.

Returns:

S : array of shape (n_samples, n_samples)

Similarity matrix between input data.

scotts_factor()[source]

Compute Scott’s factor.

Returns:

s : float

Scott’s factor.

set_bandwidth(X)[source]

Compute the estimator bandwidth. Implementation from scipy.

The new bandwidth calculated after a call to set_bandwidth is used for subsequent evaluation of the estimated density.

Parameters:

X : ndarray of shape (n_samples, n_features)

Input data.

silverman_factor()[source]

Compute the Silverman factor.

Returns:

s : float

The silverman factor.

Follow us

© 2025, nidl developers