SciPy - DisjointSet() 方法



SciPy DisjointSet() 方法用於將資料分割槽管理到不相交的子集中。它用於管理分層聚類演算法中的叢集。

語法

以下是 SciPy DisjointSet() 方法的語法 -

DisjointSet([item_1, item_2, ...])

引數

此方法接受列表作為引數。

返回值

此方法不返回任何值。

示例

以下示例展示了 SciPy DisjointSet() 方法的用法。

from scipy.cluster.hierarchy import DisjointSet

# create a disjoint-set data structure with 5 elements
ds = DisjointSet([1, 2, 3, 'a', 'b'])

# union operations 
ds.merge(1, 2)
ds.merge(3, 'a')
ds.merge('a', 'b')

# find the root elements
print(ds[2]) 
print(ds['b'])  

# test connectivity
print(ds.connected(1, 2))  
print(ds.connected(1, 'b')) 

# list elements in disjoint set
print(list(ds))  

# get the subset containing 'a'
print(ds.subset('a'))  

# get the size of the subset containing 'a'
print(ds.subset_size('a')) 

# get all subsets in the disjoint set
print(ds.subsets()) 

結果

上述程式碼產生以下結果 -

1
3
True
False
[1, 2, 3, 'a', 'b']
{'a', 3, 'b'}
3
[{1, 2}, {'a', 3, 'b'}]
scipy_reference.htm
廣告
© . All rights reserved.