欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Python-ML】SKlearn库原型聚类KMeans

程序员文章站 2022-07-14 11:49:59
...
# -*- coding: utf-8 -*-
'''
Created on 2018年1月25日
@author: Jason.F
@summary: 无监督聚类学习-KMeans算法
'''
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from matplotlib import cm
from sklearn.metrics import silhouette_samples

X,y = make_blobs(n_samples=150,n_features=2,centers=3,cluster_std=0.5,shuffle=True,random_state=0)
plt.scatter(X[:,0],X[:,1],c='red',marker='o',s=50)
plt.grid()
plt.show() 
#簇内物产平方和容忍度tol=1e-04
km = KMeans(n_clusters=3,init='random',n_init=10,max_iter=300,tol=1e-04,random_state=0)
y_km = km.fit_predict(X)
#可视化
plt.scatter(X[y_km==0,0],X[y_km==0,1],s=50,c='lightgreen',marker='s',label='cluster 1')
plt.scatter(X[y_km==1,0],X[y_km==1,1],s=50,c='orange',marker='o',label='cluster 2')
plt.scatter(X[y_km==2,0],X[y_km==2,1],s=50,c='lightblue',marker='v',label='cluster 3')
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],s=250,c='red',marker='*',label='centroids 3')
plt.legend()
plt.grid()
plt.show()
'''
KMeans存在簇类k值的先验获取,可以通过肘部法则确定最佳簇类数量
KMeans++所选择初始中心点尽可能远
'''
print ('Distortion:%.2f'%km.inertia_)#簇内误差平方和
distortions=[]
for i in range(1,11):
    km=KMeans(n_clusters=i,init='k-means++',n_init=10,max_iter=300,random_state=0)
    km.fit(X)
    distortions.append(km.inertia_)
plt.plot(range(1,11),distortions,marker='o')
plt.xlabel('Number of clusters')
plt.ylabel('Distortion')
plt.show()
'''
轮廓分析使用图形工具来度量簇中样本聚集的秘籍成都,通过计算单个样本的轮廓系数(silhouette coefficient)
'''
km = KMeans(n_clusters=3,init='k-means++',n_init=10,max_iter=300,tol=1e-04,random_state=0)
y_km = km.fit_predict(X)
cluster_labels = np.unique(y_km)
n_clusters = cluster_labels.shape[0]
silhouette_vals = silhouette_samples(X, y_km, metric='euclidean')
y_ax_lower,y_ax_upper =0,0
yticks =[]
for i,c in enumerate(cluster_labels):
    c_silhouette_vals =silhouette_vals[y_km==c]
    c_silhouette_vals.sort()
    y_ax_upper+=len(c_silhouette_vals)
    color =cm.jet(i/n_clusters)
    plt.barh(range(y_ax_lower,y_ax_upper),c_silhouette_vals,height=1.0,edgecolor='none',color=color)
    yticks.append((y_ax_lower+y_ax_upper)/2)
    y_ax_lower += len(c_silhouette_vals)
silhouette_avg = np.mean(silhouette_vals)
plt.axvline(silhouette_avg,color='red',linestyle='--')
plt.yticks(yticks,cluster_labels+1)
plt.ylabel('Cluster')
plt.xlabel('Silhouette coefficient')
plt.show()

结果:

Distortion:72.48
【Python-ML】SKlearn库原型聚类KMeans

【Python-ML】SKlearn库原型聚类KMeans

【Python-ML】SKlearn库原型聚类KMeans

【Python-ML】SKlearn库原型聚类KMeans