accuracy can work on probs now

This commit is contained in:
Filip Gralinski 2018-04-07 21:13:37 +02:00
parent ff8ec8880e
commit cea084c789
6 changed files with 39 additions and 2 deletions

View File

@ -1,5 +1,5 @@
name: geval
version: 0.5.3.0
version: 0.5.4.0
synopsis: Machine learning evaluation tools
description: Please see README.md
homepage: http://github.com/name/project

View File

@ -47,6 +47,7 @@ import Data.Text
import Data.Text.Read as TR
import Control.Applicative
import Control.Exception
import Text.Read (readMaybe)
import Control.Conditional (unlessM, whenM)
import qualified System.Directory as D
import System.Posix
@ -312,7 +313,20 @@ gevalCore' BLEU _ = gevalCoreWithoutInput (Right . Prelude.map Prelude.words . D
| otherwise = exp (1.0 - (r /. c))
gevalCore' Accuracy _ = gevalCoreWithoutInput (Right . strip) (Right . strip) hitOrMiss averageC id
where hitOrMiss (x,y) = if x == y then 1.0 else 0.0
where hitOrMiss (exp,got) = if (normalizeProbForAccuracy exp got) == exp then 1.0 else 0.0
-- if the expected value is 0 or 1 treat values between 0.0 and 1.0 as probabilities
-- for the positive outcome
normalizeProbForAccuracy :: Text -> Text -> Text
normalizeProbForAccuracy exp got
| exp == (pack "1") = case tryReadingAsFloat got of
Just p -> if p >= 0.5 && p <= 1.0 then exp else got
Nothing -> got
| exp == (pack "0") = case tryReadingAsFloat got of
Just p -> if p < 0.5 && p >= 0.0 then exp else got
Nothing -> got
| otherwise = got
tryReadingAsFloat :: Text -> Maybe Float
tryReadingAsFloat = readMaybe . unpack
gevalCore' (FMeasure beta) _ = gevalCoreWithoutInput outParser outParser getCount countAgg (fMeasureOnCounts beta)
where outParser = detected <=< (getValue . TR.double)

View File

@ -53,6 +53,8 @@ main = hspec $ do
describe "Accuracy" $ do
it "simple example" $
runGEvalTest "accuracy-simple" `shouldReturnAlmost` 0.6
it "with probs" $
runGEvalTest "accuracy-probs" `shouldReturnAlmost` 0.4
describe "F-measure" $ do
it "simple example" $
runGEvalTest "f-measure-simple" `shouldReturnAlmost` 0.57142857

View File

@ -0,0 +1,10 @@
1
0
foo
0
-1.5
0.7
1.001
0.3
0.45
0.45
1 1
2 0
3 foo
4 0
5 -1.5
6 0.7
7 1.001
8 0.3
9 0.45
10 0.45

View File

@ -0,0 +1 @@
--metric Accuracy

View File

@ -0,0 +1,10 @@
1
0
0
1
0
1
1
1
1
0
1 1
2 0
3 0
4 1
5 0
6 1
7 1
8 1
9 1
10 0