tdads.machine_learning

Classes

diagram_mds

Multidimensional scaling with persistence diagrams.

diagram_kpca

Kernel PCA with persistence diagrams.

Module Contents

class tdads.machine_learning.diagram_mds(n_components: int = 2, random_state: int = None, precomputed: bool = False, dim: int = 0, metric: str = 'W', p: float = 2, sigma: float = None, n_cores: int = cpu_count() - 1)[source]

Multidimensional scaling with persistence diagrams.

__str__()[source]

Describe a persistence diagram multidimensional scaling object via its distance metric.

fit_transform(X, y: any = None)[source]

Fit the data in X and compute the position of the persistence diagrams in the embedding space.

Parameters:
  • X ({array-like of shape (n_diagrams, n_diagrams)} or {list of length n_diagrams}) – Either a precomputed distance matrix of n_diagrams many persistence diagrams (if precomputed was set to True) or a list of n_diagrams many persistence diagrams (otherwise).

  • y (Ignored) – Not used, present for API consistency by convention.

Returns:

`X_new`X transformed in the new space.

Return type:

ndarray of shape (n_diagrams, n_components)

Examples

>>> from tdads.machine_learning import diagram_mds
>>> from tdads.distance import distance
>>> from ripser import ripser
>>> import numpy as np
>>> # create 2 datasets
>>> data1 = np.random((100,2))
>>> data2 = np.random((100,2))
>>> # compute persistence diagrams with ripser
>>> diagram1 = ripser(data1)
>>> diagram2 = ripser(data2)
>>> # project into 2D with the 2-wasserstein distance
>>> mds = diagram_mds()
>>> mds.fit_transform([D1, D2])
>>> # can also fit with a precomputed distance matrix
>>> d_wass = distance()
>>> dist_mat = d_wass.compute_matrix([D1, D2])
>>> mds_precomp = diagram_mds(precomputed = True)
>>> mds_precomp.fit_transform(dist_mat)
class tdads.machine_learning.diagram_kpca(n_components: int = 2, random_state: int = None, precomputed: bool = False, diagrams: list = None, dim: int = 0, sigma: float = 1.0, t: float = 1.0, n_cores: int = cpu_count() - 1)[source]

Kernel PCA with persistence diagrams.

__str__()[source]

Describe a persistence diagram kernel principle components analysis object via its kernel function.

fit(X, y: any = None)[source]

Fit the model from data in X.

Parameters:
  • X ({array-like of shape (n_diagrams, n_diagrams)} or {list of length n_diagrams}) – Either a precomputed Gram matrix of n_diagrams many persistence diagrams (if precomputed was set to True) or a list of n_diagrams many persistence diagrams (otherwise).

  • y (Ignored) – Not used, present for API consistency by convention.

Returns:

`self` – Returns the instance itself.

Return type:

object

Examples

>>> from tdads.machine_learning import diagram_mds
>>> from tdads.kernel import kernel
>>> from ripser import ripser
>>> import numpy as np
>>> # create 2 datasets
>>> data1 = np.random((100,2))
>>> data2 = np.random((100,2))
>>> # compute persistence diagrams with ripser
>>> diagram1 = ripser(data1)
>>> diagram2 = ripser(data2)
>>> # fit model with the persistence Fisher kernel (sigma = t = 1)
>>> kpca = diagram_kpca()
>>> kpca_fitted = kpca.fit([D1, D2])
>>> # can also fit with a precomputed distance matrix
>>> pfk = kernel()
>>> gram_mat = pfk.compute_matrix([D1, D2])
>>> kpca_precomp = diagram_kpca(precomputed = True)
>>> kpca_precomp_fitted = kpca_precomp.fit(gram_mat)
transform(X)[source]

Project new persistence diagrams into the embedding space.

Parameters:

X ({array-like of shape (n_diagrams, n_diagrams)} or {list of length n_diagrams}) – Either a precomputed (cross) Gram matrix of shape (n_new_diagrams, n_diagrams) (between the new persistence diagrams and the training set diagrams, if precomputed was set to True) or a list of n_new_diagrams many persistence diagrams (otherwise).

Returns:

`X_new` – The embedding of the new persistence diagrams.

Return type:

ndarray

Examples

>>> from tdads.machine_learning import diagram_mds
>>> from tdads.kernel import kernel
>>> from ripser import ripser
>>> import numpy as np
>>> # create 2 datasets
>>> data1 = np.random((100,2))
>>> data2 = np.random((100,2))
>>> # compute persistence diagrams with ripser
>>> diagram1 = ripser(data1)
>>> diagram2 = ripser(data2)
>>> # fit models (regular and precomputed) with the
>>> # persistence Fisher kernel (sigma = t = 1)
>>> kpca = diagram_kpca()
>>> kpca_fitted = kpca.fit([D1, D2]) # or
>>> pfk = kernel()
>>> gram_mat = pfk.compute_matrix([D1, D2])
>>> kpca_precomp = diagram_kpca(precomputed = True)
>>> kpca_precomp_fitted = kpca_precomp.fit(gram_mat)
>>> # create 2 new datasets
>>> data3 = np.random((100,2))
>>> data4 = np.random((100,2))
>>> # project new data into 2D space
>>> kpca_fitted.transform([D3, D4]) # or
>>> cross_gram = pfk.compute_matrix([D1, D2], [D3, D4])
>>> kpca_precomputed_fitted.transform([D3, D4])
fit_transform(X, y: any = None)[source]

Fit the data in X and compute the position of the persistence diagrams in the embedding space.

Parameters:
  • X ({array-like of shape (n_diagrams, n_diagrams)} or {list of length n_diagrams}) – Either a precomputed Gram matrix of n_diagrams many persistence diagrams (if precomputed was set to True) or a list of n_diagrams many persistence diagrams (otherwise).

  • y (Ignored) – Not used, present for API consistency by convention.

Returns:

`X_new`X transformed in the new space.

Return type:

ndarray

Examples

>>> from tdads.machine_learning import diagram_mds
>>> from tdads.kernel import kernel
>>> from ripser import ripser
>>> import numpy as np
>>> # create 2 datasets
>>> data1 = np.random((100,2))
>>> data2 = np.random((100,2))
>>> # compute persistence diagrams with ripser
>>> diagram1 = ripser(data1)
>>> diagram2 = ripser(data2)
>>> # fit models (regular and precomputed) with the
>>> # persistence Fisher kernel (sigma = t = 1) and
>>> # project into 2D space
>>> kpca = diagram_kpca()
>>> kpca.fit_transform([D1, D2]) # or
>>> pfk = kernel()
>>> gram_mat = pfk.compute_matrix([D1, D2])
>>> kpca_precomp = diagram_kpca(precomputed = True)
>>> kpca_precomp.fit_transform(gram_mat)