Kmeans is a classical clustering algorithm, NEWLISP provides functions, also divided into train and query two stages.
The purpose of the Kmeans algorithm is to divide the training data into K classes and select the K Center points dynamically according to certain algorithms. Here is an example where I added a Chinese comment:
(Set ' Data ' ((6.57 4.96 11.91 0.9) (2.29 4.18 1.06 0.8) (8.63 2.51 8.11 0.7) (1.85 1.89 0.11 0.6) (7.56 7.93 5.06 0.6) (3.) 61 7.95 5.11 0.8) (7.18 3.46 8.7 0.3) (8.17 6.59 7.49 0.2) (5.44 5.9 5.57 0.4) (2.43 2.14 1.59 0.6) (2.48 2.26 0.19 0.8) ( 8.16 3.83 8.93 0.9) (8.49 5.31 7.47 0.86) (3.12 3.1 1.4 0.72) (6.77 6.04 3.76 0.12) (7.01 4.2 11.9 0.3) (6.79 8.72 8.62 0.) 7) (1.17 4.46 1.02 0.55) (2.11 2.14 0.85 0.72) (9.44 2.65 7.37 0.63))) (println "Data length:" (length data)) (Kmeans-train Data 2 ' K);; Divided into two categories, the training results are stored in context K (println (symbols ' k)); Showing symbols in K (println k:labels);; The label that represents each data belongs to that class (println "K:labels Length:" (length K:labels)) (Set ' Labeled-data (Transpose (push k:labels (transpose D ATA)-1)); Add the label of the class to the list (println Labeled-data) (exit)
Similarly, it should be possible to support incremental training, as long as you save the context K, you can continue to train, because the Kmeans-train function prototype last parameter is to receive the existing results
./test.lspdata length:20 (k:centroids k:clusters k:deviations k:labels) (2 1 2 1 2 2 2 2 2 1 1 2 2 1 2 2 2 1 1 2) k:labels l Ength:20[email protected]:~/work/gitlab.com/sdf1/ews/code$./test.lspdata length:20 (K:centroids K:clusters K: Deviations K:labels) (2 1 2 1 2 2 2 2 2 1 1 2 2 1 2 2 2 1 1 2) k:labels length:20 ((6.57 4.96 11.91 0.9 2) (2.29 4.18 1.06 0 .8 1) (8.630000000000001 2.51 8.109999999999999 0.7 2) (1.85 1.89 0.11 0.6 1) (7.56 7.93 5.06 0.6 2) (3.61 7.95 5.11 0.8 2) (7.18 3.46 8.699999999999999 0.3 2) (8.17 6.59 7.49 0.2 2) (5.44 5.9 5.57 0.4 2) (2.43 2 .1.59 0.6 1) (2.48 2.26 0.19 0.8 1) (8.16 3.83 8.93 0.9 2) (8.49 5.31 7.47 0.86 2) (3.12 3.1 1.4 0.72 1) (6.77 6.04 3.76 0.12 2) (7.01 4.2 11.9 0.3 2) ( 6.79 8.720000000000001 8.619999999999999 0.7 2) (1. 4.46 1.02 0.55 1) (2.11 2.14 0.85 0.72 1) (9.44 2.65 7.37 0.63 2))
newLISP Kmeans algorithm