forked from ASPP/ASPP-2018-numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.py
123 lines (93 loc) · 3.92 KB
/
kmeans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -----------------------------------------------------------------------------
# Copyright (C) 2018 Nicolas P. Rougier
# Distributed under the terms of the BSD License.
# -----------------------------------------------------------------------------
# Code by Gareth Rees, posted on stack overflow
# https://codereview.stackexchange.com/questions/61598/k-mean-with-numpy
import numpy as np
import scipy.spatial
def cluster_centroids(data, clusters, k=None):
"""Return centroids of clusters in data.
data is an array of observations with shape (A, B, ...).
clusters is an array of integers of shape (A,) giving the index
(from 0 to k-1) of the cluster to which each observation belongs.
The clusters must all be non-empty.
k is the number of clusters. If omitted, it is deduced from the
values in the clusters array.
The result is an array of shape (k, B, ...) containing the
centroid of each cluster.
>>> data = np.array([[12, 10, 87],
... [ 2, 12, 33],
... [68, 31, 32],
... [88, 13, 66],
... [79, 40, 89],
... [ 1, 77, 12]])
>>> cluster_centroids(data, np.array([1, 1, 2, 2, 0, 1]))
array([[ 79., 40., 89.],
[ 5., 33., 44.],
[ 78., 22., 49.]])
"""
if k is None:
k = np.max(clusters) + 1
result = np.empty(shape=(k,) + data.shape[1:])
for i in range(k):
np.mean(data[clusters == i], axis=0, out=result[i])
return result
def kmeans(data, k=None, centroids=None, steps=20):
"""Divide the observations in data into clusters using the k-means
algorithm, and return an array of integers assigning each data
point to one of the clusters.
centroids, if supplied, must be an array giving the initial
position of the centroids of each cluster.
If centroids is omitted, the number k gives the number of clusters
and the initial positions of the centroids are selected randomly
from the data.
The k-means algorithm adjusts the centroids iteratively for the
given number of steps, or until no further progress can be made.
>>> data = np.array([[12, 10, 87],
... [ 2, 12, 33],
... [68, 31, 32],
... [88, 13, 66],
... [79, 40, 89],
... [ 1, 77, 12]])
>>> np.random.seed(73)
>>> kmeans(data, k=3)
(array([[79., 40., 89.],
[ 5., 33., 44.],
[78., 22., 49.]]), array([1, 1, 2, 2, 0, 1]))
"""
if centroids is not None and k is not None:
assert(k == len(centroids))
elif centroids is not None:
k = len(centroids)
elif k is not None:
# Forgy initialization method: choose k data points randomly.
centroids = data[np.random.choice(np.arange(len(data)), k, False)]
else:
raise RuntimeError("Need a value for k or centroids.")
for _ in range(max(steps, 1)):
# Squared distances between each point and each centroid.
sqdists = scipy.spatial.distance.cdist(centroids, data, 'sqeuclidean')
# Index of the closest centroid to each data point.
clusters = np.argmin(sqdists, axis=0)
new_centroids = cluster_centroids(data, clusters, k)
if np.array_equal(new_centroids, centroids):
break
centroids = new_centroids
return centroids, clusters
if __name__ == '__main__':
import imageio
# Number of final colors we want
n = 16
# Original Image
I = imageio.imread("kitten.jpg")
shape = I.shape
# Flattened image
D = I.reshape(shape[0]*shape[1], shape[2])
# Search for 16 centroids in D (using 20 iterations)
centroids, clusters = kmeans(D, k=n, steps=20)
# Create quantized image
I = (centroids[clusters]).reshape(shape)
I = np.round(I).astype(np.uint8)
# Save result
imageio.imsave("kitten-quantized.jpg", I)