文件下载地址:
链接: https://pan.baidu.com/s/1kXYmHGBpV7vrNdRk9UK1_A 提取码: 68pd
"""
聚类 均值漂移算法
量化带宽,决定每次调整概率密度函数的步进量
"""
import numpy as np
import sklearn.cluster as sc
import matplotlib.pyplot as mp
# 加载数据
x = np.loadtxt("./multiple3.txt", delimiter=",")
# 量化带宽 quantile 量化宽度
bw = sc.estimate_bandwidth(x, n_samples=len(x), quantile=0.1)
# 均值漂移算法 模型
model = sc.MeanShift(bandwidth=bw, bin_seeding=True)
model.fit(x)
centers = model.cluster_centers_
print(centers)
# 分类边界数据
n = 500
l, r = x[:, 0].min() - 1, x[:, 0].max() + 1
b, t = x[:, 1].min() - 1, x[:, 1].max() + 1
grid_x = np.meshgrid(np.linspace(l, r, n),
np.linspace(b, t, n))
flat_x = np.column_stack((grid_x[0].ravel(), grid_x[1].ravel()))
flat_y = model.predict(flat_x)
grid_y = flat_y.reshape(grid_x[0].shape)
prd_y = model.predict(x)
# 绘制结果
mp.figure('MeanShift Cluster', facecolor='lightgray')
mp.title('MeanShift Cluster', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
# 分类边界
mp.pcolormesh(grid_x[0], grid_x[1], grid_y, cmap='gray')
# 点数据
mp.scatter(x[:, 0], x[:, 1], c=prd_y, cmap="jet", s=80)
# 分类中心点
mp.scatter(centers[:, 0], centers[:, 1], marker='+', c='gold', s=1000, linewidth=3)
mp.show()