• 【机器学习】机器学习学习笔记 - 无监督学习 - k-means/均值漂移聚类/凝聚层次聚类/近邻传播聚类 - 05


    pdf在线免费转word文档 https://orcc.online/pdf

    不限次数、免费不需要注册。

    无监督学习 (聚类)

    • 聚类是一种无监督学习方法,是将数据划分为若干个簇,使得簇内的点尽可能相似,簇间尽可能不相似。

    k-means 聚类

    • k-means 聚类算法是一种迭代算法,它会不断地寻找最佳的 k 值,然后将数据分配到这些簇中。
    • 聚类算法的优点是简单,易于实现,并且对数据维度的要求不高。
    from sklearn.cluster import KMeans
    
    num_clusters = 4
    kmeans = KMeans(init='k-means++', n_clusters=num_clusters, n_init=10)
    kmeans.fit(data)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    均值漂移聚类

    • 把数据点的分布看成是概率密度函数, 希望在特征空间中根据函数分布特征找出数据点的"模式"(mode)
    • 优点是不需要事先确定集群的数量
    import numpy as np
    from sklearn.cluster import MeanShift, estimate_bandwidth
    
    # Estimating the bandwidth
    # 设置带宽参数 quantile : 0.1 代表数据集中10%的样本作为聚类中心
    # n_samples : 样本数
    bandwidth = estimate_bandwidth(X, quantile=0.1, n_samples=len(X))
    
    # Compute clustering with MeanShift
    # bin_seeding : 随机种子
    # 随机种子,保证每次聚类结果一致
    meanshift_estimator = MeanShift(bandwidth=bandwidth, bin_seeding=True)
    meanshift_estimator.fit(X)
    
    # 提取标记
    labels = meanshift_estimator.labels_
    # 聚类中心
    centroids = meanshift_estimator.cluster_centers_
    num_clusters = len(np.unique(labels))
    
    print("Number of clusters in input data =", num_clusters)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    凝聚层次聚类

    • 层次聚类算法可以是自下而上的,也可以是自上而下
    • 自下而上: 每个数据点都被看作一个簇,然后将簇进行合并,直到所有簇合并为一个簇
    • 自上而下: 先将所有数据点看作一个簇,然后将簇进行分裂,直到所有簇分裂为一个簇
    from sklearn.cluster import AgglomerativeClustering
    
    plt.figure()
    model = AgglomerativeClustering(linkage=linkage,
                    connectivity=connectivity, n_clusters=num_clusters)
    model.fit(X)
    
    # extract labels
    labels = model.labels_
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    近邻传播聚类

    • 找出数据点的相似度,然后根据相似度进行聚类
    • 优点是不需要事先确定簇的数量
    from sklearn.cluster import AffinityPropagation
    
    # 使用亲和传播聚类算法构建聚类模型
    # edge_model.covariance_可以获取到股票之间的协方差矩阵,该矩阵表示了不同股票之间的相关性和波动性
    _, labels = cluster.affinity_propagation(edge_model.covariance_)
    num_labels = labels.max()
    
    # 打印聚类结果
    for i in range(num_labels + 1):
        print("Cluster", i+1, "-->", ', '.join(names[labels == i]))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    华为云运维小结
    MongoDB-索引Index
    Spring Boot整合Spring mvc(文件上传/拦截器)
    linux篇【3】:Linux 环境基础开发工具
    JavaScript高级知识-闭包
    华为机试真题 C++ 实现【字符串序列判定】
    气膜球幕影院:娱乐体验的新高度—轻空间
    在软件测试行业这种情况下,凭什么他能拿25k?我却约面试都难?
    python-web开发[13]之前端js
    【操作系统一】图解TCP/IP模型+实战
  • 原文地址:https://blog.csdn.net/qq_22475637/article/details/137799021