cluster.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:big-data 作者: michaelgbw 项目源码 文件源码
def recolor_image(input_file, k=5):

    img = mpimg.imread(path_to_png_file)
    pixels = [pixel for row in img for pixel in row]
    clusterer = KMeans(k)
    clusterer.train(pixels) # this might take a while    

    def recolor(pixel):
        cluster = clusterer.classify(pixel) # index of the closest cluster
        return clusterer.means[cluster]     # mean of the closest cluster

    new_img = [[recolor(pixel) for pixel in row]
               for row in img]

    plt.imshow(new_img)
    plt.axis('off')
    plt.show()

#
# hierarchical clustering
#
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号