import numpy as np
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt c1x = np.random.uniform(0.5, 1.5, (1, 10))
c1y = np.random.uniform(0.5, 1.5, (1, 10))
c2x = np.random.uniform(3.5, 4.5, (1, 10))
c2y = np.random.uniform(3.5, 4.5, (1, 10))
x = np.hstack((c1x, c2x))
y = np.hstack((c1y, c2y))
X = np.vstack((x, y)).T K = range(1, 10)
meanDispersions = []
for k in K:
kmeans = KMeans(n_clusters=k)
kmeans.fit(X)
#理解为计算某个与其所属类聚中心的欧式距离
#最终是计算所有点与对应中心的距离的平方和的均值
meanDispersions.append(sum(np.min(cdist(X, kmeans.cluster_centers_, 'euclidean'), axis=1)) / X.shape[0]) plt.plot(K, meanDispersions, 'bx-')
plt.xlabel('k')
plt.ylabel('Average Dispersion')
plt.title('Selecting k with the Elbow Method')
plt.show()

X为:

[[0.84223858 1.18059879]
[0.84834276 0.84499409]
[1.13263229 1.34316399]
[0.95487981 0.59743761]
[0.81646041 1.32361288]
[0.90405171 0.54047701]
[1.2723004 1.3461647 ]
[0.52939142 1.03325549]
[0.84592514 0.74344317]
[1.07882783 1.4286598 ]
[3.71702311 3.97510452]
[3.95476036 3.83842502]
[4.4297804 3.91854623]
[4.08686159 4.15798624]
[3.90406684 3.84413461]
[4.32395689 4.06825926]
[4.23112269 3.78578326]
[3.70602931 4.08608482]
[3.58690191 4.37072349]
[4.38564657 4.02168693]]

随着K的增加,纵轴呈下降趋势且最终趋于稳定,那么拐点肘部处的位置所对应的k 值,不妨认为是相对最佳的类聚数量值。

使用肘部法确定k-means均值的k值的更多相关文章

  1. 机器学习 —— 基础整理(三)生成式模型的非参数方法: Parzen窗估计、k近邻估计;k近邻分类器

    本文简述了以下内容: (一)生成式模型的非参数方法 (二)Parzen窗估计 (三)k近邻估计 (四)k近邻分类器(k-nearest neighbor,kNN) (一)非参数方法(Non-param ...

  2. [CareerCup] 13.1 Print Last K Lines 打印最后K行

    13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...

  3. 一些简单的问题. 2的10次方与k (涉及到b k m的要用乘来解读)

    2的10次方是k k就表示2的10次方 2的16次方,解读为 2的6次方(64)*2的10次方(k)  简写为64k    64k=64*k 同理2的20次方  解读为2的10次方*2的10次方  k ...

  4. 快速排序/快速查找(第k个, 前k个问题)

    //快速排序:Partition分割函数,三数中值分割 bool g_bInvalidInput = false; int median3(int* data, int start, int end) ...

  5. 在数组a中,a[i]+a[j]=a[k],求a[k]的最大值,a[k]max——猎八哥fly

    在数组a中,a[i]+a[j]=a[k],求a[k]的最大值,a[k]max. 思路:将a中的数组两两相加,组成一个新的数组.并将新的数组和a数组进行sort排序.然后将a数组从大到小与新数组比较,如 ...

  6. [LeetCode] Top K Frequent Words 前K个高频词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  7. [LeetCode] K Inverse Pairs Array K个翻转对数组

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  8. [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  9. Top K Frequent Elements 前K个高频元素

    Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素

随机推荐

  1. AD10生成Gerber文件详细步骤

    参考:https://wenku.baidu.com/view/faf0363c195f312b3069a5d2.html

  2. lock in share mode 和 select for update

    lock in share mode 和 select for update 2018年07月11日 01:57:58 道不虚行只在人 阅读数 146    版权声明:欢迎转载,请注明出处 https ...

  3. 第一次使用Git(常用的dos命令整理)

    在使用git的过程中,有许多dos命令也要会用才行 Git 工具分类 命令行 bash.cmd.power shell GUI Git GUI.GitHub Desktop IDE 集成 Visual ...

  4. InnoDB存储引擎与MyIsam存储引擎的区别

    特性比较 mysql5.5之后默认的存储引擎为InnoDB,在此之前默认存储引擎是MyIsam 特点 MyIsam InnoDB 锁机制 表锁 行锁 事务 不支持 支持 外键 不支持 支持 B树索引 ...

  5. Codeforces C. Jzzhu and Cities(dijkstra最短路)

    题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. Good Numbers(HDU5447+唯一分解)

    题目链接 传送门 题面 题意 首先定义对于\(k\)的好数\(u\):如果\(u\leq k\)且\(u\)的所有质因子与\(k\)的质因子一样则称\(u\)对于\(k\)是一个好数. 现给你两个数\ ...

  7. winafl 源码分析

    前言 winafl 是 afl 在 windows 的移植版, winafl 使用 dynamorio 来统计代码覆盖率,并且使用共享内存的方式让 fuzzer 知道每个测试样本的覆盖率信息.本文主要 ...

  8. Python萌新笔记

    Mychael上了大学,对Python产生了浓厚的兴趣,便开始了Python的学习 学习的时候,感觉Python确实比以往学的C++表达简洁很多,而又不失强大 以后的学习笔记就记在这啦 变量 Pyth ...

  9. 分库分表相关 - hash与range结合去分库分表

    相关文章1 整体看下来通过hash取模去分库,然后根据range去分到哪个区间的表中. 具体还要实践下来.

  10. 自助法(Bootstraping)

    自助法(Bootstraping)是另一种模型验证(评估)的方法(之前已经介绍过单次验证和交叉验证:验证和交叉验证(Validation & Cross Validation)).其以自助采样 ...