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) let showResults (c, r) = putStrLn (classes !! c ++ " ~ " ++ classes !! r)
mapM_ showResults results 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 $ "Recall: " ++ show (recall results) ++ "%"
putStrLn $ "Precision: " ++ show (precision results) ++ "%" putStrLn $ "Precision: " ++ show (precision results) ++ "%"
putStrLn $ "F Measure: " ++ show (fmeasure (precision results) (recall results))
putStrLn $ "Accuracy: " ++ show (accuracy results) ++ "%" putStrLn $ "Accuracy: " ++ show (accuracy results) ++ "%"

View File

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