import matplotlib.pyplot as plt from sklearn.datasets import load_sample_image import numpy as np from sklearn.cluster import KMeans import matplotlib.image as img china = img.imread('C:\UsersAdministratorDesktopchina.jpg') plt.imshow(china) plt.show() china.shape china plt.imshow(china[:,:,0]) #提取第三列的颜色 print(china.shape) plt.show() chinas = china[::3,::3] #降低分辨率 plt.imshow(chinas) plt.show() china1 = chinas.reshape(-1,3) print(china1) print(china.shape,chinas.shape,china1.shape) n_color = 64 #将所有颜色定义为64组 (分组) model = KMeans(n_color) labels = model.fit_predict(china1) colors = model.cluster_centers_ colors[labels] new_images = colors[labels] new_images = new_images.reshape(chinas.shape) new_images = new_images.astype(np.uint8) plt.imshow(chinas) plt.show() plt.imshow(new_images) plt.show()
运行结果:
贝叶斯算法:
理解贝叶斯定理:
- M桶:7红3黄
- N桶:1红9黄
- 现在:拿出了一个红球
- 试问:这个红球是M、N桶拿出来的概率分别是多少?