fix(recall, precision): little bug in calculations

feat(fmeasure): calculate fmeasure using recall and precision
This commit is contained in:
Mahdi Dibaiee 2016-07-29 22:09:30 +04:30
parent 812717522e
commit 76e7e7faef
2 changed files with 15 additions and 6 deletions

View File

@ -24,6 +24,11 @@ module Main
let showResults (c, r) = putStrLn (classes !! c ++ " ~ " ++ classes !! r)
mapM_ showResults results
let showAccuracy (c, r) =
print $ genericLength (filter (\(h, j) -> h == j && h == c) results) / genericLength results
mapM_ showAccuracy results
putStrLn $ "Recall: " ++ show (recall results) ++ "%"
putStrLn $ "Precision: " ++ show (precision results) ++ "%"
putStrLn $ "F Measure: " ++ show (fmeasure (precision results) (recall results))
putStrLn $ "Accuracy: " ++ show (accuracy results) ++ "%"

View File

@ -9,6 +9,7 @@ module Sibe.NaiveBayes
accuracy,
precision,
recall,
fmeasure,
)
where
import Data.List
@ -74,24 +75,27 @@ module Sibe.NaiveBayes
let correct = filter (uncurry (==)) results
in round $ genericLength correct / genericLength results * 100
recall :: [(Int, Int)] -> Int
recall :: [(Int, Int)] -> Double
recall results =
let classes = ordNub (map fst results)
s = sum (map rec classes) / genericLength results
in round $ s * 100
s = sum (map rec classes) / genericLength classes
in s * 100
where
rec a =
let t = genericLength $ filter (\(c, r) -> c == r && c == a) results
y = genericLength $ filter (\(c, r) -> c == a) results
in t / y
precision :: [(Int, Int)] -> Int
precision :: [(Int, Int)] -> Double
precision results =
let classes = ordNub (map fst results)
s = sum (map prec classes) / genericLength results
in round $ s * 100
s = sum (map prec classes) / genericLength classes
in s * 100
where
prec a =
let t = genericLength $ filter (\(c, r) -> c == r && c == a) results
y = genericLength $ filter (\(c, r) -> r == a) results
in t / y
fmeasure :: Double -> Double -> Double
fmeasure r p = (2 * p * r) / (p + r)