聚类--K均值算法:自主实现与sklearn.cluster.KMeans调用

1.K-means是一个反复迭代的过程,算法分为四个步骤:

(1)选取数据空间中的K个对象作为初始中心,每个对象代表一个聚类中心;

(2)对于样本中的数据对象,根据它们与这些聚类中心的欧氏距离,按距离最近的准则将它们分到距离它们最近的聚类中心(最相似)所对应的类;

(3)更新聚类中心:将每个类别中所有对象所对应的均值作为该类别的聚类中心,计算目标函数的值;

(4)判断聚类中心和目标函数的值是否发生改变,若不变,则输出结果,若改变,则返回2)。

import numpy as np
x=np.random.randint(1,100,[20,1])
y=np.zeros(20)
k=3

#初始聚类中心数组
def initcenter(x,k):
    return x[:k].reshape(k)
#数组中的值,与聚类中心最新距离所在类别的索引号
def nearest(kc,i):
    d = (abs(kc - i))
    w = np.where(d == np.min(d))       
    return w[0][0]
#对数组的每个组分类
def xclassify(x,y,kc):
    for i in range(x.shape[0]):
        y[i]=nearest(kc,x[i])
    return y
kc=initcenter(x,k)
y=xclassify(x,y,kc)
print(kc,y)
#计算各聚类新均值
def kcmean(x,y,kc,k):    
    l = list(kc)
    flag = False
    for c in range(k):
        m = np.where(y == c)
        n=np.mean(x[m])
        if m[0].shape != (0,):
            n = np.mean(x[m])
        if l[c] != n:
            l[c] = n
            flag = True     
        return (np.array(l),flag)

k = 3
kc = initcenter(x,k)
flag = True 
print(x,y,kc,flag)
while flag:
    y = xclassify(x,y,kc)
    kc,flag = kcmean(x, y, kc, k)
print(y,kc)
print(x,y)     

运行结果如下:

 

2. 鸢尾花花瓣长度数据做聚类并用散点图显示。

#加载numpy包
import numpy as np
#加载sklearn包
from sklearn.datasets import load_iris 
#读出鸢尾花数据集iris
iris=load_iris()

x=iris.data[:,1]
y=np.zeros(150)
#初始聚类中心数组
def initcenter(x,k):
    return x[0:k].reshape(k)
#数组中的值,与聚类中心最新距离所在类别的索引号
def nearest(kc,i):
    d = (abs(kc - i))
    w=np.where(d==np.min(d))
    return w[0][0]
#计算各聚类新均值
def kcmean(x, y, kc, k):  
    l = list(kc)
    flag = False
    for c in range(k):
        m = np.where(y == c)
        n = np.mean(x[m])
        if l[c] != n:
            l[c] = n
            flag = True  
    return (np.array(l), flag)
#对数组的每个组分类
def xclassify(x,y,kc):
    for i in range(x.shape[0]):
        y[i]=nearest(kc,x[i])
    return y

k = 3
kc = initcenter(x, k)
flag = True
print(x, y, kc, flag)
while flag:
    y = xclassify(x, y, kc)
    kc, flag = kcmean(x, y, kc, k)
print(y, kc, type(kc))

import matplotlib.pyplot as plt
plt.scatter(x,x,c=y,s=50,cmap='rainbow',marker='p',alpha=0.5)
plt.show()

运行结果如下:

 

3. 用sklearn.cluster.KMeans,鸢尾花花瓣长度数据做聚类并用散点图显示.

import numpy as np
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

iris= load_iris()
x=iris.data

petal_length = x[:, 2:3]
 
print(petal_length)
est = KMeans(n_clusters=3)
est.fit(petal_length)
kc = est.cluster_centers_
y_kmeans = est.predict(petal_length)

print(y_kmeans,kc)
print(kc.shape,y_kmeans.shape,np.shape)
plt.scatter(petal_length,np.linspace(1,150,150),c=y_kmeans,marker='o',cmap='rainbow')
plt.show()

运行结果如下:

 

4. 鸢尾花完整数据做聚类并用散点图显示。

import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans

iris=load_iris()
x=iris.data

est = KMeans(n_clusters = 3)
est.fit(x)
kc = est.cluster_centers_
y_kmeans = est.predict(x)  

print(y_kmeans,kc)
print(kc.shape,y_kmeans.shape,np.shape)

plt.scatter(x[:,0],x[:,1],c=y_kmeans,s=50,cmap='rainbow');
plt.show()

运行结果如下:

 

posted @ 2018-10-31 18:25  cxseven  阅读(382)  评论(0编辑  收藏  举报