11.12作業
阿新 • • 發佈:2018-11-15
1
from
sklearn.datasets
import
load_sample_image
from
sklearn.cluster
import
KMeans
import
matplotlib.pyplot as plt
china
=
load_sample_image(
"china.jpg"
)
plt.imshow(china)
plt.show()
print
(china.shape)
2
image
=
china[::
3
,::
3
]
#行列分別按step為3的距離取
x
=
image.reshape(
-
1
,
3
)
#生成行數自填充,列數為3的二維陣列
plt.imshow(image)
plt.show()
print
(image.shape,x.shape)
n_color
=
64
model
=
KMeans(n_color)
labels
=
model.fit_predict(x)
#每個點的顏色分類
color
=
model.cluster_centers_
#64個聚類中心,顏色值
color[labels]
images
=
image.reshape(
143
,
214
,
3
)
print
(images.shape)
plt.imshow(images)
plt.show()