vector_quantization.py 文件源码

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

项目:Python-Machine-Learning-Cookbook 作者: PacktPublishing 项目源码 文件源码
def compress_image(img, num_clusters):
    # Convert input image into (num_samples, num_features) 
    # array to run kmeans clustering algorithm 
    X = img.reshape((-1, 1))  

    # Run kmeans on input data
    kmeans = cluster.KMeans(n_clusters=num_clusters, n_init=4, random_state=5)
    kmeans.fit(X)
    centroids = kmeans.cluster_centers_.squeeze()
    labels = kmeans.labels_

    # Assign each value to the nearest centroid and 
    # reshape it to the original image shape
    input_image_compressed = np.choose(labels, centroids).reshape(img.shape)

    return input_image_compressed
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号