链接:https://pan.baidu.com/s/1RWNVHuXMQleOrEi5vig_bQ
提取码:p57s
首先嘉定样本空间中的每个聚类均服从某种已知的概率分布规则, 然后用不同的概率密度函数拟合样本中的统计直方图, 不断移动密度函数的中心位置, 直到获得最佳拟合效果为止.这些概率密度函数的峰值点就是聚类的中心, 再根据每个样本距离各个中心的距离, 选择最近的聚类中心所属的类别作为该样本的类别.
均值漂移算法的特点:
均值漂移相关的API:
# x: 输入 n_samples: 样本数量 # quantile: 量化宽度 (直方图一条的宽度) bw = sc.estimate_bandwidth( x, n_samples=len(x), quantile=0.1) # 构建均值漂移模型 model = sc.MeanShift(bandwidth=bw)
案例: multiple3.txt
""" demo05_meanshift.py 均值漂移 """ import numpy as np import sklearn.cluster as sc import matplotlib.pyplot as mp x = np.loadtxt('../ml_data/multiple3.txt', delimiter=',') # 均值漂移实现聚类划分 bw = sc.estimate_bandwidth( x, n_samples=len(x), quantile=0.2) model = sc.MeanShift(bandwidth=bw) model.fit(x) centers = model.cluster_centers_ print(centers) pred_y = model.predict(x) # 划分聚类边界 l, r = x[:, 0].min()-1, x[:, 0].max()+1 b, t = x[:, 1].min()-1, x[:, 1].max()+1 n = 500 grid_x, grid_y = np.meshgrid( np.linspace(l, r, n), np.linspace(b, t, n)) mesh_x = np.column_stack((grid_x.ravel(), grid_y.ravel())) pred_mesh_y = model.predict(mesh_x) grid_z = pred_mesh_y.reshape(grid_x.shape) mp.figure('MeanShift', facecolor='lightgray') mp.title('MeanShift', fontsize=16) mp.xlabel('X',fontsize=14) mp.ylabel('Y',fontsize=14) mp.tick_params(labelsize=10) mp.pcolormesh(grid_x,grid_y,grid_z,cmap='gray') mp.scatter(x[:,0], x[:,1], c=pred_y, cmap='jet', label='points') # 绘制聚类中心点 mp.scatter(centers[:,0], centers[:,1], marker='+', s=230, c='orangered') mp.legend() mp.show()