当我使用
HDBSCAN_flat()指定参数 n_clusters=4 时,我收到警告
UserWarning: HDBSCAN can only compute 3 clusters. Setting n_clusters to 3...
。在指定参数之前我可以得到 max_eom_clusters
吗?
实际上,max_eom_clusters(max_clusters)是在flat.py第656-657行计算的:
654 # With method 'eom', max clusters are produced for epsilon=0,
655 # as computed by
656 eom_base_clusters = condensed_tree._select_clusters()
657 max_clusters = len(eom_base_clusters)
但是,我无法在类中使用函数 _select_clusters(),因为它是受保护的。
我认为使用 HDBSCAN().fit(X) 时我们得到了最佳的簇数,但不是最大的簇数。所以使用最佳计数作为max_eom_clusters是不好的。我说得对吗?
我可以从 UserWarning 中提取
max_eom_clusters
值,如下所示。但我认为这不是一个很好的解决方案。
import numpy as np
import matplotlib.pyplot as plt
from hdbscan.flat import HDBSCAN_flat
import re
# simulate two dimension data
np.random.seed(0)
data = np.random.rand(100, 2)
plt.scatter(x=data[:, 0], y=data[:, 1])
plt.show()
# get max_eom_clusters by excepting UserWarning which was filtered s error
import warnings
warnings.filterwarnings('error', category=UserWarning)
try:
huge_n_clusterer = 999
clusterer = HDBSCAN_flat(X=data, n_clusters=huge_n_clusterer)
except UserWarning as e:
numbers = re.findall("[0-9]+", e.args[0])
max_eom_clusters = eval(numbers[0])
print(f"max_eom_clusters is {max_eom_clusters}") # max_eom_clusters is 3