python 聚类_使用python+sklearn实现聚类性能评估中随机分配对聚类度量值的影响
注意:单击此处https://urlify.cn/3iAzUr下载完整的示例代码,或通过Binder在浏览器中运行此示例
下图说明了聚类数量和样本数量对各种聚类性能评估度量指标的影响。未调整的度量指标(例如V度量)显示了聚类的数量与样本数之间的依赖关系:随机标记的平均V度量随着聚类的数量越接近用于计算的样本总数而显着增加。针对ARI等偶然性度量指标进行调整后,对于任意数量的样本和聚类,一些随机方差(variations)均以0.0的平均得分为中心。因此,只有调整后的度量指标才能安全地用作共识指数(consensus index),才能用来评估数据集中在各种重叠子样本上给定k值时,聚类算法的平均稳定性。
输出:
Computing adjusted_rand_score for 10 values of n_clusters and n_samples=100
done in 0.050s
Computing v_measure_score for 10 values of n_clusters and n_samples=100
done in 0.068s
Computing ami_score for 10 values of n_clusters and n_samples=100
done in 0.356s
Computing mutual_info_score for 10 values of n_clusters and n_samples=100
done in 0.044s
Computing adjusted_rand_score for 10 values of n_clusters and n_samples=1000
done in 0.051s
Computing v_measure_score for 10 values of n_clusters and n_samples=1000
done in 0.064s
Computing ami_score for 10 values of n_clusters and n_samples=1000
done in 0.208s
Computing mutual_info_score for 10 values of n_clusters and n_samples=1000
done in 0.048s
print(__doc__)
# 作者: Olivier Grisel
# 许可证: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from time import time
from sklearn import metrics
def uniform_labelings_scores(score_func, n_samples, n_clusters_range,
fixed_n_classes=None, n_runs=5, seed=42):
"""计算2个随机均一聚类标签的得分。
两个随机标签中每个在n_clusters_range中的可能值都具有相同数量的聚类。
当fixed_n_classes不为None时,第一个标签被认为是具有固定类数量的真实类(ground truth class)。
"""
random_labels = np.random.RandomState(seed).randint
scores = np.zeros((len(n_clusters_range), n_runs))
if fixed_n_classes is not None:
labels_a = random_labels(low=0, high=fixed_n_classes, size=n_samples)
for i, k in enumerate(n_clusters_range):
for j in range(n_runs):
if fixed_n_classes is None:
labels_a = random_labels(low=0, high=k, size=n_samples)
labels_b = random_labels(low=0, high=k, size=n_samples)
scores[i, j] = score_func(labels_a, labels_b)
return scores
def ami_score(U, V):
return metrics.adjusted_mutual_info_score(U, V)
score_funcs = [
metrics.adjusted_rand_score,
metrics.v_measure_score,
ami_score,
metrics.mutual_info_score,
]
# 2个独立的随机聚类,具有相同的聚类数
n_samples = 100
n_clusters_range = np.linspace(2, n_samples, 10).astype(np.int)
plt.figure(1)
plots = []
names = []
for score_func in score_funcs:
print("Computing %s for %d values of n_clusters and n_samples=%d"
% (score_func.__name__, len(n_clusters_range), n_samples))
t0 = time()
scores = uniform_labelings_scores(score_func, n_samples, n_clusters_range)
print("done in %0.3fs" % (time() - t0))
plots.append(plt.errorbar(
n_clusters_range, np.median(scores, axis=1), scores.std(axis=1))[0])
names.append(score_func.__name__)
plt.title("Clustering measures for 2 random uniform labelings\n"
"with equal number of clusters")
plt.xlabel('Number of clusters (Number of samples is fixed to %d)' % n_samples)
plt.ylabel('Score value')
plt.legend(plots, names)
plt.ylim(bottom=-0.05, top=1.05)
# 根据真实类标签使用不同的n_clusters随机标签
# 聚类数量固定
n_samples = 1000
n_clusters_range = np.linspace(2, 100, 10).astype(np.int)
n_classes = 10
plt.figure(2)
plots = []
names = []
for score_func in score_funcs:
print("Computing %s for %d values of n_clusters and n_samples=%d"
% (score_func.__name__, len(n_clusters_range), n_samples))
t0 = time()
scores = uniform_labelings_scores(score_func, n_samples, n_clusters_range,
fixed_n_classes=n_classes)
print("done in %0.3fs" % (time() - t0))
plots.append(plt.errorbar(
n_clusters_range, scores.mean(axis=1), scores.std(axis=1))[0])
names.append(score_func.__name__)
plt.title("Clustering measures for random uniform labeling\n"
"against reference assignment with %d classes" % n_classes)
plt.xlabel('Number of clusters (Number of samples is fixed to %d)' % n_samples)
plt.ylabel('Score value')
plt.ylim(bottom=-0.05, top=1.05)
plt.legend(plots, names)
plt.show()
脚本的总运行时间:(0分钟1.225秒)估计的内存使用量: 8 MB下载Python源代码: plot_adjusted_for_chance_measures.py
下载Jupyter notebook源代码: plot_adjusted_for_chance_measures.ipynb
由Sphinx-Gallery生成的画廊
文壹由“伴编辑器”提供技术支持
☆☆☆为方便大家查阅,小编已将scikit-learn学习路线专栏文章统一整理到公众号底部菜单栏,同步更新中,关注公众号,点击左下方“系列文章”,如图:
欢迎大家和我一起沿着scikit-learn文档这条路线,一起巩固机器学习算法基础。(添加微信:mthler,备注:sklearn学习,一起进【sklearn机器学习进步群】开启打怪升级的学习之旅。)
python 聚类_使用python+sklearn实现聚类性能评估中随机分配对聚类度量值的影响相关推荐
- 第一章 第一节:Python基础_认识Python
Python基础入门(全套保姆级教程) 第一章 第一节:Python基础_认识Python 1. 什么是编程 通俗易懂,编程就是用代码编写程序,编写程序有很多种办法,像c语言,javaPython语言 ...
- python层次聚类_用Python做层次聚类分析
欢迎关注 公众号:大邓和他的Python 层次分析属于聚类分析的一种,scipy有这方面的封装包. linkage函数从字面意思是链接,层次分析就是不断链接的过程,最终从n条数据,经过不断链接,最终聚 ...
- python 机器学习_基于 Python 语言的机器学习工具Sklearn
本篇介绍了Sklearn机器学习基础,从Sklearn的简介,模块以及与tensorflow的优劣势对比来了解基于Python语言的机器学习工具. 01.Sklearn简介 sklearn(sc ...
- java python算法_用Python,Java和C ++示例解释的排序算法
java python算法 什么是排序算法? (What is a Sorting Algorithm?) Sorting algorithms are a set of instructions t ...
- excel python插件_利用 Python 插件 xlwings 读写 Excel
Python 通过 xlwings 读取 Excel 数据 去年底公司让我做设备管理,多次委婉拒绝,最终还是做了.其实我比较喜欢技术.做管理后发现现场没有停机率统计,而原始数据有,每次要自己在Exce ...
- 网络安全用python吗_使用Python进行网络安全渗透——密码攻击测试器
相关文章: 本篇将会涉及: HTTP 基本认证 对HTTP Basic认证进行密码暴力攻击测试 什么是HTTP 基本认证 HTTP基本认证(HTTP Basic Authentication)是HTT ...
- 动态照片墙 python 实现_利用python生成照片墙的示例代码
这篇文章主要介绍了利用python生成照片墙的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 PIL(Python Im ...
- linux保存python文件_告诉Python将.txt文件保存到Linux上的某个目录 - python
我试图将文件保存在QCTestFiles目录中,但是我的脚本未将文件写入指定的目录中.我的路径文件是否适用于Linux,如果不是,我如何更改代码以在另一个目录中写入和保存文件?我当前的脚本在brper ...
- python字符串_(Python基础教程之七)Python字符串操作
Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...
最新文章
- 跨平台PHP调试器设计及使用方法——高阶封装
- FPGA黑金开发板mini版新鲜出炉!!!
- Shareplex 错误 can't open or read object cache file
- 一年级学情分析计算机,小学一年级语文学情分析范文
- Mac如何简单的翻录3D蓝光视频
- [转]xshell使用技巧
- 利用Vitis开发基于ZCU106的神经网络加速器(二)——DPU编译及Demo
- 网易企业邮箱:与POP3、WebMail相比,为什么Imap4更被接受
- 华为存储发展史:从筚路蓝缕到星辰大海
- 虚拟服务器钟楼古城微博,最无聊中外钟楼微博走红
- linux mysql ip_Linux下配置mysql允许指定IP远程访问
- Meta首份元宇宙白皮书9大看点,瞄准80万亿美元市场
- opencore黑苹果教程
- 操作系统 进程管理(一)——进程的含义与状态
- 分时调度和抢占式调度
- C:\windows\system32文件
- 属性选择器、结构伪类选择器、伪元素选择器
- Puppet nginx+Mongrel安装配置篇
- pvs-stdio ue4_使用PVS-Studio for C#在GitLab中分析合并请求
- 微信公众号 扫码自动回复消息
热门文章
- 工作如果没有方法,光靠苦力是不行的
- 机器学习常用术语词汇表
- LeetCode简单题之复写零
- 如何为应用选择最佳的FPGA(下)
- DMS是临时解决方案?
- HTTP/HTTPS抓包工具-Fiddler
- struct.error: ushort format requires 0 <= number <= 0xffff
- Ubuntu 系统打不开图片提示Fatal error reading PNG image File: Not a PNG file
- java中对Excel的创建、样式修改
- Spark-Spark setMaster WordCount Demo