0


人工智能|机器学习——Canopy聚类算法(密度聚类)

1.简介

Canopy聚类算法是一个将对象分组到类的简单、快速、精确地方法。每个对象用多维特征空间里的一个点来表示。这个算法使用一个快速近似距离度量和两个距离阈值T1 > T2 处理。

Canopy聚类很少单独使用, 一般是作为k-means前不知道要指定k为何值的时候,用Canopy聚类来判断k的取值

2.算法步骤

输入:所有点的集合D, 超参数:T1 , T2 , 且 T1 > T2

输出:聚类好的集合

  1. 注意
  • 当T1过大时,会使许多点属于多个Canopy,可能会造成各个簇的中心点间距离较近,各簇间区别不明显;
  • 当T2过大时,增加强标记数据点的数量,会减少簇个个数;
  • T2过小,会增加簇的个数,同时增加计算时间;

一幅图说明算法:

内圈的一定属于该类, 外圈的一定不属于该类, 中间层的可能属于别的类(因为不止一个聚类中心, 他可能属于别的类的内圈);

3.python实现

对iris数据集做Canopy聚类, 半径分别设置为1和2

  1. #%% Canopy聚类
  2. import pandas as pd
  3. from sklearn.cluster import KMeans
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import copy
  7. class Solution(object):
  8. def Canopy(self, x, t1, t2):
  9. '''
  10. Parameters
  11. ----------
  12. x : array
  13. 数据集.
  14. t1 : float
  15. 外圈半径.
  16. t2 : float
  17. 内圈半径.
  18. Returns
  19. -------
  20. result: list.
  21. 聚好类的数据集
  22. '''
  23. if t1 < t2:
  24. return print("t1 应该大于 t2")
  25. x = copy.deepcopy(x)
  26. result = [] # 用于存放最终结果
  27. index = np.zeros((len(x),)) # 用于标记外圈外的点 1表示强标记, 2表示弱标记
  28. while (index == np.zeros((len(x),))).any():
  29. alist = [] # 用于存放某一类的数据集
  30. choice_index = None
  31. for i, j in enumerate(index):
  32. if j == 0:
  33. choice_index = i
  34. break
  35. C = copy.deepcopy(x[choice_index])
  36. alist.append(C)
  37. x[choice_index] = np.zeros((1, len(x[0])))
  38. index[choice_index] = 1
  39. for i,a in enumerate(x):
  40. if index[i] != 1:
  41. distant = (((a-C)**2).sum())**(1/2)
  42. if distant <= t2: # 打上强标记
  43. alist.append(copy.deepcopy(x[i]))
  44. x[i] = np.zeros((1, len(x[0])))
  45. index[i] = 1
  46. elif distant <= t1:
  47. index[i] = 2
  48. result.append(alist)
  49. return result
  50. def pint(r, x, y, c):
  51. # 点的横坐标为a
  52. a = np.arange(x-r,x+r,0.0001)
  53. # 点的纵坐标为b
  54. b = np.sqrt(np.power(r,2)-np.power((a-x),2))
  55. plt.plot(a,y+b,color=c,linestyle='-')
  56. plt.plot(a,y-b,color=c,linestyle='-')
  57. plt.scatter(x, y, c='r',marker='*')
  58. if __name__ == '__main__':
  59. data = pd.read_csv(r'C:/Users/潘登/Documents/python全系列/人工智能/iris.csv')
  60. X = np.array(data.iloc[:, 2:4])
  61. Y = data['species']
  62. result = Solution().Canopy(X, 2, 1)
  63. x1 = []
  64. y1 = []
  65. for i in result[0]:
  66. x1.append(i[0])
  67. y1.append(i[1])
  68. x2 = []
  69. y2 = []
  70. for i in result[1]:
  71. x2.append(i[0])
  72. y2.append(i[1])
  73. x3 = []
  74. y3 = []
  75. for i in result[2]:
  76. x3.append(i[0])
  77. y3.append(i[1])
  78. plt.figure(figsize=(16,12))
  79. plt.scatter(X[:,0], X[:,1], s=50, c='violet', marker='s')
  80. plt.scatter(x1, y1, s=50, c='orange', marker='s')
  81. plt.scatter(x2, y2, s=50, c='lightblue', marker='s')
  82. plt.scatter(x3, y3, s=50, c='blue', marker='s')
  83. pint(2, x1[0], y1[0], 'b')
  84. pint(1, x1[0], y1[0], 'y')
  85. pint(2, x2[0], y2[0], 'b')
  86. pint(1, x2[0], y2[0], 'y')
  87. pint(2, x3[0], y3[0], 'b')
  88. pint(1, x3[0], y3[0], 'y')
  89. plt.xlim([0, 8])
  90. plt.ylim([-3, 5])
  91. plt.show()

+结果如下:


本文转载自: https://blog.csdn.net/admin_maxin/article/details/136574073
版权归原作者 博士僧小星 所有, 如有侵权,请联系我们删除。

“人工智能|机器学习——Canopy聚类算法(密度聚类)”的评论:

还没有评论