Python 打印每个簇的图像

Python 打印每个簇的图像,python,machine-learning,image-processing,computer-vision,Python,Machine Learning,Image Processing,Computer Vision,我使用sklearn KMeans来形成图像簇,我在打印每个簇的图像时遇到了困难 我有一个维数为:(10000,100,100,3)的np数组序列 然后我将图像展平,使每一行显示一个图像。列车尺寸:(10000、30000) 我应用了KMeans from scipy import ndimage from sklearn.cluster import KMeans kmeans = KMeans(n_clusters=10, random_state=0) clusters = kmea

我使用sklearn KMeans来形成图像簇,我在打印每个簇的图像时遇到了困难

  • 我有一个维数为:(10000,100,100,3)的np数组序列
  • 然后我将图像展平,使每一行显示一个图像。列车尺寸:(10000、30000)
  • 我应用了KMeans

    from scipy import ndimage
    
    from sklearn.cluster import KMeans
    
    kmeans = KMeans(n_clusters=10, random_state=0)
    
    clusters = kmeans.fit_predict(train)
    
    centers = kmeans.cluster_centers_
    

  • 在此之后,我想打印每个集群的图像,

    对于十个集群,您将得到十个集群中心。你现在可以把它们打印出来,也可以把它们形象化——我想你会这么做的

    import numpy as np
    import matplotlib.pyplot as plt
    
    #fake centers 
    centers = np.random.random((10,100,100,3))
    
    #print centers
    for ci in centers:
        print(ci)
    
    #visualize centers:
    for ci in centers: 
        plt.imshow(ci)
        plt.show()
    
    编辑:我知道您不仅希望可视化中心,还希望可视化每个集群中的其他成员

    您可以对单个随机成员执行以下操作:

    from scipy import ndimage
    from sklearn.cluster import KMeans
    import numpy as np
    import matplotlib.pyplot as plt
    import random
    
    #PARAMS
    n_clusters=10  
    
    #fake train data
    original_train = np.random.random((100, 100, 100, 3)) #100 images of each 100 px,py and RGB 
    
    n,x,y,c = original_train.shape
    
    flat_train = original_train.reshape((n,x*y*c))
    
    kmeans = KMeans(n_clusters, random_state=0)
    
    clusters = kmeans.fit_predict(flat_train)
    
    centers = kmeans.cluster_centers_
    
    #visualize centers:
    for ci in centers: 
        plt.imshow(ci.reshape(x,y,c))
        plt.show()
    
    #visualize other members
    for cluster in np.arange(n_clusters):
    
        cluster_member_indices = np.where(clusters == cluster)[0]
        print("There are %s members in cluster %s" % (len(cluster_member_indices), cluster))
    
        #pick a random member
        random_member = random.choice(cluster_member_indices)
        plt.imshow(original_train[random_member,:,:,:])
        plt.show()
    

    对于十个集群,您将获得十个集群中心。你现在可以把它们打印出来,也可以把它们形象化——我想你会这么做的

    import numpy as np
    import matplotlib.pyplot as plt
    
    #fake centers 
    centers = np.random.random((10,100,100,3))
    
    #print centers
    for ci in centers:
        print(ci)
    
    #visualize centers:
    for ci in centers: 
        plt.imshow(ci)
        plt.show()
    
    编辑:我知道您不仅希望可视化中心,还希望可视化每个集群中的其他成员

    您可以对单个随机成员执行以下操作:

    from scipy import ndimage
    from sklearn.cluster import KMeans
    import numpy as np
    import matplotlib.pyplot as plt
    import random
    
    #PARAMS
    n_clusters=10  
    
    #fake train data
    original_train = np.random.random((100, 100, 100, 3)) #100 images of each 100 px,py and RGB 
    
    n,x,y,c = original_train.shape
    
    flat_train = original_train.reshape((n,x*y*c))
    
    kmeans = KMeans(n_clusters, random_state=0)
    
    clusters = kmeans.fit_predict(flat_train)
    
    centers = kmeans.cluster_centers_
    
    #visualize centers:
    for ci in centers: 
        plt.imshow(ci.reshape(x,y,c))
        plt.show()
    
    #visualize other members
    for cluster in np.arange(n_clusters):
    
        cluster_member_indices = np.where(clusters == cluster)[0]
        print("There are %s members in cluster %s" % (len(cluster_member_indices), cluster))
    
        #pick a random member
        random_member = random.choice(cluster_member_indices)
        plt.imshow(original_train[random_member,:,:,:])
        plt.show()
    

    你试过打印(中心)吗?参考this@NikolasRieble中心形状是(10,100,100,3),所以我只得到10张图片。你试过打印(中心)吗?参考this@NikolasRieble中心的形状是(10,100,100,3),所以我只得到了10张图像,但它只显示了10张图像,如何显示每个中心的一些图像。似乎对集群和中心的含义有一些误解。您使用KMeans将数据分为10个集群。然后,您需要可视化/打印集群的中心。每个集群都有一个中心。然后你会有10个集群中心。你的论点完全正确,但OP可能会问如何打印每个集群的一些图像(即样本),而不是集群中心;无论如何,我不确定在这种情况下打印集群中心有多重要/有趣。我相应地编辑了答案,现在它绘制了一个随机成员。他几乎没有调整,而是绘制了所有成员,绘制了N个成员,等等。但它只显示了10幅图像,如何显示每个中心的一些图像。似乎对集群和中心的含义有一些误解。您使用KMeans将数据分为10个集群。然后,您需要可视化/打印集群的中心。每个集群都有一个中心。然后你会有10个集群中心。你的论点完全正确,但OP可能会问如何打印每个集群的一些图像(即样本),而不是集群中心;无论如何,我不确定在这种情况下打印集群中心有多重要/有趣。我相应地编辑了答案,现在它绘制了一个随机成员。只需稍作调整,他就绘制了所有成员、N个成员等。