常用的package:, DIPHA
, GUDHI
, and Ripser,
- H0: Connected Components
- H1: Loops
- H2: Voids
1. VR复形
2. Cech复形
3. 使用gudhi计算BettiCureve, Persisten
以下默认使用lower star filtration
, 又称为 sublevelset filtration
。相关教程: gudhi tutorial, gudhi-sklearn
from sklearn_tda import *
from ripser import ripser, lower_star_img
import matplotlib.pyplot as plt
from gudhi.sklearn.cubical_persistence import CubicalPersistence
from gudhi.representations.vector_methods import BettiCurve
from gudhi.representations import PersistenceImage, DiagramSelector
import skimage
import matplotlib.pyplot as plt
img = skimage.data.camera().astype(np.float64)
dgm = lower_stat_img(img) # only H0 (connect components)
gudhi_cp = CubicalPersistence([0, 1])
# result is a list
# [[np.array((m, 2)), np.array((n, 2))]]
gudhi_pd = gudhi_cp.fit_transform([img])
# remove infinite diagrams
ds = DiagramSelector(use=True, point_type="finite")
gudhi_pd = ds.fit_transform(gudhi_pd) # gudhi_pd is a list of n×2 array
gudhi_bc = BettiCurve(resolution=255, sample_range=[0, 255])
bc_number = gudhi_bc.fit_transform(gudhi_pd[0])
plt.plot(bc_number[0], label="H0-Connected Compponents")
plt.plot(bc_number[1], label="H1-Loops")
plt.legend()
pi = PersistenceImage(bandwidth=50, weight=lambda x: x[1] ** 2, im_range=[0, 256, 0, 256], resolution=[20, 20])
pi_img = pi.fit_transform(gudhi_pd).reshape((20, 20))
plt.imshow(pi_img, cmap="gray")
或者使用sklean.pipeline
# Standard scientific Python imports
import numpy as np
# Standard scikit-learn imports
from sklearn.datasets import fetch_openml
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn import metrics
# Import TDA pipeline requirements
from gudhi.sklearn.cubical_persistence import CubicalPersistence
from gudhi.representations import PersistenceImage, DiagramSelector
pipe = Pipeline(
[
("cub_pers", CubicalPersistence(homology_dimensions=[0, 1], newshape=[n, img_h, img_w], n_jobs=-2)),
# Or for multiple persistence dimension computation
# ("cub_pers", CubicalPersistence(homology_dimensions=[0, 1], newshape=[28, 28], n_jobs=-2)),
# ("H0_diags", DimensionSelector(index=0), # where index is the index in homology_dimensions array
("finite_diags", DiagramSelector(use=True, point_type="finite")),
(
"pers_img",
PersistenceImage(bandwidth=50, weight=lambda x: x[1] ** 2, im_range=[0, 256, 0, 256], resolution=[20, 20]),
),
# ("svc", SVC()),
]
)
pi = pipe.fit_transform(img_arr)
2. 使用sklean_tda
from sklearn_tda import BettiCurve
BC = BettiCurve(resolution=1000)
bc = BC.fit_transform([a[0][0]])
3. 使用gtda
from gtda.homology import CubicalPersistence
from gtda.plotting import plot_diagram
cp = CubicalPersistence()
pd = cp.fit_transform_plot(data) # plot and return the PD
from gtda.diagrams import BettiCurve
bc = BettiCurve()
betti_number = bc.fit_transform(pd) # return the betti number
bc.plot(betti_number[:, :2, :])
from gtda.diagrams import PersistenceImage
pi = PersistenceImage()
pi.fit_transform(pd) # return the PersistenceImage