2014-03-26 78 views
0

我一直在嘗試使用Incanter的principal-components函數來做PCA,看起來在使用它的時候出現了偏離軌跡。我從PCA教程發現了一些樣本數據在線,並希望它的做法:主要組件功能Inc器

(def data [[0.69 0.49] [-1.31 -1.21] [0.39 0.99] [0.09 0.29] [1.29 1.09] 
      [0.49 0.79] [0.19 (- 0 0.31)] [(- 0 0.81) (- 0 0.81)] 
      [(- 0 0.31) (- 0 0.31)] [(- 0 0.71) (- 0 1.01)]]) 

在第一次嘗試實現PCA我試圖通過向量咒術矩陣功能,但發現自己傳遞的參數太多。在這一點上,我決定嘗試一個上面定義的嵌套向量結構,但是想避免這條路線。

如何將data轉換成矩陣(瞳孔),以便它將被接受爲Incanter函數principal-components的輸入。爲了簡單起見,我們稱之爲新矩陣fooMatrix。

一旦這個矩陣,fooMatrix,已構建了下面的代碼應該工作以提取前兩個主成分

 (def pca (principal-components fooMatrix)) 
    (def components (:rotation pca)) 
    (def pc1 (sel components :cols 0)) 
    (def pc2 (sel components :cols 1)) 

,然後將數據可以在主成分由

 (def principal1 (mmult fooMatrix pc1)) 
    (def principal2 (mmult fooMatrix pc2)) 
+0

您似乎沒有提出問題。 [此鏈接](http://data-sorcery.org/category/pca/)討論通過Incanter使用PCA,並且在帖子的底部是所有使用的源代碼的鏈接。 – galdre

+0

我不明白的是如何將'data'中包含的向量轉換成矩陣,可以用於Incanter函數'principal-component'。@ galdre – sunspots

+0

我修改了我的初始文章,使得問題是明確的, 謝謝。 – sunspots

回答

1
被投影

查看Incanter API。我相信你只是想要(incanter.core/matrix data)。這些是您選擇Incanter矩陣功能的選項。也許A2是你有興趣

(def A (matrix [[1 2 3] [4 5 6] [7 8 9]])) ; produces a 3x3 matrix 
(def A2 (matrix [1 2 3 4 5 6 7 8 9] 3)) ; produces the same 3x3 matrix 
(def B (matrix [1 2 3 4 5 6 7 8 9])) ; produces a 9x1 column vector 

示例使用您的數據:

user=> (use '[incanter core stats charts datasets]) 
nil 
user=>(def data [0.69 0.49 -1.31 -1.21 0.39 0.99 0.09 0.29 1.29 
       1.09 0.49 0.79 0.19 (- 0 0.31) (- 0 0.81) (- 0 0.81) 
       (- 0 0.31) (- 0 0.31) (- 0 0.71) (- 0 1.01)]) 
user=>(def fooMatrix (matrix data 2)) 
user=>(principal-components fooMatrix) 
{:std-dev (1.3877785387777999 0.27215937850413047), :rotation A 2x2 matrix 
------------- 
-7.07e-01 -7.07e-01 
-7.07e-01 7.07e-01 
} 

瞧。嵌套的矢量結構消失了。

+0

'A2'就是我之後進行矩陣構建的時候 – sunspots