kernel

A class to implement diffusion kernels.

class pydiffmap.kernel.Kernel(kernel_type='gaussian', epsilon='bgh', k=64, neighbor_params=None, metric='euclidean', metric_params=None)[source]

Class abstracting the evaluation of kernel functions on the dataset.

Parameters:
  • type (string, optional) – Type of kernel to construct. Currently the only option is ‘gaussian’, but more will be implemented.
  • epsilon (string, optional) – Method for choosing the epsilon. Currently, the only options are to provide a scalar (epsilon is set to the provided scalar) or ‘bgh’ (Berry, Giannakis and Harlim).
  • k (int, optional) – Number of nearest neighbors over which to construct the kernel.
  • neighbor_params (dict or None, optional) – Optional parameters for the nearest Neighbor search. See scikit-learn NearestNeighbors class for details.
  • metric (string, optional) – Distance metric to use in constructing the kernel. This can be selected from any of the scipy.spatial.distance metrics, or a callable function returning the distance.
  • metric_params (dict or None, optional) – Optional parameters required for the metric given.
choose_optimal_epsilon(epsilon=None)[source]

Chooses the optimal value of epsilon and automatically detects the dimensionality of the data.

Parameters:epsilon (string or scalar, optional) – Method for choosing the epsilon. Currently, the only options are to provide a scalar (epsilon is set to the provided scalar) or ‘bgh’ (Berry, Giannakis and Harlim).
Returns:self (the object itself)
compute(Y=None)[source]

Computes the sparse kernel matrix.

Parameters:Y (array-like, shape (n_query, n_features), optional.) – Data against which to calculate the kernel values. If not provided, calculates against the data provided in the fit.
Returns:K (array-like, shape (n_query_X, n_query_Y)) – Values of the kernel matrix.
fit(X)[source]

Fits the kernel to the data X, constructing the nearest neighbor tree.

Parameters:X (array-like, shape (n_query, n_features)) – Data upon which to fit the nearest neighbor tree.
Returns:self (the object itself)
pydiffmap.kernel.choose_optimal_epsilon_BGH(scaled_distsq, epsilons=None)[source]

Calculates the optimal epsilon for kernel density estimation according to the criteria in Berry, Giannakis, and Harlim.

Parameters:
  • scaled_distsq (numpy array) – Values for scaled distance squared values, in no particular order or shape. (This is the exponent in the Gaussian Kernel, aka the thing that gets divided by epsilon).
  • epsilons (array-like, optional) – Values of epsilon from which to choose the optimum. If not provided, uses all powers of 2. from 2^-40 to 2^40
Returns:

  • epsilon (float) – Estimated value of the optimal length-scale parameter.
  • d (int) – Estimated dimensionality of the system.

Notes

Erik sez : I have a suspicion that the derivation here explicitly assumes that the kernel is Gaussian. However, I’m not sure. Also, we should perhaps replace this with some more intelligent optimization routine. Here, I’m just picking from several values and choosin the best.

References

The algorithm given is based on [1]. If you use this code, please cite them.

[1]T. Berry, D. Giannakis, and J. Harlim, Physical Review E 91, 032915 (2015).