コード例 #1
0
import scipy as sp
import matplotlib.pyplot as plt

from sklearn import cluster


try:  # SciPy >= 0.16 have face in misc
    from scipy.misc import face
    face = face(gray=True)
except ImportError:
    face = sp.face(gray=True)

n_clusters = 5
np.random.seed(0)

X = face.reshape((-1, 1))  # We need an (n_sample, n_feature) array
k_means = cluster.KMeans(n_clusters=n_clusters, n_init=4)
k_means.fit(X)
values = k_means.cluster_centers_.squeeze()
labels = k_means.labels_

# create an array from labels and values
face_compressed = np.choose(labels, values)
face_compressed.shape = face.shape

vmin = face.min()
vmax = face.max()

# original face
plt.figure(1, figsize=(3, 2.2))
plt.imshow(face, cmap=plt.cm.gray, vmin=vmin, vmax=256)
コード例 #2
0
import scipy as sp
import matplotlib.pyplot as plt

from sklearn import cluster

try:  # SciPy >= 0.16 have face in misc
    from scipy.misc import face

    face = face(gray=True)
except ImportError:
    face = sp.face(gray=True)

n_clusters = 5
np.random.seed(0)

X = face.reshape((-1, 1))  # We need an (n_sample, n_feature) array
k_means = cluster.KMeans(n_clusters=n_clusters, n_init=4)
k_means.fit(X)
values = k_means.cluster_centers_.squeeze()
labels = k_means.labels_

# create an array from labels and values
face_compressed = np.choose(labels, values)
face_compressed.shape = face.shape

vmin = face.min()
vmax = face.max()

# original face
plt.figure(1, figsize=(3, 2.2))
plt.imshow(face, cmap=plt.cm.gray, vmin=vmin, vmax=256)
コード例 #3
0
ファイル: 20210702.py プロジェクト: Qqww4599/gittt
assert a == 1
print(1)
assert a == 0
print(0)

# 實作k-means
try:  # SciPy >= 0.16 have face in misc
    from scipy.misc import face
    face = face(gray=True)
except ImportError:
    face = sp.face(gray=True)  # face是一張圖片,大小為768*1024

n_clusters = 5
np.random.seed(0)

X = face.reshape((-1, 1))  # 拉成一維向量 (n_sample, n_feature) array
k_means = cluster.KMeans(n_clusters=n_clusters,
                         n_init=4)  # K_means分類,num_cluster=5
k_means.fit(X)
values = k_means.cluster_centers_.squeeze(
)  # 使用不同 centroid seeds 運行 k-means 算法的時間
labels = k_means.labels_

face_compressed = np.choose(labels,
                            values)  #labels範圍0~4,長度768*1024,values代表五個k_keans值
face_compressed.shape = face.shape

v_min = face.min()
v_max = face.max()

plt.figure(1, figsize=(3, 2.2))