第八次作业
聚类--K均值算法:自主实现与sklearn.cluster.KMeans调用
1.用python实现K均值算法
K-means是一个反复迭代的过程,算法分为四个步骤:
(x,k,y)
1) 选取数据空间中的K个对象作为初始中心,每个对象代表一个聚类中心;
def initcenter(x, k): kc
2) 对于样本中的数据对象,根据它们与这些聚类中心的欧氏距离,按距离最近的准则将它们分到距离它们最近的聚类中心(最相似)所对应的类;
def nearest(kc, x[i]): j
def xclassify(x, y, kc):y[i]=j
3) 更新聚类中心:将每个类别中所有对象所对应的均值作为该类别的聚类中心,计算目标函数的值;
def kcmean(x, y, kc, k):
4) 判断聚类中心和目标函数的值是否发生改变,若不变,则输出结果,若改变,则返回2)。
while flag:
y = xclassify(x, y, kc)
kc, flag = kcmean(x, y, kc, k)
2. 鸢尾花花瓣长度数据做聚类并用散点图显示。
3. 用sklearn.cluster.KMeans,鸢尾花花瓣长度数据做聚类并用散点图显示.
4. 鸢尾花完整数据做聚类并用散点图显示.
import numpy as np x = np.random.randint(1,100,[20,1]) y = np.zeros(20) k = 3 #1) 选取数据空间中的K个对象作为初始中心,每个对象代表一个聚类中心; def initcen(x,k): return x[:k] #2) 对于样本中的数据对象,根据它们与这些聚类中心的欧氏距离,按距离最近的准则将它们分到距离它们最近的聚类中心(最相似)所对应的类; 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 #3) 更新聚类中心:将每个类别中所有对象所对应的均值作为该类别的聚类中心,计算目标函数的值; def kcmean(x,y,kc,k): l = list(kc) flag = False for c in range(k): m = np.where(y ==0) n = np.mean(x[m]) if l[c] != n: l[c] = n flag = True print(l,flag) return (np.array(l),flag) #4) 判断聚类中心和目标函数的值是否发生改变,若不变,则输出结果,若改变,则返回2) kc = initcen(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)

# 鸢尾花花瓣长度数据做聚类并用散点图显示。 import numpy as np from sklearn.datasets import load_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 xclassify(x, y, kc): for i in range(x.shape[0]): # 对数组的每个值进行分类,shape[0]读取矩阵第一维度的长度 y[i] = nearest(kc, x[i]) return y def kcmean(x, y, kc, k): # 计算各聚类新均值 l = list(kc) flag = False for c in range(k): print(c) m = np.where(y == c) n = np.mean(x[m]) if l[c] != n: l[c] = n flag = True # 聚类中心发生变化 print(l, flag) return (np.array(l), flag) k = 3 kc = initcenter(x, k) flag = True print(x, y, kc, flag) # 判断聚类中心和目标函数的值是否发生改变,若不变,则输出结果,若改变,则返回2 while flag: y = xclassify(x, y, kc) kc, flag = kcmean(x, y, kc, k) print(y, kc, type(kc)) print(x, y) import matplotlib.pyplot as plt plt.scatter(x, x, c=y, marker='*',alpha=0.5,linewidths=4, cmap="Paired"); plt.show()




#用sklearn.cluster.KMeans,鸢尾花花瓣长度数据做聚类并用散点图显示 import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_iris iris = load_iris() S = iris.data S from sklearn.cluster import KMeans iris_length = S[:, 2:3] k_means = KMeans(n_clusters=3) result = k_means.fit(iris_length) kc1 = result.cluster_centers_ y_kmeans = k_means.predict(iris_length) plt.scatter(iris_length,np.linspace(1,150,150),c=y_kmeans,marker='x',cmap='rainbow',linewidths=4) plt.show()

#鸢尾花完整数据做聚类并用散点图显示. k_means1 = KMeans(n_clusters=3) result1 = k_means1.fit(S) kc2 = result1.cluster_centers_ y_kmeans1 = k_means1.predict(S) print(y_kmeans1, kc2) print(kc2.shape, y_kmeans1.shape, S.shape) plt.scatter(S[:, 0], S[:, 1], c=y_kmeans1, marker='p', cmap='flag', linewidths=4, alpha=0.6) plt.show()



浙公网安备 33010602011771号