使用肘部法确定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个高频元素
随机推荐
- 静态Pod
静态Pod kubernetes 除了我们常用的普通Pod外,还有一种特殊的Pod,叫静态Pod. 概念 静态Pod是直接由节点kubelet进程来管理的,不能通过apiserver的master节点 ...
- 百度语音合成api/sdk及demo
1.流程 1)换取token 用Api Key 和 SecretKey.访问https://openapi.baidu.com/oauth/2.0/token 换取 token // appKey = ...
- jni接口
https://www.jianshu.com/p/d4a502420a89 #pragma once /*DO NOT EDIT THIS FILE - it is machine generate ...
- Python使用pip安装TensorFlow模块
1.首先确保已经安装python,然后用pip来安装matplotlib模块. 2.进入到cmd窗口下,建议执行python -m pip install -U pip setuptools进行升级. ...
- Python 通过lxml遍历html xpath
#coding:utf-8 ''' Created on 2017年10月9日 @author: li.liu ''' from selenium import webdriver from lxml ...
- win10在ip变动之后重启ftp
自己的笔记本拿去公司工作一周后,回到家里发现自己的ftp服务器没办法访问了. 首先,确定自己的ftp服务是打开的 接着搜索internet information service打开 接着点击网站查看 ...
- Centos7安装Hadoop2.7
准备 1.三台Centos7的机器: hostname IP地址 部署规划 node1 172.20.0.4 NameNode.DataNode node2 172.20.0.5 DataNode n ...
- 2020年假期sql excel文件 获取
下载地址: https://files.cnblogs.com/files/shmily3929/2020.zip 说明:sql 不区分节假期和周六周末 excel文件区分节假日和周六周末
- ASP.net MVC C# 当前上下文中不存在名称"viewbag"
出现的错误如下: 错误 2 当前上下文中不存在名称“model” e:\Stuff\projects\蓝狐软件工作室\src\Lanhu.Admin\Views\Student\Index.cshtm ...
- 嘉立创制作PCB流程
现在做板子基本上是选择嘉立创和捷配,今天看一下嘉立创如何下PCB和STM贴片单,改天再写一下捷配的下单 我喜欢用下单助手,比较方便 注意需要把自己的板子的PCB文件用压缩软件生成压缩包文件,名字自己取 ...