-
Notifications
You must be signed in to change notification settings - Fork 2
/
k_means.ml
35 lines (32 loc) · 1017 Bytes
/
k_means.ml
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
open Common;;
let k_means data len k =
(* Initial Centers *)
let cen = choice data k in
let cof = normalize_coeffs data in
let dist_log = dist_log_gen cof in
for trials = 0 to 10 do
(* Assign to Clusters *)
Array.iter (fun elm ->
let m = ref 0 in
for i = 0 to k-1 do
if dist_log elm cen.(i).cl_cen <
dist_log elm cen.(!m).cl_cen
then m:=i
done;
let m_cen = Array.get cen !m in
m_cen.cl_elm <- elm :: m_cen.cl_elm) data;
(* Find new centers *)
Array.iter (fun center ->
let candidate_centers = List.map (fun elm ->
let m = List.fold_left (fun x y -> x +. dist_log elm y) 0.0 center.cl_elm in elm,m
) center.cl_elm in
let new_cen,_ = foldl1
(fun x y -> match x,y with
| (xn,xv),(yn,yv) -> if xv>yv then x else y) candidate_centers in
center.cl_cen <- new_cen;
center.cl_elm <- []) cen;
done; cen;;
let loader data len param =
let k = int_of_string param in
let res, time = time_it (fun _ -> k_means data len k) in
res,k,time;;