美文网首页
学习笔记:sklearn-KMeans

学习笔记:sklearn-KMeans

作者: zeolite | 来源:发表于2021-06-18 16:44 被阅读0次
    from sklearn.cluster import KMeans
    from sklearn.datasets import make_blobs
    import matplotlib.pyplot as plt
    
    X, y=make_blobs(n_samples=600, n_features=2, centers=4, random_state=0)
    
    n_clusters=4
    cluster=KMeans(n_clusters=n_clusters, max_iter=100, init='random')
    
    y_pred =cluster.fit_predict(X)
    
    #查看标签
    y_pred=cluster.labels_
    #查看簇心点坐标
    centers =cluster.cluster_centers_
    #簇心的均方距离
    cluster.inertia_
    #循环次数
    cluster.n_iter_
    
    for i in range(n_clusters):
        plt.scatter(X[y_pred==i,0], X[y_pred==i,1])
    plt.scatter(centers[:,0], centers[:,1], marker='^' ,color='purple')
    plt.show()
    

    轮廓系数 最好=1,最差=-1

    from sklearn.metrics import silhouette_samples, silhouette_score
    
    #返回全部样本的轮廓系数均值
    silhouette_score(X,y_pred)
    #返回每个样本的轮廓系数
    silhouette_samples(X, y_pred)
    

    k_means会返回 ,cluster.cluster_centers_,cluster.labels_,cluster.inertia_,cluster.n_iter_

    from sklearn.cluster import k_means
    k_means(X, n_cluster, return_n_iter=True)
    

    相关文章

      网友评论

          本文标题:学习笔记:sklearn-KMeans

          本文链接:https://www.haomeiwen.com/subject/qxxlyltx.html