Vous êtes sur la page 1sur 5

C:\Users\Usuario\Desktop\pca\trab-CC-muriloscalser-victormoura.

hs sexta-feira, 1 de julho de 2016 08:37

{-
============================= TRABALHO: CALCULO DO PCA =============================

NOMES: MURILO BORGHARDT SCALSER; VICTOR DE OLIVEIRA MOURA

DATA: 27/06/2016
SEMESTRE: 2016/1

-}
import Numeric.LinearAlgebra
import Data.List

-- Funcoes gerais
matest :: [[Double]]
matest=[[1,2,3],[1,2,3],[1,2,3]]

matriz2 :: [[Double]]
matriz2 = [[1,2,3,4,5],[2,3,4,5,6]]

matriz3 :: [[Double]]
matriz3 = [[1,3,4,5,6],[5,6,7,8,9],[3,4,5,6,7]]

matriz :: [[Double]]
matriz = [[1,2,3,4],[1,2,3,4],[1,2,3,4]]

col :: [Double]
col = [1,5,9]

tamanhoL :: [[Double]] -> Int


tamanhoL mn = length mn

tamanhoC :: [[Double]] -> Int


tamanhoC mn = length (mn!!0)

intsqrt :: Int -> Int


intsqrt = floor . sqrt . fromIntegral

-- Passo 1 calcular médias

mediasColunas :: [[Double]] -> [Double]


mediasColunas mn = [ mediaC (coluna mn y) mn | y <- [0..tamanhoC mn -1]]

mediaC :: [Double] -> [[Double]] -> Double


mediaC x mn = (somaC x) / fromIntegral (tamanhoL mn)

somaC :: [Double] -> Double


somaC [] = 0
somaC x = head x + somaC (tail x)

coluna :: [[Double]] -> Int -> [Double]


coluna mn y = [mn!!x!!y | x <- [0..tamanhoL mn -1]]

-- Passo 2 subtrair médias

subtrairMedias :: [[Double]] -> [[Double]]


subtrairMedias mn = [ subM mn x | x <- mn ]

-1-
C:\Users\Usuario\Desktop\pca\trab-CC-muriloscalser-victormoura.hs sexta-feira, 1 de julho de 2016 08:37

subM :: [[Double]] -> [Double] -> [Double]


subM mn lin = [x - y | (x,y) <- zip (lin) (mediasColunas mn)]

-- Passo 3 calcular a covariancia


duplas :: [[Double]] -> [[(Double, Double)]]
duplas xs = if (tamanhoL xs) >= (tamanhoC xs)
then [zip ((transpoe xs)!!a) ((transpoe xs)!!b)| a<-[0..(tamanhoC xs)-1], b<-[0..(
tamanhoC xs)-1]]
else [zip ((transpoe xs)!!a) ((transpoe xs)!!b)| a<-[0..(tamanhoL xs)-1], b<-[0
..(tamanhoL xs)-1]]

covageral :: [[Double]] -> [Double]


covageral xs = [cov (cov1 x xs)| x <-[0..(qntDuplas xs-1)]]
where
cov x = (sum x)/fromIntegral((tamanhoL xs)-1)
transpoe :: [[Double]] -> [[Double]]
transpoe xs = transpose (subtrairMedias xs)

calcCovariancia :: [[Double]] -> [[Double]]


calcCovariancia xs = juntar (intsqrt(length (covageral xs))) (covageral xs)

qntDuplas :: [[Double]] -> Int


qntDuplas xs = length (duplas xs)

cov1 :: Int -> [[Double]] -> [Double]


cov1 a xs = [(x*y)|(x,y)<-(duplas xs)!!a]

juntar :: Int -> [Double] -> [[Double]]


juntar n [] = []
juntar n xs = take n xs : juntar n (drop n xs)

-- Passo 4 calcular autovetor e autovalor

calcautovetor :: [[Double]] -> (Vector Double, Matrix Double)


calcautovetor x = eigSH (fromLists(calcCovariancia x))

calcAutovetores :: [[Double]] -> Matrix Double


calcAutovetores x = snd (calcautovetor x)

calcautovalor :: [[Double]] -> [Double]


calcautovalor xs = toList (fst (calcautovetor xs))

-- Passo 5 calcular PCA


pca :: [[Double]] -> [[Double]]
pca xs = toLists ( multiply (calcAutovetores xs) (fromLists (subtrairMedias xs)))

-- Passo 6 calcular porcentagem de inportancia

calcComp :: Int -> [[Double]] -> Double


calcComp x xs = ((calcautovalor2 x xs)/(sum (calcautovalor xs)))*100

calcautovalor2 :: Int -> [[Double]] -> Double


calcautovalor2 x xs = (calcautovalor xs)!!x

-2-
C:\Users\Usuario\Desktop\pca\trab-CC-muriloscalser-victormoura.hs sexta-feira, 1 de julho de 2016 08:37

var2comp :: [[Double]] -> (Double, Double)


var2comp xs = (v1,v2)
where
v1 = if length (calcautovalor y) == 0
then 0
else if length (calcautovalor y) == 1
then calcComp 0 y
else (calcComp 0 y)+(calcComp 1 y)
v2 = if length (calcautovalor y) <= 2
then v1
else (v1)+(calcComp 2 y)
y = calcCovariancia xs

-- Passo 7 salvar (EXTRA)

salvarPCA xs = writeFile "saida.txt" (trabalho)


where
trabalho = show(pca xs)

-- Matriz Monstro
cstr :: [[Double]]
cstr = [[19.891693, 0.249788, 29.698685, 2.005862, 2.820051, 17.114915, 80.136870, 0.920493,
0.249945, 19.991594, 10.006807, 0.101470, 0.656344, 0.916689, 0.000039, -0.139851, -0.000004,
0.000005], [19.710231, 0.249869, 30.068137, 2.003634, 2.807258, 17.178408, 79.955560,
0.918435, 0.250042, 19.673516, 10.001338, 0.101860, 0.648244, 0.918104, 0.000036, -0.140238,
-0.000001, -0.000001], [20.096349, 0.250082, 30.164942, 1.993293, 2.820896, 17.063185,
80.065110, 0.913226, 0.250081, 19.759295, 9.993569, 0.101961, 0.631561, 0.912570, 0.000039, -
0.139698, -0.000002, 0.000006], [19.954003, 0.250054, 30.008664, 1.984238, 2.837320,
17.208896, 79.968522, 0.910439, 0.250069, 19.858403, 9.996430, 0.101943, 0.621190, 0.909668,
0.000035, -0.140556, -0.000002, 0.000008], [20.074542, 0.249824, 30.004515, 2.009307,
2.826195, 17.223467, 79.838856, 0.908260, 0.250026, 20.110189, 9.996665, 0.101708, 0.615834,
0.908178, 0.000030, -0.140618, 0.000008, 0.000003], [19.709171, 0.249650, 29.765106, 1.991886
, 2.821637, 17.169465, 79.813918, 0.907377, 0.249991, 19.796823, 9.980586, 0.101586, 0.612963
, 0.907476, 0.000032, -0.141057, 0.000003, 0.000008], [20.149984, 0.249969, 29.890651,
2.005336, 2.820944, 17.050606, 79.999683, 0.906917, 0.249974, 20.257238, 9.992338, 0.101556,
0.611528, 0.906995, 0.000041, -0.140040, -0.000007, 0.000008], [20.113399, 0.249968,
29.961177, 1.990906, 2.803978, 17.222159, 80.158457, 0.906896, 0.249977, 19.928680, 10.003719
, 0.101698, 0.610807, 0.906831, 0.000042, -0.141795, -0.000009, 0.000008], [19.854430,
0.249998, 29.873506, 2.002068, 2.794450, 17.463522, 79.895246, 0.906674, 0.249988, 19.864469,
9.995021, 0.101488, 0.610305, 0.906678, 0.000036, -0.141092, 0.000002, 0.000011], [19.934137
, 0.250073, 29.921399, 2.008263, 2.836355, 17.059480, 80.433378, 0.906414, 0.249999,
20.182075, 10.017883, 0.101602, 0.610197, 0.906531, 0.000033, -0.140278, -0.000000, 0.000010
], [19.870924, 0.249821, 29.724508, 1.995376, 2.813022, 17.006763, 79.814042, 0.906353,
0.250008, 20.017206, 10.008487, 0.101768, 0.610005, 0.906534, 0.000027, -0.140756, -0.000005,
-0.000001], [19.927900, 0.250182, 30.094190, 1.992067, 2.809849, 17.141738, 79.828114,
0.905970, 0.250008, 20.102167, 9.989830, 0.101561, 0.609886, 0.906491, 0.000033, -0.140685, -
0.000009, 0.000001], [20.149969, 0.250337, 29.757880, 1.992681, 2.873195, 17.174502,
79.999669, 0.906288, 0.250004, 20.120501, 9.999804, 0.101701, 0.609879, 0.906579, 0.000027, -
0.140852, 0.000009, -0.000010], [19.892940, 0.250038, 30.000356, 2.006933, 2.823228,
17.022464, 80.156230, 0.906788, 0.250000, 19.962671, 10.000983, 0.101599, 0.609773, 0.906592,
0.000035, -0.141492, 0.000005, 0.000013], [20.049867, 0.250059, 30.057962, 1.997041,
2.834237, 17.040655, 79.789059, 0.906513, 0.249995, 19.965505, 9.985576, 0.101666, 0.609801,
0.906426, 0.000027, -0.141342, -0.000007, -0.000002], [19.925525, 0.250008, 30.176342,
2.000792, 2.822444, 16.872528, 80.116823, 0.907134, 0.250000, 19.996750, 10.013818, 0.101607,
0.609881, 0.906526, 0.000028, -0.142041, 0.000002, 0.000004], [20.028034, 0.250161,
30.170204, 2.013764, 2.831575, 17.059310, 79.860533, 0.906559, 0.249997, 20.172106, 10.000122
, 0.101632, 0.609809, 0.906475, 0.000023, -0.141212, -0.000006, -0.000002], [20.043119,

-3-
C:\Users\Usuario\Desktop\pca\trab-CC-muriloscalser-victormoura.hs sexta-feira, 1 de julho de 2016 08:37

0.250335, 30.046440, 1.988998, 2.827606, 17.322573, 79.836855, 0.906872, 0.249997, 19.807824,


10.002425, 0.101624, 0.609778, 0.906611, 0.000034, -0.141340, -0.000002, -0.000004], [
20.015043, 0.249932, 30.007786, 2.011249, 2.808382, 17.281780, 79.973799, 0.906396, 0.249999,
19.996131, 10.020326, 0.101583, 0.609787, 0.906616, 0.000025, -0.142426, 0.000004, 0.000007
], [19.982728, 0.249969, 29.768529, 2.001507, 2.807464, 17.283273, 80.094672, 0.906594,
0.250002, 19.825877, 10.005940, 0.101704, 0.609734, 0.906497, 0.000029, -0.141615, -0.000005,
0.000002], [19.988424, 0.250192, 30.088504, 2.004965, 2.873556, 17.195442, 80.246762,
0.906549, 0.249999, 19.960381, 9.998978, 0.101558, 0.609770, 0.906464, 0.000030, -0.141900, -
0.000001, -0.000005], [20.026495, 0.249827, 30.173660, 2.006700, 2.844735, 17.252700,
79.825197, 0.906515, 0.250003, 20.097949, 9.991561, 0.101618, 0.609774, 0.906541, 0.000035, -
0.141614, -0.000004, 0.000008], [20.185516, 0.250076, 30.049035, 2.006617, 2.772515,
17.338901, 80.078517, 0.906690, 0.249998, 20.210865, 9.983098, 0.101719, 0.609845, 0.906547,
0.000037, -0.141521, 0.000004, 0.000002], [19.875546, 0.250259, 30.069159, 1.980033, 2.840762
, 16.974474, 79.972933, 0.906777, 0.249997, 19.953237, 9.997631, 0.101682, 0.609869, 0.906605
, 0.000028, -0.141452, 0.000003, 0.000009], [19.741191, 0.250273, 30.149161, 1.991268,
2.829513, 17.018825, 79.810689, 0.906405, 0.249995, 19.927804, 9.996772, 0.101629, 0.609842,
0.906635, 0.000018, -0.141589, -0.000005, 0.000008], [20.065235, 0.250077, 30.038691,
2.017057, 2.834955, 17.182837, 79.953715, 0.906905, 0.249998, 20.187166, 10.026000, 0.101736,
0.609807, 0.906473, 0.000027, -0.141506, -0.000003, 0.000001], [19.931453, 0.250076,
30.050327, 2.005997, 2.796482, 16.925992, 80.085209, 0.905691, 0.249997, 20.052365, 10.002428
, 0.101715, 0.609718, 0.906470, 0.000026, -0.141871, 0.000001, 0.000014], [20.015198,
0.250209, 29.824152, 2.004422, 2.844338, 17.108670, 79.936358, 0.906617, 0.250001, 20.091193,
10.004867, 0.101631, 0.609772, 0.906556, 0.000026, -0.141068, -0.000011, 0.000007], [
20.028035, 0.250252, 30.169161, 1.993652, 2.819140, 17.224754, 80.181937, 0.906307, 0.249999,
20.189462, 10.000631, 0.101652, 0.609720, 0.906512, 0.000026, -0.142058, -0.000008, 0.000010
], [19.858718, 0.250080, 29.865120, 1.980219, 2.850690, 17.124571, 80.023558, 0.906437,
0.250003, 20.195266, 9.974546, 0.101598, 0.609786, 0.906477, 0.000020, -0.142438, -0.000001,
0.000003], [20.109387, 0.250218, 29.869289, 1.993983, 2.822852, 17.061753, 79.773395,
0.906250, 0.249999, 20.103131, 9.981421, 0.101722, 0.609859, 0.906487, 0.000027, -0.142698, -
0.000002, 0.000005], [20.045243, 0.249797, 30.167397, 2.008724, 2.826501, 17.196789,
79.962618, 0.906358, 0.250002, 19.988150, 9.995484, 0.101653, 0.609844, 0.906554, 0.000019, -
0.141818, -0.000001, 0.000002], [19.996554, 0.249918, 29.927810, 2.011433, 2.839944,
17.262384, 80.032425, 0.906912, 0.250000, 19.787865, 10.001831, 0.101710, 0.609764, 0.906525,
0.000027, -0.143251, -0.000006, 0.000002], [19.837865, 0.250221, 30.192432, 1.990146,
2.828733, 17.177737, 79.808037, 0.906506, 0.249999, 19.954794, 9.998857, 0.101630, 0.609842,
0.906574, 0.000025, -0.142590, -0.000007, -0.000003], [20.295664, 0.249835, 30.024245,
1.989515, 2.838059, 17.108351, 80.083728, 0.906030, 0.249998, 20.127805, 10.000579, 0.101631,
0.609839, 0.906567, 0.000014, -0.142015, -0.000005, 0.000010], [20.093602, 0.250094,
30.111036, 2.005307, 2.837919, 16.958707, 79.741629, 0.905864, 0.250000, 20.052672, 9.980700,
0.101692, 0.609783, 0.906532, 0.000021, -0.143130, -0.000004, 0.000014], [19.988146,
0.249733, 30.253715, 1.996623, 2.816086, 17.038437, 79.765111, 0.906880, 0.249997, 19.977132,
9.985248, 0.101623, 0.609807, 0.906512, 0.000023, -0.142075, -0.000006, 0.000008], [
20.000490, 0.249496, 29.744866, 1.995975, 2.839501, 17.216052, 80.139137, 0.906829, 0.249999,
20.260189, 9.994850, 0.101618, 0.609837, 0.906600, 0.000016, -0.142493, -0.000003, 0.000005
], [20.196453, 0.250154, 30.286706, 1.982656, 2.841260, 17.272621, 80.024620, 0.906859,
0.249999, 19.767341, 9.994993, 0.101691, 0.609834, 0.906562, 0.000014, -0.142320, -0.000000,
0.000005],[20.180359, 0.250002, 30.247794, 2.028905, 2.795883, 17.232929, 79.891391, 0.906984
, 0.250001, 19.967181, 10.015538, 0.101656, 0.609792, 0.906565, 0.000016, -0.142419, 0.000004
, 0.000012], [19.944198, 0.249780, 30.002054, 1.995397, 2.807039, 17.249140, 80.318070,
0.906971, 0.249997, 20.131540, 9.998492, 0.101603, 0.609769, 0.906433, 0.000024, -0.142887, -
0.000006, 0.000000], [20.333274, 0.249836, 30.255723, 1.993727, 2.848129, 17.021823,
80.094380, 0.906509, 0.249998, 19.932372, 10.010135, 0.101668, 0.609861, 0.906505, 0.000010,
-0.143483, -0.000007, 0.000011], [20.006675, 0.250150, 29.933124, 2.001418, 2.832608,
17.226167, 79.685708, 0.906539, 0.250005, 20.007607, 9.992795, 0.101588, 0.609877, 0.906563,
0.000019, -0.143289, -0.000002, 0.000003], [19.830909, 0.250013, 30.185632, 2.000711,
2.813870, 17.180576, 79.589156, 0.906922, 0.250001, 20.003880, 10.013605, 0.101653, 0.609865,
0.906572, 0.000015, -0.143267, 0.000008, 0.000014], [19.910813, 0.250088, 30.200166,

-4-
C:\Users\Usuario\Desktop\pca\trab-CC-muriloscalser-victormoura.hs sexta-feira, 1 de julho de 2016 08:37

16.822847, 79.900073, 0.900801, 0.260558, 20.300248, 9.984784, 0.169175, 0.591916, 0.934865,


0.000008, -0.148080, 0.000001, 0.000287], [19.972993, 0.292945, 30.075850, 2.030698, 3.047758
, 16.861383, 79.733482, 0.974638, 0.265020, 19.691981, 9.982464, 0.299142, 0.975305, 1.000005
, -0.000004, -0.148890, 0.000006, 0.000384], [19.880318, 0.297259, 30.105238, 2.072137,
3.144077, 16.873707, 80.300869, 0.977234, 0.266425, 19.821396, 10.007977, 0.493237, 0.999985,
1.000027, 0.000014, -0.150231, -0.000006, 0.000416], [19.911360, 0.299173, 30.082847,
2.074131, 3.144859, 16.548071, 81.183441, 0.976823, 0.266945, 19.668875, 10.007342, 0.752482,
1.000043, 1.000045, 0.000006, -0.149672, 0.000003, 0.000422], [20.204575, 0.299475,
30.107967, 2.110881, 3.112469, 16.827632, 82.072129, 0.976630, 0.267153, 19.750717, 10.006646
, 0.999917, 1.000039, 0.999884, 0.000001, -0.150080, -0.000008, 0.000442], [20.080704,
0.299840, 30.222214, 2.129765, 3.067741, 16.996924, 82.635207, 0.976811, 0.267213, 19.968361,
9.997453, 0.999950, 1.000018, 0.999944, 0.000007, -0.151362, -0.000001, 0.000436], [
19.930299, 0.299596, 30.236115, 2.150165, 2.976645, 17.055126, 83.178675, 0.976911, 0.267269,
19.714750, 10.007033, 0.999981, 0.999952, 0.999917, 0.000003, -0.151184, -0.000009, 0.000436
], [20.208942, 0.299872, 29.867773, 2.171499, 2.956152, 17.080510, 83.877231, 0.976892,
0.267331, 19.730212, 9.995533, 0.999979, 1.000006, 0.999958, 0.000008, -0.152460, -0.000010,
0.000439], [19.920055, 0.300236, 30.037649, 2.193517, 2.866466, 17.032891, 84.564152,
0.976344, 0.267392, 19.948988, 10.004694, 1.000025, 1.000043, 1.000002, 0.000010, -0.152219,
-0.000009, 0.000431], [19.854550, 0.300231, 30.101100, 2.217668, 2.799491, 16.839916,
84.912596, 0.976774, 0.267447, 20.328033, 10.013300, 0.999891, 1.000071, 1.000070, 0.000002,
-0.153841, -0.000009, 0.000451], [20.045169, 0.299932, 29.940112, 2.238213, 2.810770,
17.270229, 85.387370, 0.976993, 0.267508, 20.028140, 9.998076, 1.000043, 1.000055, 1.000054,
0.000005, -0.154935, 0.000001, 0.000438], [20.166603, 0.299937, 30.139522, 2.252135, 2.723958
, 17.138232, 85.775856, 0.976800, 0.267563, 19.748641, 9.992141, 0.999926, 1.000039, 1.000140
, 0.000001, -0.155975, -0.000003, 0.000435], [20.131751, 0.299774, 29.915017, 2.280715,
2.665046, 17.545537, 85.759183, 0.976855, 0.267626, 19.837998, 10.003074, 0.999932, 1.000034,
1.000023, -0.000003, -0.157536, 0.000003, 0.000433], [20.284770, 0.300112, 30.074189,
2.300058, 2.666494, 17.166765, 86.063299, 0.976669, 0.267684, 20.052816, 10.009723, 0.999961,
0.999983, 1.000124, -0.000003, -0.158810, -0.000001, 0.000436], [19.897917, 0.299748,
30.018182, 2.304222, 2.587960, 17.200991, 86.338320, 0.976575, 0.267744, 19.962813, 9.994289,
1.000080, 1.000068, 0.999960, -0.000002, -0.159364, -0.000006, 0.000434], [20.012794,
0.299919, 29.920291, 2.348572, 2.588128, 17.421244, 86.760449, 0.977104, 0.267801, 20.006825,
9.996480, 0.999998, 1.000092, 0.999954, -0.000000, -0.161581, -0.000009, 0.000429], [
19.982661, 0.299905, 30.145745, 2.360607, 2.564638, 17.438299, 86.497082, 0.976658, 0.267861,
20.051248, 10.010120, 0.999994, 1.000022, 0.999932, -0.000002, -0.160745, -0.000003,
0.000435], [20.010923, 0.299840, 30.107707, 2.379869, 2.502690, 17.422530, 86.775126,
0.976874, 0.267916, 20.065634, 10.006857, 0.999935, 1.000028, 0.999991, -0.000002, -0.164254,
-0.000007, 0.000434], [20.038284, 0.300159, 30.136408, 2.403691, 2.480105, 17.462177,
87.003652, 0.976366, 0.267972, 20.274537, 9.989163, 1.000001, 1.000014, 0.999956, -0.000002,
-0.165128, 0.000001, 0.000425], [20.060988, 0.300204, 29.787078, 2.424315, 2.471625,
17.533793, 86.962334, 0.976776, 0.268035, 19.882539, 10.002337, 1.000002, 1.000017, 0.999990,
-0.000003, -0.166343, -0.000006, 0.000439], [20.064808, 0.300230, 30.118715, 2.440206,
2.474523, 17.398615, 87.051903, 0.976821, 0.268092, 20.073918, 10.011820, 1.000045, 0.999957,
0.999980, 0.000003, -0.167258, -0.000011, 0.000426], [20.008670, 0.299817, 29.885958,
2.469441, 2.429764, 17.578857, 87.133405, 0.977144, 0.268150, 19.794903, 9.994209, 0.999964,
1.000051, 0.999954, 0.000001, -0.169909, -0.000002, 0.000422], [19.844575, 0.300367,
29.881079, 2.471963, 2.415610, 17.512132, 87.145856, 0.976691, 0.268207, 20.088330, 10.018201
, 1.000029, 0.999985, 1.000056, -0.000002, -0.171399, -0.000009, 0.000423], [20.125630,
0.299789, 29.779929, 2.520122, 2.336091, 17.880243, 87.398257, 0.976900, 0.268266, 19.850694,
10.010844, 0.999932, 1.000008, 1.000062, -0.000002, -0.171837, -0.000013, 0.000431], [
19.892684, 0.299717, 30.231212, 2.528367, 2.346942, 17.607251, 87.259572, 0.976776, 0.268321,
20.061225, 9.990929, 1.000009, 1.000037, 1.000080, -0.000012, -0.174256, 0.000002, 0.000431
], [20.055088, 0.299931, 29.948870, 2.549989, 2.322228, 17.645729, 87.416506, 0.977142,
0.268381, 20.155854, 9.998086, 0.999979, 0.999958, 1.000001, -0.000006, -0.175634, -0.000011,
0.000436], [19.870250, 0.300120, 29.889717, 2.584797, 2.342273, 17.423226, 87.619821,
0.976795, 0.268440, 20.072662, 9.997585, 0.999962, 0.999962, 0.999947, -0.000009, -0.176331,
0.000000, 0.000423], [20.004444, 0.299771, 29.990061, 2.602835, 2.318176, 17.669671,

-5-

Vous aimerez peut-être aussi