聚类算法学习----之----sklearn.cluster.KMeans

本文详细介绍了sklearn.cluster.KMeans聚类算法的输入参数,包括n_clusters、init、n_init、max_iter、tol等,并解释了各参数的作用和默认值。此外,还提到了算法的收敛条件、预计算距离的选项以及如何选择进程数量。文章最后讨论了算法的属性,如cluster_centers_、labels_和inertia_,并提供了相关示例和参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class sklearn.cluster.KMeans(n_clusters=8, init=’k-means++’, n_init=10, max_iter=300, tol=0.0001, precompute_distances=’auto’, verbose=0, random_state=None, copy_x=True, n_jobs=1, algorithm=’auto’)


(一)输入参数:

(1)n_clusters:分成的簇数(要生成的质心数)=====>整型,[可选],默认值=8;

     n_clusters : int, optional, default: 8
          The number of clusters to form as well as the number of centroids to generate.

(2)init:初始化质心的方法====>有三个可选值:'k-means++', 'random',或者传递一个ndarray向量,默认为'k-means++'

     ‘k-means++’ 用一种智能的方法选定初始质心从而能加速迭代过程的收敛,参见 k_init 的解释获取更多信息。
       ‘random’ 随机从训练数据中选取初始质心。
       如果传递的是一个ndarray,则应该形如 (n_clusters, n_features) 并给出初始质心。

    init : {‘k-means++’, ‘random’ or an ndarray}

 Method for initialization, defaults to ‘k-means++’:

‘k-means++’ : selects initial cluster centers for k-mean clustering in a smart way to speed up convergence. See section Notes in k_init for more details.

‘random’: choose k observations (rows) at random from data for the initial centroids.

If an ndarray is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.


(3)n_init::用不同的质心初始化值运行算法的次数====>整型,默认值=10次,最终解是在inertia意义下选出的最优结果。

(ps:每一次算法运行时开始的centroid seeds是随机生成的, 这样得到的结果也可能有好有坏. 所以要运行算法n_init次, 取其中最好的。)

n_init : int, default: 10

        Number of time the k-means algorithm will be run with different centroid seeds. The final results will be the best output of n_init consecutive runs in terms of inertia.

(4)max_iter:算法每次迭代的最大次数====>整型,默认值=300

max_iter : int, default: 300

Maximum number of iterations of the k-means algorithm for a single run.

(5)tol:与inertia结合来确定收敛条件====> float型,默认值= 1e-4

   tol : float, default: 1e-4

Relative tolerance with regards to inertia to declare convergence

(6)precompute_distances:预计算距离,计算速度更快但占用更多内存 ====>类型:(auto,True,False)三个值可选,,默认值=“auto”

auto’:如果样本数乘以聚类数大于 12million 的话则不预计算距离‘’

‘True‘:总是预先计算距离。

‘False‘:永远不预先计算距离。

这个参数会在空间和时间之间做权衡,如果是True 会把整个距离矩阵都放到内存中,auto 会默认在数据样本大于featurs*samples 的数量大于12e6 的时候False,False时

核心实现的方法是利用Cpython 来实现的

  precompute_distances : {‘auto’, True, False}

Precompute distances (faster but takes more memory).

‘auto’ : do not precompute distances if n_samples * n_clusters > 12 million. This corresponds to about 100MB overhead per job using doubleprecision.

True : always precompute distances

False : never precompute distances

(7)verbose:是否输出详细信息====>类型:整型,默认值=0

  verbose : int, default 0

Verbosity mode.

(8)random_state: 用于初始化质心的生成器(generator),和初始化中心有关。

    random_state : int, RandomState instance or None, optional, default: None

If int, random_state is the seed used by the random number generator;

If RandomState instance, random_state is the random numbergenerator; 

If None, the random number generator is the RandomState instance used by np.random.

(9)copy_x:是否对输入数据继续copy 操作====> 布尔型,默认值=True

  

本程序是在python中完成,基于sklearn.cluster中的k-means聚类包来实现数据的聚类,对于里面使用的数据格式如下:(注意更改程序中的相关参数) 138 0 124 1 127 2 129 3 119 4 127 5 124 6 120 7 123 8 147 9 188 10 212 11 229 12 240 13 240 14 241 15 240 16 242 17 174 18 130 19 132 20 119 21 48 22 37 23 49 0 42 1 34 2 26 3 20 4 21 5 23 6 13 7 19 8 18 9 36 10 25 11 20 12 19 13 19 14 5 15 29 16 22 17 13 18 46 19 15 20 8 21 33 22 41 23 69 0 56 1 49 2 40 3 52 4 62 5 54 6 32 7 38 8 44 9 55 10 70 11 74 12 105 13 107 14 56 15 55 16 65 17 100 18 195 19 136 20 87 21 64 22 77 23 61 0 53 1 47 2 33 3 34 4 28 5 41 6 40 7 38 8 33 9 26 10 31 11 31 12 13 13 17 14 17 15 25 16 17 17 17 18 14 19 16 20 17 21 29 22 44 23 37 0 32 1 34 2 26 3 23 4 25 5 25 6 27 7 30 8 25 9 17 10 12 11 12 12 12 13 7 14 6 15 6 16 12 17 12 18 39 19 34 20 32 21 34 22 35 23 33 0 57 1 81 2 77 3 68 4 61 5 60 6 56 7 67 8 102 9 89 10 62 11 57 12 57 13 64 14 62 15 69 16 81 17 77 18 64 19 62 20 79 21 75 22 57 23 73 0 88 1 75 2 70 3 77 4 73 5 72 6 76 7 76 8 74 9 98 10 90 11 90 12 85 13 79 14 79 15 88 16 88 17 81 18 84 19 89 20 79 21 68 22 55 23 63 0 62 1 58 2 58 3 56 4 60 5 56 6 56 7 58 8 56 9 65 10 61 11 60 12 60 13 61 14 65 15 55 16 56 17 61 18 64 19 69 20 83 21 87 22 84 23 41 0 35 1 38 2 45 3 44 4 49 5 55 6 47 7 47 8 29 9 14 10 12 11 4 12 10 13 9 14 7 15 7 16 11 17 12 18 14 19 22 20 29 21 23 22 33 23 34 0 38 1 38 2 37 3 37 4 34 5 24 6 47 7 70 8 41 9 6 10 23 11 4 12 15 13 3 14 28 15 17 16 31 17 39 18 42 19 54 20 47 21 68 22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值