summaryrefslogtreecommitdiff
path: root/examples/pca1.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pca1.hs')
-rw-r--r--examples/pca1.hs18
1 files changed, 8 insertions, 10 deletions
diff --git a/examples/pca1.hs b/examples/pca1.hs
index a11eba9..ad2214d 100644
--- a/examples/pca1.hs
+++ b/examples/pca1.hs
@@ -8,27 +8,25 @@ import Control.Monad(when)
8type Vec = Vector Double 8type Vec = Vector Double
9type Mat = Matrix Double 9type Mat = Matrix Double
10 10
11 11{-
12-- Vector with the mean value of the columns of a matrix 12-- Vector with the mean value of the columns of a matrix
13mean a = constant (recip . fromIntegral . rows $ a) (rows a) <> a 13mean a = constant (recip . fromIntegral . rows $ a) (rows a) <> a
14 14
15-- covariance matrix of a list of observations stored as rows 15-- covariance matrix of a list of observations stored as rows
16cov x = (trans xc <> xc) / fromIntegral (rows x - 1) 16cov x = (trans xc <> xc) / fromIntegral (rows x - 1)
17 where xc = x - asRow (mean x) 17 where xc = x - asRow (mean x)
18-}
18 19
19 20
20-- creates the compression and decompression functions from the desired number of components 21-- creates the compression and decompression functions from the desired number of components
21pca :: Int -> Mat -> (Vec -> Vec , Vec -> Vec) 22pca :: Int -> Mat -> (Vec -> Vec , Vec -> Vec)
22pca n dataSet = (encode,decode) 23pca n dataSet = (encode,decode)
23 where 24 where
24 encode x = vp <> (x - m) 25 encode x = vp #> (x - m)
25 decode x = x <> vp + m 26 decode x = x <# vp + m
26 m = mean dataSet 27 (m,c) = meanCov dataSet
27 c = cov dataSet 28 (_,v) = eigSH (trustSym c)
28 (_,v) = eigSH' c 29 vp = tr $ takeColumns n v
29 vp = takeRows n (trans v)
30
31norm = pnorm PNorm2
32 30
33main = do 31main = do
34 ok <- doesFileExist ("mnist.txt") 32 ok <- doesFileExist ("mnist.txt")
@@ -43,4 +41,4 @@ main = do
43 let (pe,pd) = pca 10 xs 41 let (pe,pd) = pca 10 xs
44 let y = pe x 42 let y = pe x
45 print y -- compressed version 43 print y -- compressed version
46 print $ norm (x - pd y) / norm x --reconstruction quality 44 print $ norm_2 (x - pd y) / norm_2 x --reconstruction quality