如何用Vietoris Rips Filtration生成含孔洞特征的数据集Barcode
解决Vietoris-Rips滤过无法显示孔洞对应Barcode的问题
你的代码只显示0维Barcode的核心原因是:调用create_simplex_tree()时默认仅生成到1维单纯形(边),而要检测孔洞(对应1维同调拓扑特征),需要构造2维单纯形(三角形)来定义孔洞的边界。
修改后的代码
import numpy as np import gudhi import matplotlib.pyplot as plt data = np.array([[5.1, 3.5], [4.9, 3], [4.7, 3.2], [4.6, 3.1], [5., 3.6], [5.4, 3.9], [4.6, 3.4], [5., 3.4], [4.4, 2.9], [4.9, 3.1]]) # 创建Vietoris-Rips复形,指定最大单纯形维度为2(支持生成三角形) rips_complex = gudhi.RipsComplex(points=data) # 设置max_dimension=2,才能计算1维同调特征(孔洞) simplex_tree = rips_complex.create_simplex_tree(max_dimension=2) # 获取全维度的Barcode barcode = simplex_tree.persistence() # 绘制Barcode,此时会包含0维(孤立点连接)和1维(孔洞形成/闭合)特征 gudhi.plot_persistence_barcode(barcode) plt.title("Vietoris-Rips Persistence Barcode (0维和1维)") plt.show() # 可选:单独查看1维Barcode(孔洞特征) one_dim_barcode = simplex_tree.persistence(homology_coeff_field=2, min_persistence=0.0) gudhi.plot_persistence_barcode(one_dim_barcode, max_intervals=10) plt.title("1维同调Barcode(孔洞特征)") plt.show()
关键说明
max_dimension=2:这个参数告诉GUDHI生成到2维单纯形(三角形),只有存在2维单纯形时,才能通过其边界检测出1维孔洞(即被三角形包围的“洞”)。- 1维Barcode的每个条代表一个孔洞:条的起点是孔洞形成的半径,终点是孔洞被更大的单纯形“填充”(闭合)的半径;如果条的终点是
inf,则代表这个孔洞在当前滤过范围内始终存在。
内容的提问来源于stack exchange,提问作者Sofia Ordaz
相关产品推荐
相关产品推荐

