module HasGP.Data.Normalise where
import Numeric.LinearAlgebra
import HasGP.Types.MainTypes
import HasGP.Support.Linear as L
import HasGP.Support.Functions as F
exampleMean :: Inputs
-> DVector
exampleMean examples =
fromList $ map (L.sumVectorDiv r) (toColumns examples)
where
r = rows examples
exampleVariance :: Inputs
-> DVector
exampleVariance examples =
fromList $ map (L.sumVectorDiv r)
(toColumns $ fromRows $ map ((mapVector (^2)) . (\x -> xm))
(toRows examples))
where
r = rows examples
m = exampleMean examples
exampleMeanVariance :: Inputs
-> (DVector, DVector)
exampleMeanVariance examples = (exampleMean examples, exampleVariance examples)
normaliseMeanVariance :: DVector
-> DVector
-> Inputs
-> Inputs
normaliseMeanVariance newMean newVariance examples =
fromRows $ map (\x -> x+newMean) varianceAdjusted
where
(m, v) = exampleMeanVariance examples
zeroMean = map (\x -> xm) (toRows examples)
varianceAdjustment = zipVectorWith (\x y -> (sqrt x)/(sqrt y))
newVariance v
varianceAdjusted = map (zipVectorWith (*) varianceAdjustment) zeroMean
normaliseMeanVarianceSimple :: Double
-> Double
-> Inputs
-> Inputs
normaliseMeanVarianceSimple newMean newVariance examples =
normaliseMeanVariance (constant newMean c) (constant newVariance c) examples
where
c = cols examples
normaliseBetweenLimits :: Double
-> Double
-> Inputs
-> Inputs
normaliseBetweenLimits min max examples =
fromColumns $ zipWith (\x y -> mapVector (x+) y)
cV (zipWith scale mV columns)
where
columns = toColumns examples
minV = map minElement columns
maxV = map maxElement columns
mV = zipWith (\x y -> ((max min) / (y x))) minV maxV
cV = zipWith (\x y -> (min (y * x))) minV mV
findRedundantAttributes :: Inputs
-> [Bool]
findRedundantAttributes examples = map allSame columns
where
columns = map toList (toColumns examples)
allSame [] = True
allSame [h] = True
allSame [h1,h2] = (h1 == h2)
allSame (h1:h2:t) = (h1 == h2) && (allSame (h2:t))
listRedundantAttributes :: Inputs
-> [Int]
listRedundantAttributes examples = findColumns boolean 1 []
where
boolean = findRedundantAttributes examples
findColumns [] n result = reverse result
findColumns (h:t) n result
| h = findColumns t (n+1) (n:result)
| otherwise = findColumns t (n+1) result
removeRedundantAttributes :: Inputs
-> Inputs
removeRedundantAttributes examples =
fromColumns $ removeTrueColumns [] r (toColumns examples)
where
r = findRedundantAttributes examples
removeTrueColumns result [] []
= reverse result
removeTrueColumns result (True:t1) (c:t2)
= removeTrueColumns result t1 t2
removeTrueColumns result (False:t1) (c:t2)
= removeTrueColumns (c:result) t1 t2
retainAttributes :: [Int]
-> Inputs
-> Inputs
retainAttributes l m = trans $ extractRows l2 $ trans m
where
l2 = map (\x -> x1) l
confusionMatrix :: Targets
-> Outputs
-> (Double,Double,Double,Double)
confusionMatrix correct predicted =
cm (toList correct) (toList predicted) (0,0,0,0)
where
cm [] [] result = result
cm (h1:t1) (h2:t2) (a,b,c,d) = case (h1, h2) of
(1.0, 1.0) -> cm t1 t2 (a,b,c,d+1)
(1.0,1.0) -> cm t1 t2 (a,b,c+1,d)
(1.0, 1.0) -> cm t1 t2 (a,b+1,c,d)
(1.0,1.0) -> cm t1 t2 (a+1,b,c,d)
cm _ _ result
= error "Correct and predicted vectors must have the same length"
printConfusionMatrix :: Targets
-> Outputs
-> IO ()
printConfusionMatrix correct predicted = do
let (a,b,c,d) = confusionMatrix correct predicted
let n = a+b+c+d
let trueP = d/(d+c)
let precision = d/(d+b)
putStrLn ("------------------------------------------------")
putStrLn ("Correct -1, Predicted -1: a = " ++ (show a))
putStrLn ("Correct -1, Predicted +1: b = " ++ (show b))
putStrLn ("Correct +1, Predicted -1: c = " ++ (show c))
putStrLn ("Correct +1, Predicted +1: d = " ++ (show d))
putStrLn ("------------------------------------------------")
putStrLn ("Number of examples: n = a+b+c+d = " ++ (show n))
putStrLn ("Accuracy: a+d/n = " ++ (show ((a+d)/n)))
putStrLn ("Recall/True Positive: d/d+c = " ++ (show trueP))
putStrLn ("False Positive: b/b+a = " ++ (show (b/(b+a))))
putStrLn ("True Negative: a/b+a = " ++ (show (a/(b+a))))
putStrLn ("False Negative: c/d+c = " ++ (show (c/(d+c))))
putStrLn ("Precision: d/d+b = " ++ (show precision))
putStrLn ("F Measure (beta = 1) = " ++
(show ((2 * trueP * precision)/(trueP + precision))))
putStrLn ("------------------------------------------------")
return ()
countLabels :: Targets -> IO ()
countLabels v = do
let d = dim v
let plus = length $ filter (==(1.0)) $ toList v
putStrLn ("Total number of labels: " ++ (show d))
putStrLn ("Number of +1 labels: " ++ (show plus))
putStrLn ("Number of -1 labels: " ++ (show (d plus)))
return ()