我正在尝试根据用户的行为对一些产品进行聚类。我最后得到的是具有非常不同数量的观察值的星系团。
我已经检查了k-means聚类参数,但无法找到控制每个聚类的最小(或最大)观察值数量的参数。
例如,这里是如何在不同的集群中分布观察值的数量。
cluster_id num_observations
0 6
1 4
2 1
3 3
4 29
5 5有什么关于如何处理这个问题的帮助吗?有没有其他的聚类算法可以解决这个问题?
发布于 2020-09-18 14:12:14
对于那些仍在寻找答案的人。我找到了一个处理这类问题的good module或this module
使用pip install size-constrained-clustering或pip install git+https://github.com/jingw2/size_constrained_clustering.git并使用MinMaxKMeansMinCostFlow,您可以在其中选择size_min和size_max
n_samples = 2000
n_clusters = 3
X = np.random.rand(n_samples, 2)
model = minmax.MinMaxKMeansMinCostFlow(n_clusters, size_min=400, size_max=800)
model.fit(X)
centers = model.cluster_centers_
labels = model.labels_发布于 2021-05-14 23:58:35
这将通过k-means约束pip库来解决。check here
示例:
>>> from k_means_constrained import KMeansConstrained
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],
... [4, 2], [4, 4], [4, 0]])
>>> clf = KMeansConstrained(
... n_clusters=2,
... size_min=2,
... size_max=5,
... random_state=0
... )
>>> clf.fit_predict(X)
array([0, 0, 0, 1, 1, 1], dtype=int32)
>>> clf.cluster_centers_
array([[ 1., 2.],
[ 4., 2.]])
>>> clf.labels_
array([0, 0, 0, 1, 1, 1], dtype=int32)https://stackoverflow.com/questions/55930406
复制相似问题