import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans, DBSCAN
from sklearn.datasets import load_sample_image
# 生成多个分类图:2、4、16、64
# 1、导入图片数据
china = load_sample_image('china.jpg')
# 2、数据预处理改变维度保留颜色通道变为二位数据
china = china.reshape(-1,3)
display(china.shape) # (273280, 3)
# 3、随机选取10000个点作为训练数据
data_train = china.copy()
np.random.shuffle(data_train)
data_train = data_train[:10000]
display(data_train.shape) # (10000, 3)
# 4、训练绘制子图
plt.figure(figsize=(2*3,2*3))
for i,k in enumerate([2,4,16,64]):
ax = plt.subplot(2,2,i+1)
# 训练
km = KMeans(k,n_init='auto')
km.fit(data_train)
# 获取所有簇中心的RGB值
centers = km.cluster_centers_
# 通过预测获取原图每个位置的分类标签
label = km.predict(china)
# 生成预测图
final_img = centers[label]
# 将预测图所有位置RGB值转化为0~256范围
final_img = final_img/256
ax.imshow(final_img.reshape(427,640,3))
ax.set_title(f'K = {k}',fontsize=15)
plt.imsave(f'./tupian{i}.png',final_img.reshape(427,640,3))