zoukankan      html  css  js  c++  java
  • 3D聚类

    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

  • 相关阅读:
    (数据科学学习手札21)sklearn.datasets常用功能详解
    (数据科学学习手札20)主成分分析原理推导&Python自编函数实现
    (数据科学学习手札19)R中基本统计分析技巧总结
    (数据科学学习手札18)二次判别分析的原理简介&Python与R实现
    P2633|主席树+dfs序+树链剖分求lca+离散化
    主席树|求区间第k小模板
    树上问题
    数据结构|序列问题与树上问题小结
    珂朵莉树 例题小结
    CF#609E|二分+树状数组
  • 原文地址:https://www.cnblogs.com/xxswkl/p/11695422.html
Copyright © 2011-2022 走看看