Skip to content

Instantly share code, notes, and snippets.

@liebke
Created May 19, 2009 01:09
Show Gist options
  • Select an option

  • Save liebke/113850 to your computer and use it in GitHub Desktop.

Select an option

Save liebke/113850 to your computer and use it in GitHub Desktop.
(use '(incanter core stats charts datasets))
;; load the iris dataset
(def iris (to-matrix (get-dataset :iris)))
;; run the pca on the first four columns only
(def pca (principal-components (sel iris :cols (range 4))))
;; extract the first two principal components
(def pc1 (sel (:rotation pca) :cols 0))
(def pc2 (sel (:rotation pca) :cols 1))
;; project the first four dimension of the iris data onto the first
;; two principal components
(def x1 (mmult (sel iris :cols (range 4)) pc1))
(def x2 (mmult (sel iris :cols (range 4)) pc2))
;; now plot the transformed data, coloring each species a different color
(doto (scatter-plot (sel x1 :rows (range 50)) (sel x2 :rows (range 50))
:x-label "PC1" :y-label "PC2" :title "Iris PCA")
(add-points (sel x1 :rows (range 50 100)) (sel x2 :rows (range 50 100)))
(add-points (sel x1 :rows (range 100 150)) (sel x2 :rows (range 100 150)))
view)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment