Persistent Homology相关

常用的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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值