使用肘部法确定k-means均值的k值
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值的更多相关文章
- 机器学习 —— 基础整理(三)生成式模型的非参数方法: Parzen窗估计、k近邻估计;k近邻分类器
本文简述了以下内容: (一)生成式模型的非参数方法 (二)Parzen窗估计 (三)k近邻估计 (四)k近邻分类器(k-nearest neighbor,kNN) (一)非参数方法(Non-param ...
- [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行,最 ...
- 一些简单的问题. 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 ...
- 快速排序/快速查找(第k个, 前k个问题)
//快速排序:Partition分割函数,三数中值分割 bool g_bInvalidInput = false; int median3(int* data, int start, int end) ...
- 在数组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数组从大到小与新数组比较,如 ...
- [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 ...
- [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 ...
- [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 ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
随机推荐
- 二级数组省市表(二维数组json)
<一省份.城市二级联动+vue搭架> a. template部分 <section class="edit__place"> <select v-mo ...
- 《TensorFlow2深度学习》学习笔记(四)对笔记二中的模型增加正确率展示
全部代码如下:(红色部分为与笔记二不同之处) #1.Import the neccessary libraries needed import numpy as np import tensorflo ...
- 《exception》第九次团队作业:Beta冲刺与验收准备(第一天)
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件黑盒测试技术:2.学会编制软件项目 ...
- nginx和tomcat配置负载均衡和session同步
一.背景 因业务需求,现需配置多台服务器,实现负载均衡. 二.解决方案 使用 nginx + tomcat,在这一台应用服务器部署一个nginx和两个tomcat.通过nginx修改配置后reload ...
- [CSS3] Use media query to split css files and Dark mode (prefers-color-scheme: dark)
Dark Mode: :root { --text-color: #000; --background-color: #fff; } body { color: var(--text-color); ...
- Python连接oracle数据库 例子一
step1:下载cx_Oracle模块,cmd--pip install cx_Oracle step2: 1 import cx_Oracle #引用模块cx_Oracle 2 conn=cx_Or ...
- java实现ssh登录linux服务器并下发命令
依赖jar包:jsch-0.1.55.jar commons-io-2.5.jar import com.jcraft.jsch.ChannelExec; import com.jcraft.js ...
- comlink 是来自google chrome 团队的简化webwokers 开发的类库
comlink 可以帮助我们简单webworkers 的开发,同时很小(1.1kb),具体使用我们可以看下面 一张图 说明 comlink 使用起来也比较方便,官方也提供了完整的api 文档 参考资 ...
- linux patch 简单学习
使用patch 我们可以方便的进行软件补丁包处理,以下演示一个简单的c 项目补丁处理 原代码 app.c #include <stdio.h> int main(){ printf(&qu ...
- 使用vue+mintui 开发省市区功能
做移动端的都知道 经常会有省市区这种三级联动的功能 今天研究了一下午~ 1.准备工作 vue+mintui+省市区的json数据 下载地址:https://github.com/chzm/addres ...