一、聚类:物以类聚
数组可以进行聚类,并找到数组的聚类中心。使用的第三方库是scipy,需要pip install scipy,先安装该库。数组聚类代码:
import numpy as np
from scipy.cluster.vq import vq, kmeans, whiten
import matplotlib.pyplot as plt
fe = np.array([[1.9,2.0],
[1.7,2.5],
[1.6,3.1],
[0.1,0.1],
[0.8,0.3],
[0.4,0.3],
[0.22,0.1],
[0.4, 0.3],
[0.4,0.5],
[1.8,1.9]])
book = np.array((fe[0], fe[1]))
print(type(book))
print("book:\n",book)
codebook, distortion = kmeans(fe,book)
print("codebook:",codebook)
print("distortion:",distortion)
plt.scatter(fe[:,0],fe[:,1],c="g")
plt.scatter(codebook[:,0],codebook[:,1],c="r")
plt.show()#红色的为聚类中心
运行结果: