机器学习四

1. 应用K-means算法进行图片压缩

from sklearn.datasets import load_sample_image
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import matplotlib.image as img
import numpy as np
import sys

china = load_sample_image("china.jpg")

X=china.reshape(-1,3)
model=KMeans(64)
labels = model.fit_predict(X)
colors = model.cluster_centers_
new_img = colors[labels].reshape(china.shape)
plt.imshow(china)
plt.show()
plt.imshow(new_img.astype(np.uint8))
plt.show()
memory_size = sys.getsizeof(china)
print("原占用内存:",memory_size)
memory_size2 = sys.getsizeof(new_img)
print("压缩后占用内存:",memory_size2)
img.imsave("C:\\Users\\15108\\Desktop\\temp\\china.jpg",china)
img.imsave("C:\\Users\\15108\\Desktop\\temp\\new_img.jpg",new_img)

结果:

原图片:

 

 

 

压缩后图片:

 

 2. 观察学习与生活中可以用K均值解决的问题。

# 天气情况类似适合旅游的
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
df = pd.DataFrame({
    'city' : pd.Categorical(["广州","番禺","从化","增城","花都","韶关","乳源","始兴","翁源","乐昌"]),#城市名
    'high' : np.array([23,25,25,22,25,23,25,23,22,22],dtype='int32'),#最高温度
    'low' : np.array([16,18,12,15,15,15,14,13,14,14],dtype='int32'),#最低温度
    'fl' : np.array([3,3,2,1,2,1,3,2,1,3],dtype='int32'),#风力
})

# 取数据集
x2=df.iloc[:,1:]
# 模型的构建和训练
k2=KMeans(n_clusters=2)
k2.fit(x2)
kc2=k2.cluster_centers_
y_kmeans2=k2.predict(x2)
print(y_kmeans2)
# 增加一列类别
df['same']  = y_kmeans2
# 天气情况类似0
print(df[df.same==0])
# 天气情况类似1
print(df[df.same==1])

 

posted on 2020-04-16 20:17  不忘川  阅读(219)  评论(0编辑  收藏  举报

导航