12345678910111213141516171819202122232425262728293031323334353637# 1 "src/owl/misc/owl_cluster.ml"(*
* OWL - OCaml Scientific and Engineering Computing
* Copyright (c) 2016-2019 Liang Wang <liang.wang@cl.cam.ac.uk>
*)moduleMX=Owl_dense.Matrix.DmoduleUT=Owl_utils(** K-means clustering algorithm
x is the row-based data points and c is the number of clusters.
*)letkmeansxc=letopenMXinletcpts0=fst(draw_rowsxc)inletcpts1=zerosc(col_numx)inletassignment=Array.make(row_numx)(0,max_float)inlet_=tryforcounter=1to100doOwl_log.info"iteration %i ..."counter;flushstdout;iteri_rows(funiv->iteri_rows(funju->lete=sum'(pow_scalar(subvu)2.)inifStdlib.(e<sndassignment.(i))thenassignment.(i)<-(j,e))cpts0)x;iteri_rows(funj_u->letl=UT.Array.filteri_v(funiy->Stdlib.(fsty=j),i)assignmentinletz=mean_rows(rowsxl)inlet_=copy_row_tozcpts1jin())cpts0;ifequalcpts0cpts1thenfailwith"converged"elseignore(copy_~out:cpts0cpts1)donewith_exn->()incpts1,UT.Array.mapfstassignment