python - 虹膜数据在哪一部分接收到标记簇?

标签 python scikit-learn k-means

我将使用 sklearn 对我公司的一个项目的数据进行聚类。在开始部分,我必须证明我能够对数据进行聚类。在 R 中,这对我来说没有问题,但 R 与 HBase 一起使用并不那么容易。我不想拖延,但问题是我不知道数据点在什么时候收到标签。另外,这是一个 3D 绘图,那么为什么 X (iris.data) 每个有 4 个数字 ([ 5.4 3.9 1.3 0.4])数据点?

我真正需要的是知道哪个数据点对应于哪个集群。我不需要视觉效果。

这是代码(取自 here )

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


from sklearn.cluster import KMeans
from sklearn import datasets

np.random.seed(5)

centers = [[1, 1], [-1, -1], [1, -1]]
iris = datasets.load_iris()
X = iris.data
y = iris.target

estimators = {'k_means_iris_3': KMeans(n_clusters=3),
              'k_means_iris_8': KMeans(n_clusters=8),
              'k_means_iris_bad_init': KMeans(n_clusters=3, n_init=1,
                                              init='random')}


fignum = 1
for name, est in estimators.items():
    fig = plt.figure(fignum, figsize=(4, 3))
    plt.clf()
    ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

    plt.cla()
    est.fit(X)
    labels = est.labels_

    ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=labels.astype(np.float))

    ax.w_xaxis.set_ticklabels([])
    ax.w_yaxis.set_ticklabels([])
    ax.w_zaxis.set_ticklabels([])
    ax.set_xlabel('Petal width')
    ax.set_ylabel('Sepal length')
    ax.set_zlabel('Petal length')
    fignum = fignum + 1

# Plot the ground truth
fig = plt.figure(fignum, figsize=(4, 3))
plt.clf()
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

plt.cla()

for name, label in [('Setosa', 0),
                    ('Versicolour', 1),
                    ('Virginica', 2)]:
    ax.text3D(X[y == label, 3].mean(),
              X[y == label, 0].mean() + 1.5,
              X[y == label, 2].mean(), name,
              horizontalalignment='center',
              bbox=dict(alpha=.5, edgecolor='w', facecolor='w'))
# Reorder the labels to have colors matching the cluster results
y = np.choose(y, [1, 2, 0]).astype(np.float)
ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y)

ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
plt.show()

最佳答案

标签

这是在代码中添加两个 print 语句的结果,它将在生成标签时向您显示。

for name, est in estimators.items():
    print est
    fig = plt.figure(fignum, figsize=(4, 3))
    plt.clf()
    ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

    plt.cla()
    est.fit(X)
    labels = est.labels_
    print labels

est 显示所使用的估计器的参数。正如您所看到的,第一个有 8 个簇,由 标签 中的 0-7 个簇分配反射(reflect)出来。

KMeans(copy_x=True, init='k-means++', max_iter=300, n_clusters=8, n_init=10,
    n_jobs=1, precompute_distances=True, random_state=None, tol=0.0001,
    verbose=0)
[1 5 5 5 1 1 5 1 5 5 1 5 5 5 1 1 1 1 1 1 1 1 5 1 5 5 1 1 1 5 5 1 1 1 5 5 1
 5 5 1 1 5 5 1 1 5 1 5 1 5 2 2 2 7 2 7 2 6 2 7 6 7 7 2 7 2 7 7 2 7 4 7 4 2
 2 2 2 2 2 7 7 7 7 4 7 2 2 2 7 7 7 2 7 6 7 7 7 2 6 7 0 4 3 0 0 3 7 3 0 3 0
 4 0 4 4 0 0 3 3 4 0 4 3 4 0 3 4 4 0 3 3 3 0 4 4 3 0 0 4 0 0 0 4 0 0 0 4 0
 0 4]
KMeans(copy_x=True, init='k-means++', max_iter=300, n_clusters=3, n_init=10,
    n_jobs=1, precompute_distances=True, random_state=None, tol=0.0001,
    verbose=0)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 1 1 1 1 2 1 1 1 1
 1 1 2 2 1 1 1 1 2 1 2 1 2 1 1 2 2 1 1 1 1 1 2 1 1 1 1 2 1 1 1 2 1 1 1 2 1
 1 2]
KMeans(copy_x=True, init='random', max_iter=300, n_clusters=3, n_init=1,
    n_jobs=1, precompute_distances=True, random_state=None, tol=0.0001,
    verbose=0)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 2 1 2 2 2 2
 2 2 1 1 2 2 2 2 1 2 1 2 1 2 2 1 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2
 2 1]

尺寸

如果您查看 hereiris 数据集有 4 个维度(属性) ,你会看到有 4 个维度。本示例中未绘制的一维是萼片宽度。您可以通过将 print iris 放在 iris = datasets.load_iris() 后面来查看每个数据点对应的内容。它打印出很多信息,但重要的信息位于底部(顺便说一句,不太漂亮)。看起来像这样-

:Attribute Information:\n        - sepal length in cm\n        - sepal width in cm\n        - petal length in cm\n        - petal width in cm

属性对应X[flower][0]、X[flower][1]、X[flower][2]、X[flower][3]。

作业

要查看每个数据点的聚类分配,请在 labels = est.labels_ 下面添加此内容:

for flower in range(len(labels)):
        print (X[flower],labels[flower])

将为您提供下面的结果,仅显示一种访问数据点集群分配的方法,您可能不关心打印它们,而是将它们存储在有意义的地方。

(array([ 5.1,  3.5,  1.4,  0.2]), 1)
(array([ 4.9,  3. ,  1.4,  0.2]), 5)
(array([ 4.7,  3.2,  1.3,  0.2]), 5)

关于python - 虹膜数据在哪一部分接收到标记簇?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34884175/

相关文章:

Python 安装工具 : first build from sources then install

python - 如何在 Python 中设置 Gtk 图像的大小

python - Predict_proba(np.array(test)) 的类型错误

hadoop - 如何在不耗尽内存的情况下运行大型 Mahout 模糊 kmeans 聚类?

python - KIVY:按钮中的图像被拉伸(stretch)

python - 如何在 TensorFlow 中模拟降低精度的 float ?

python - 拟合函数返回 TypeError : float() argument must be string or a number in ScikitLearn

python-3.x - 选择反射(reflect) python 中每个结果类别影响因素的分类模型

python - 图像压缩后得到的灰度图像

opencv - 使用Kmeans功能的输出中心对数据进行聚类