赞
踩
本文数据采用mnist dataset。
使用cluster_centers_
即可。
示例:将mnist数据集分为十个聚类,并绘制各个中心点。
import pandas as pd import matplotlib.pyplot as plt from sklearn.cluster import KMeans from sklearn.datasets import load_digits mnist = load_digits() data_a = mnist.data kmeans_a = KMeans(n_clusters = 10) kmeans_a.fit(data_a) from sklearn.decomposition import PCA centers = PCA(2).fit_transform(kmeans_a.cluster_centers_) plt.scatter(centers[:, 0], centers[:, 1]) plt.show()
我们可以使用disagree distance来评价两个聚类之间的差异。其定义为:
D
(
P
,
C
)
=
Σ
x
,
y
I
P
,
C
(
x
,
y
)
D(P, C)=\Sigma _{x, y} \mathbb{I}_{P, C}(x, y)
D(P,C)=Σx,yIP,C(x,y)
其中
I
P
,
C
(
x
,
y
)
=
{
1
,
若
P
与
C
在
x
,
y
的
的
归
属
问
题
上
不
同
0
\mathbb{I}_{P,C}(x,y)=\left\{ 1,若P与C在x,y的的归属问题上不同0
举个例子:下表为对
x
1
x_1
x1至
x
5
x_5
x5,
P
P
P,
C
C
C两聚类给出的标签:
P | C | |
---|---|---|
x 1 x_1 x1 | 1 | 1 |
x 2 x_2 x2 | 1 | 2 |
x 3 x_3 x3 | 2 | 1 |
x 4 x_4 x4 | 3 | 3 |
x 5 x_5 x5 | 3 | 4 |
对于
x
1
,
x
2
x_1, x_2
x1,x2,
P
P
P认为它们标签相同, 均为1, 而
C
C
C认为它们属不同类,因此diagree distance加上1.
对于
x
1
,
x
4
x_1, x_4
x1,x4,
P
P
P认为它们标签不同,
C
C
C 也认为它们标签不同,因此两个聚类在这两个数据上达成一致, diagree distance加上0.
示例:计算上一节进行的10分类与原标签的disagree distance
labels_b = mnist.target
labels_a = kmeans_a.labels_
disagreement_dis = 0
for i in range(len(labels_b)-1):
for j in range(i+1, len(labels_b)):
if (labels_a[i] == labels_a[j]) != (labels_b[i] == labels_b[j]):
disagreement_dis += 1
print(disagreement_dis)
99161
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。