Advertisement
kosh

kmeans-sample.lsp

Jul 12th, 2013
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env newlisp
  2.  
  3. ;; $ newlisp kmeans-sample.lsp > output.txt
  4. ;; gnuplot> plot "output.txt" index 0, "" index 1, ...
  5.  
  6. (define data-file
  7.   ;"http://research.microsoft.com/en-us/um/people/cmbishop/prml/webdatasets/faithful.txt"
  8.   "http://www.cs.cmu.edu/~dpelleg/kmeans/11class.unit.ds"
  9.   )
  10.  
  11. (define (parse-file path)
  12.   (let ((acc '()))
  13.     (dolist (line (parse (read-file path) "\n"))
  14.       (let ((xy (map float (0 2 (parse line)))))
  15.         (when (and xy
  16.                    (float? (xy 0))
  17.                    (float? (xy 1)))
  18.           (push xy acc -1))))
  19.     acc))
  20.  
  21. (setq data (parse-file data-file))
  22.  
  23. (kmeans-train data 10 'K)
  24. ;(save "output.lsp" 'K 'data)
  25.  
  26. (dotimes (i (length K:clusters))
  27.   (unless (empty? (K:clusters i))
  28.     (println "# clusters[" i "]")
  29.     (dolist (xy (select data (K:clusters i)))
  30.       (println (format "%1.10f %1.10f" (xy 0) (xy 1))))
  31.     (println)
  32.     (println)))
  33.  
  34. (exit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement