kosh

kmeans-sample.lsp

Jul 12th, 2013
156
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)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×