标签:http kmeans clu png dataset nump size plt 分享
##加载数据
from sklearn.datasets import load_sample_image
china = load_sample_image("china.jpg")
print(china.shape)
china
import matplotlib.pyplot as plt
plt.imshow(china)
plt.show()
plt.imshow(china[:,:,0])
plt.show()
plt.imshow(china[:,:,0],plt.cm.gray)
plt.show()
from sklearn .datasets import load_sample_image
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np
china = load_sample_image("china.jpg")
plt.imshow(china)
plt.show()
image = china[::3,::3]
image.shape
plt.imshow(image)
plt.show()
x=image.reshape(-1,3)
model =KMeans(n_clusters=64)
b = model.fit_predict(x)
a = model.cluster_centers_
a[b]
##压缩图片
import sys
sys.getsizeof(china)
sys.getsizeof(new_image)
import matplotlib.image as img
img.imsave("F://02.jpg",china)
#img.imsave("F://03.jpg",new_image)
结果:
标签:http kmeans clu png dataset nump size plt 分享
原文地址:https://www.cnblogs.com/huang201606050002/p/9945631.html