/
kmeans.jl
312 lines (247 loc) · 9.12 KB
/
kmeans.jl
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
struct KmeansResult{T<:AbstractFloat}
K::Int
centroids::Vector{Vector{T}}
cluster::Vector{Int}
withinss::T
iter::Int
end
Object resulting from kmeans algorithm that contains the number of clusters, centroids, clusters prediction,
total-variance-within-cluster and number of iterations until convergence.
"""
struct KmeansResult{T<:AbstractFloat}
K::Int
centroids::Vector{Vector{T}}
cluster::Vector{Int}
withinss::T
iter::Int
end
function Base.print(io::IO, model::KmeansResult{T}) where {T<:AbstractFloat}
p = [" $(v)\n" for v in model.centroids]
print(IOContext(io, :limit => true), "KmeansResult{$T}:
K = $(model.K)
centroids = [\n", p..., " ]
cluster = ", model.cluster, "
within-cluster sum of squares = $(model.withinss)
iterations = $(model.iter)")
end
Base.show(io::IO, model::KmeansResult) = print(io, model)
"""
ClusterAnalysis.euclidean(a::AbstractVector, b::AbstractVector)
Calculate euclidean distance from two vectors. √∑(aᵢ - bᵢ)².
# Arguments (positional)
- `a`: First vector.
- `b`: Second vector.
# Example
```julia
julia> using ClusterAnalysis
julia> a = rand(100); b = rand(100);
julia> ClusterAnalysis.euclidean(a, b)
3.8625780213774954
```
"""
function euclidean(a::AbstractVector{T},
b::AbstractVector{T}) where {T<:AbstractFloat}
@assert length(a) == length(b)
# euclidean(a, b) = √∑(aᵢ- bᵢ)²
s = zero(T)
@simd for i in eachindex(a)
@inbounds s += (a[i] - b[i])^2
end
return √s
end
"""
ClusterAnalysis.squared_error(data::AbstractMatrix)
ClusterAnalysis.squared_error(col::AbstractVector)
Function that evaluate the kmeans, using the Sum of Squared Error (SSE).
# Arguments (positional)
- `data` or `col`: Matrix of data observations or a Vector which represents one column of data.
# Example
```julia
julia> using ClusterAnalysis
julia> a = rand(100, 4);
julia> ClusterAnalysis.squared_error(a)
34.71086095943974
julia> ClusterAnalysis.squared_error(a[:, 1])
10.06029322934825
```
"""
function squared_error(data::AbstractMatrix{T}) where {T<:AbstractFloat}
error = zero(T)
@simd for i in axes(data, 2)
error += squared_error(view(data, :, i))
end
return error
end
function squared_error(col::AbstractVector{T}) where {T<:AbstractFloat}
μ = mean(col)
error = zero(T)
@simd for i in eachindex(col)
@inbounds error += (col[i] - μ)^2
end
return error
end
"""
ClusterAnalysis.totalwithinss(data::AbstractMatrix, K::Int, cluster::Vector)
Calculate the total-variance-within-cluster using the `squared_error()` function.
# Arguments (positional)
- `data`: Matrix of data observations.
- `K`: number of clusters.
- `cluster`: Vector of cluster for each data observation.
# Example
```julia
julia> using ClusterAnalysis
julia> using CSV, DataFrames
julia> iris = CSV.read(joinpath(pwd(), "path/to/iris.csv"), DataFrame);
julia> df = iris[:, 1:end-1];
julia> model = kmeans(df, 3);
julia> ClusterAnalysis.totalwithinss(Matrix(df), model.K, model.cluster)
78.85144142614601
```
"""
function totalwithinss(data::AbstractMatrix{T}, K::Int, cluster::AbstractVector{Int}) where {T<:AbstractFloat}
# evaluate total-variance-within-clusters
error = zero(T)
@simd for k in 1:K
error += squared_error(data[cluster .== k, :])
end
return error
end
"""
kmeans(table, K::Int; nstart::Int = 1, maxiter::Int = 10, init::Symbol = :kmpp)
kmeans(data::AbstractMatrix, K::Int; nstart::Int = 1, maxiter::Int = 10, init::Symbol = :kmpp)
Classify all data observations in k clusters by minimizing the total-variance-within each cluster.
# Arguments (positional)
- `table` or `data`: table or Matrix of data observations.
- `K`: number of clusters.
## Keyword
- `nstart`: number of starts.
- `maxiter`: number of maximum iterations.
- `init`: centroids inicialization algorithm - `:kmpp` (default) or `:random`.
# Example
```julia
julia> using ClusterAnalysis
julia> using CSV, DataFrames
julia> iris = CSV.read(joinpath(pwd(), "path/to/iris.csv"), DataFrame);
julia> df = iris[:, 1:end-1];
julia> model = kmeans(df, 3)
KmeansResult{Float64}:
K = 3
centroids = [
[5.932307692307693, 2.755384615384615, 4.42923076923077, 1.4384615384615382]
[5.006, 3.4279999999999995, 1.462, 0.24599999999999997]
[6.874285714285714, 3.088571428571429, 5.791428571428571, 2.117142857142857]
]
cluster = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2 … 3, 3, 1, 3, 3, 3, 1, 3, 3, 1]
within-cluster sum of squares = 78.85144142614601
iterations = 7
```
# Pseudo-code of the algorithm:
* Repeat `nstart` times:
1. Initialize `K` clusters centroids using KMeans++ algorithm or random init.
2. Estimate clusters.
3. Repeat `maxiter` times:
* Update centroids using the mean().
* Reestimates the clusters.
* Calculate the total-variance-within-cluster.
* Evaluate the stop rule.
* Keep the best result (minimum total-variance-within-cluster) of all `nstart` executions.
For more detailed explanation of the algorithm, check the
[`Algorithm's Overview of KMeans`](https://github.com/AugustoCL/ClusterAnalysis.jl/blob/main/algo_overview/kmeans_overview.md).
"""
function kmeans(table, K::Int; kwargs...)
Tables.istable(table) ? (data = Tables.matrix(table)) : throw(ArgumentError("The table argument passed does not implement the Tables.jl interface."))
return kmeans(data, K; kwargs...)
end
kmeans(data::AbstractMatrix{T}, K::Int; kwargs...) where {T} = kmeans(convert(Matrix{Float64}, data), K; kwargs...)
function kmeans(data::AbstractMatrix{T}, K::Int;
nstart::Int = 1,
maxiter::Int = 10,
init::Symbol = :kmpp) where {T<:AbstractFloat}
nl = size(data, 1)
centroids = Vector{Vector{T}}(undef, K)
cluster = Vector{Int}(undef, nl)
withinss = Inf
iter = 0
# run multiple kmeans to get the best result
for _ in 1:nstart
new_centroids, new_cluster, new_withinss, new_iter = _kmeans(data, K, maxiter, init)
if new_withinss < withinss
centroids .= new_centroids
cluster .= new_cluster
withinss = new_withinss
iter = new_iter
end
end
return KmeansResult(K, centroids, cluster, withinss, iter)
end
function _kmeans(data::AbstractMatrix{T}, K::Int, maxiter::Int, init::Symbol) where {T<:AbstractFloat}
nl = size(data, 1)
# generate random centroids
centroids = _initialize_centroids(data, K, init)
# first clusters estimate
cluster = Vector{Int}(undef, nl)
for (i, obs) in enumerate(eachrow(data))
dist = [euclidean(obs, c) for c in centroids]
@inbounds cluster[i] = argmin(dist)
end
# first evaluation of total-variance-within-cluster
withinss = totalwithinss(data, K, cluster)
# variables to update during the iterations
new_centroids = copy(centroids)
new_cluster = copy(cluster)
iter = 1
norms = norm.(centroids)
# start kmeans iterations until maxiter or convergence
for _ in 2:maxiter
# update new_centroids using the mean
@simd for k in 1:K # mean.(eachcol(data[new_cluster .== k, :]))
@inbounds new_centroids[k] = vec(mean(view(data, new_cluster .== k, :), dims = 1))
end
# estimate cluster to all observations
for (i, obs) in enumerate(eachrow(data))
dist = [euclidean(obs, c) for c in new_centroids]
@inbounds new_cluster[i] = argmin(dist)
end
# update iter, withinss-variance and calculate centroid norms
new_withinss = totalwithinss(data, K, new_cluster)
new_norms = norm.(new_centroids)
iter += 1
# convergence rule
norm(norms - new_norms) ≈ 0 && break
# update centroid norms
norms .= new_norms
# update centroids, cluster and whithinss
if new_withinss < withinss
centroids .= new_centroids
cluster .= new_cluster
withinss = new_withinss
end
end
return centroids, cluster, withinss, iter
end
function _initialize_centroids(data::AbstractMatrix{T}, K::Int, init::Symbol) where {T<:AbstractFloat}
nl = size(data, 1)
if init == :random
indexes = rand(1:nl, K)
centroids = Vector{T}[data[i, :] for i in indexes]
elseif init == :kmpp
centroids = Vector{Vector{T}}(undef, K)
centroids[1] = data[rand(1:nl), :]
# distance vector for each observation
dists = Vector{T}(undef, nl)
# get each new centroid by the furthest observation (maximum distance)
for k in 2:K
# for each observation get the nearest centroid by the minimum distance
for (i, row) in enumerate(eachrow(data))
dist_c = [euclidean(row, c) for c in @view centroids[1:(k-1)]]
@inbounds dists[i] = minimum(dist_c)
end
# new centroid by the furthest observation
@inbounds centroids[k] = data[argmax(dists), :]
end
else
throw(ArgumentError("The symbol :$init is not a valid argument. Use :random or :kmpp."))
end
return centroids
end