首页 > 其他 > 详细

3D聚类

时间:2019-10-17 22:43:56      阅读:80      评论:0      收藏:0      [点我收藏+]

1 3D聚类和普通的二维聚类实质一样,只不过维数太高了,用三维图来表示了.

下面将官网的改成只生成一个图了

技术分享图片
#!/usr/bin/python
# -*- coding:utf-8 -*-

print(__doc__)


# Code source: Gaël Varoquaux
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause

import numpy as np
import matplotlib.pyplot as plt
# Though the following import is not directly being used, it is required
# for 3D projection to work
from mpl_toolkits.mplot3d import Axes3D

from sklearn.cluster import KMeans
from sklearn import datasets

np.random.seed(5)

iris = datasets.load_iris()
X = iris.data
y = iris.target

estimators = [(k_means_iris_8, KMeans(n_clusters=8)),
              (k_means_iris_3, KMeans(n_clusters=3)),
              (k_means_iris_bad_init, KMeans(n_clusters=3, n_init=1,
                                               init=random))]

fignum = 1
titles = [8 clusters, 3 clusters, 3 clusters, bad initialization]
# for name, est in estimators:
name = k_means_iris_8
est = KMeans(n_clusters=8)
print(est)
picture = plt.figure(fignum, figsize=(4, 3))
ax = Axes3D(picture, rect=[0, 0, .95, 1], elev=48, azim=134)
est.fit(X)
labels = est.labels_

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

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)
ax.set_title(titles[fignum - 1])
ax.dist = 12


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

for name, label in [(Setosa, 0),
                    (Versicolour, 1),
                    (Virginica, 2)]:
    ax.text3D(X[y == label, 3].mean(),
              X[y == label, 0].mean(),
              X[y == label, 2].mean() + 2, name,
              horizontalalignment=center,
              bbox=dict(alpha=.2, 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, edgecolor=k)

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)
ax.set_title(Ground Truth)
ax.dist = 12

plt.show()
View Code

官网链接:https://scikit-learn.org/stable/auto_examples/cluster/plot_cluster_iris.html#sphx-glr-auto-examples-cluster-plot-cluster-iris-py

参考:https://mp.weixin.qq.com/s?__biz=MzAxNTc0Mjg0Mg==&mid=2653290530&idx=1&sn=7008fc46129106703a05fdfef1ddd4e6&chksm=802dc237b75a4b2173af42e9703c8591e3a4a37e50b2825f5a00cdba1099b49297f2300169e6&mpshare=1&scene=1&srcid=10178eaFgbBY6JZo05vM66s1&sharer_sharetime=1571301323063&sharer_shareid=a49b9557eabaed0d06d2de311dd63b54&pass_ticket=9rhsUGDoFYNdc2RJMbTG%2BMUeXE%2BY%2Bb9d2F3ZAMul8kAKgV7h8aQNp4StajH8jKTj#rd

3D聚类

原文:https://www.cnblogs.com/xxswkl/p/11695422.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!