zoukankan      html  css  js  c++  java
  • day09-K-means聚类

    • k-means聚类是处理没有目标值的数据
    • 目的是将数据分成指定数量的类别,即“物以类聚”
    • k-means步骤
      1、随机设置K个特征空间内的点作为初始的聚类中心
      2、对于其他每个点计算到K个中心的距离,未知的点选择最近的一个聚类中心点作为标记类别
      3、接着对着标记的聚类中心之后,重新计算出每个聚类的新中心点(平均值)
      4、如果计算得出的新中心点与原中心点一样,那么结束,否则重新进行第二步过程
    
    # coding=utf-8
    from sklearn.cluster import KMeans
    from sklearn.metrics import silhouette_score
    from sklearn.datasets import load_iris
    
    def jl():
    
        # 获取数据
        # 已知有三类
        iris = load_iris()
    
        # 开始聚类
        km = KMeans(n_clusters=3)
        km.fit(iris.data)
        predict = km.predict(iris.data)
        print("聚类结果为:",predict)
        print("实际的结果为:",iris.target)
    
        # 轮廓系数的值区间为[-1,1],越接近1结果越好,越接近-1结果越差
        print("轮廓系数为:",silhouette_score(iris.data,predict))
    
        return None
    
    
    if __name__ == '__main__':
        jl()
    
    
    
    

    结果:

    
    聚类结果为: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
     1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 2 1 2 2 2 2
     2 2 1 1 2 2 2 2 1 2 1 2 1 2 2 1 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2
     2 1]
    实际的结果为: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
     2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
     2 2]
    轮廓系数为: 0.5528190123564091
    
    Process finished with exit code 0
    
    
    
    
  • 相关阅读:
    数据库路由中间件MyCat
    数据库路由中间件MyCat
    数据库路由中间件MyCat
    数据库路由中间件MyCat
    数据库路由中间件MyCat
    数据库路由中间件MyCat
    数据库路由中间件MyCat
    云架构师进阶攻略(3)
    PAT 1059. C语言竞赛
    PAT 1058. 选择题
  • 原文地址:https://www.cnblogs.com/wuren-best/p/14283921.html
Copyright © 2011-2022 走看看