代码之家  ›  专栏  ›  技术社区  ›  dre_84w934

无监督学习聚类一维数组

  •  -2
  • dre_84w934  · 技术社区  · 6 年前

    我面临着以下阵势:

    y = [1,2,4,7,9,5,4,7,9,56,57,54,60,200,297,275,243]
    

    我想做的是提取得分最高的聚类那就是

    best_cluster = [200,297,275,243]
    

    我在stack上检查了很多关于这个主题的问题,其中大多数建议使用kmeans尽管还有一些人提到kmeans可能是对1D数组集群的过度破坏。 然而KMeans是一个有监督的学习算法,因此这意味着我必须传入质心的数量。因为我需要将这个问题推广到其他数组,所以我无法传递每个数组的质心数因此,我正在考虑实现某种无监督学习算法,这种算法能够自己找出集群并选择最高的集群。 在数组y中,我可以看到3个簇[1,2,4,7,9,5,4,7,9],[56,57,54,60],[200297275243]。 考虑到计算成本和准确性,哪种算法最适合我的需要?我如何实现它来解决我的问题?

    3 回复  |  直到 6 年前
        1
  •  1
  •   EasonL    6 年前

    尝试 MeanShift . 从斯克里恩来的 user guide 意义转移:

    该算法自动设置簇的数目。。。

    修改的演示代码:

    import numpy as np
    from sklearn.cluster import MeanShift, estimate_bandwidth
    
    # #############################################################################
    # Generate sample data
    X = [1,2,4,7,9,5,4,7,9,56,57,54,60,200,297,275,243]
    X = np.reshape(X, (-1, 1))
    
    # #############################################################################
    # Compute clustering with MeanShift
    
    # The following bandwidth can be automatically detected using
    # bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=100)
    
    ms = MeanShift(bandwidth=None, bin_seeding=True)
    ms.fit(X)
    labels = ms.labels_
    cluster_centers = ms.cluster_centers_
    
    labels_unique = np.unique(labels)
    n_clusters_ = len(labels_unique)
    
    print("number of estimated clusters : %d" % n_clusters_)
    print(labels)
    

    输出:

    number of estimated clusters : 2
    [0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1]
    

    注意MeanShift是 可根据样本数量进行缩放建议的上限是10000。


    顺便说一下,正如rahlf23已经提到的,K-mean是 无监督 学习算法必须指定集群数量并不意味着它是受监控的。

    另见:

        2
  •  0
  •   Edward Khachatryan    6 年前

    HDBSCAN 是最好的聚类算法,您应该始终使用它。

    基本上你只需要提供一个合理的 min_cluster_size ,有效距离 metric 你可以走了。

    为了 最小群集大小 我建议使用3,因为一个2的集群是无用的 米制的 违约 euclidean 工作得很好,所以你甚至不用提它。

    别忘了距离指标适用于 向量 在这里我们有 标量 所以一些丑陋的重塑是有序的。

    总而言之,假设“得分最高的集群”是指包含我们得到的最大值的集群:

    from hdbscan import HDBSCAN
    import numpy as np
    
    y = [1,2,4,7,9,5,4,7,9,56,57,54,60,200,297,275,243]
    y = np.reshape(y, (-1, 1))
    
    clusterer = HDBSCAN(min_cluster_size=3)
    cluster_labels = clusterer.fit_predict(y)
    
    best_cluster = clusterer.exemplars_[cluster_labels[y.argmax()]].ravel()
    print(best_cluster)
    

    输出是 [297 200 275 243] . 不保留原始订单。 西拉维 是的。

        3
  •  0
  •   Has QUIT--Anony-Mousse    6 年前

    聚类 这里杀戮过度

    只需计算后续元素的差异也就是说,看看 x[i]-x[i-1] .

    选择 最大的差异作为分割点或者定义何时拆分的阈值例如20取决于你的数据知识。

    这是O(n),比前面提到的要快得多也很容易理解和预测。

    在一维有序数据上,任何不使用该顺序的方法都将慢于所需的速度。