-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans_clustering.jl
71 lines (43 loc) · 1.03 KB
/
kmeans_clustering.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
using Clustering, Plots
using Random
# initialize plot
gr(size = (400, 400))
# generate random points
Random.seed!(1)
f1 = rand(100)
f2 = rand(100)
p_rand = scatter(f1, f2)
X = [f1 f2]'
k = 2
itr = 100
Random.seed!(1)
result = kmeans(X, k; maxiter = itr, display = :iter)
a = assignments(result)
c = counts(a)
mu = result.centers
p_kmeans_demo = scatter(f1, f2, group = a)
scatter!(mu[1, :], mu[2, :])
## Application ##
using RDatasets
cats = dataset("boot", "catsM")
vscodedisplay(cats)
p_cats = scatter(cats.BWt, cats.HWt)
f1 = cats.BWt
f2 = cats.HWt
f1_min = minimum(f1)
f2_min = minimum(f2)
f1_max = maximum(f1)
f2_max = maximum(f2)
f1_n = (f1 .- f1_min) ./ (f1_max - f1_min)
f2_n = (f2 .- f2_min) ./ (f2_max - f2_min)
X = [f1_n f2_n]'
p_cats = scatter(f1_n, f2_n)
k = 3
itr = 200000
Random.seed!(1)
result = kmeans(X, k; maxiter = itr, display = :iter)
a = assignments(result)
c = counts(a)
mu = result.centers
p_cats_n = scatter(f1_n, f2_n, group = a)
scatter!(mu[1, :], mu[2, :], color = :yellow, markersize = 10)