fix purity
This commit is contained in:
parent
065a3ce9cd
commit
595b2c9650
@ -21,7 +21,7 @@ library
|
|||||||
, GEval.BLEU
|
, GEval.BLEU
|
||||||
, GEval.ClippEU
|
, GEval.ClippEU
|
||||||
, GEval.PrecisionRecall
|
, GEval.PrecisionRecall
|
||||||
, GEval.Purity
|
, GEval.ClusteringMetrics
|
||||||
, GEval.Common
|
, GEval.Common
|
||||||
build-depends: base >= 4.7 && < 5
|
build-depends: base >= 4.7 && < 5
|
||||||
, cond
|
, cond
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
module GEval.Purity
|
module GEval.ClusteringMetrics
|
||||||
(purity, purityFromConfusionMap, updateConfusionMap)
|
(purity, purityFromConfusionMap, updateConfusionMap)
|
||||||
where
|
where
|
||||||
|
|
||||||
@ -12,17 +12,17 @@ purity :: (Hashable a, Eq a, Hashable b, Eq b) => [(a, b)] -> Double
|
|||||||
purity pL = purityFromConfusionMap cM
|
purity pL = purityFromConfusionMap cM
|
||||||
where cM = confusionMap pL
|
where cM = confusionMap pL
|
||||||
|
|
||||||
purityFromConfusionMap :: (Hashable a, Eq a, Hashable b, Eq b) => M.HashMap a (M.HashMap b Int) -> Double
|
purityFromConfusionMap :: (Hashable a, Eq a, Hashable b, Eq b) => M.HashMap b (M.HashMap a Int) -> Double
|
||||||
purityFromConfusionMap cM = numberOfMajorityItems /. numberOfItems
|
purityFromConfusionMap cM = numberOfMajorityItems /. numberOfItems
|
||||||
where numberOfItems = sum $ map fst classCounts
|
where numberOfItems = sum $ map fst classCounts
|
||||||
numberOfMajorityItems = sum $ map snd classCounts
|
numberOfMajorityItems = sum $ map snd classCounts
|
||||||
classCounts = map getClassCount $ M.toList cM
|
classCounts = map getClassCount $ M.toList cM
|
||||||
getClassCount (_, sh) = foldl' (\(s, m) (_, c) -> (s + c, max m c)) (0, 0) $ M.toList sh
|
getClassCount (_, sh) = foldl' (\(s, m) (_, c) -> (s + c, max m c)) (0, 0) $ M.toList sh
|
||||||
|
|
||||||
confusionMap :: (Hashable a, Eq a, Hashable b, Eq b) => [(a, b)] -> M.HashMap a (M.HashMap b Int)
|
confusionMap :: (Hashable a, Eq a, Hashable b, Eq b) => [(a, b)] -> M.HashMap b (M.HashMap a Int)
|
||||||
confusionMap = foldl' updateConfusionMap M.empty
|
confusionMap = foldl' updateConfusionMap M.empty
|
||||||
|
|
||||||
updateConfusionMap :: (Hashable a, Eq a, Hashable b, Eq b) => M.HashMap a (M.HashMap b Int) -> (a, b) -> M.HashMap a (M.HashMap b Int)
|
updateConfusionMap :: (Hashable a, Eq a, Hashable b, Eq b) => M.HashMap b (M.HashMap a Int) -> (a, b) -> M.HashMap b (M.HashMap a Int)
|
||||||
updateConfusionMap h (e, g) = M.insertWith updateSubHash e (unitHash g) h
|
updateConfusionMap h (e, g) = M.insertWith updateSubHash g (unitHash e) h
|
||||||
where unitHash k = M.singleton k 1
|
where unitHash k = M.singleton k 1
|
||||||
updateSubHash uh sh = M.unionWith (+) uh sh
|
updateSubHash uh sh = M.unionWith (+) uh sh
|
10
test/Spec.hs
10
test/Spec.hs
@ -7,7 +7,7 @@ import GEval.OptionsParser
|
|||||||
import GEval.BLEU
|
import GEval.BLEU
|
||||||
import GEval.ClippEU
|
import GEval.ClippEU
|
||||||
import GEval.PrecisionRecall
|
import GEval.PrecisionRecall
|
||||||
import GEval.Purity
|
import GEval.ClusteringMetrics
|
||||||
import Data.Attoparsec.Text
|
import Data.Attoparsec.Text
|
||||||
import Options.Applicative
|
import Options.Applicative
|
||||||
import Data.Text
|
import Data.Text
|
||||||
@ -53,10 +53,10 @@ main = hspec $ do
|
|||||||
precisionCount [["foo", "baz"], ["bar"], ["baz", "xyz"]] ["foo", "bar", "foo"] `shouldBe` 2
|
precisionCount [["foo", "baz"], ["bar"], ["baz", "xyz"]] ["foo", "bar", "foo"] `shouldBe` 2
|
||||||
describe "purity (in flat clustering)" $ do
|
describe "purity (in flat clustering)" $ do
|
||||||
it "the example from Information Retrieval Book" $ do
|
it "the example from Information Retrieval Book" $ do
|
||||||
purity [(2, "o") :: (Int, String), (2, "o"), (2, "d"), (3, "x"), (3, "d"),
|
purity [("o", 2) :: (String, Int), ("o", 2), ("d", 2), ("x", 3), ("d", 3),
|
||||||
(1, "x"), (1, "o"), (1, "x"), (1, "x"), (1, "x"), (1, "x"),
|
("x", 1), ("o", 1), ("x", 1), ( "x", 1), ("x", 1), ("x", 1),
|
||||||
(2, "x"), (2, "o"), (2, "o"),
|
("x", 2), ("o", 2), ("o", 2),
|
||||||
(3, "x"), (3, "d"), (3, "d")] `shouldBeAlmost` 0.70588
|
("x", 3), ("d", 3), ("d", 3)] `shouldBeAlmost` 0.70588
|
||||||
|
|
||||||
describe "reading options" $ do
|
describe "reading options" $ do
|
||||||
it "can get the metric" $ do
|
it "can get the metric" $ do
|
||||||
|
Loading…
Reference in New Issue
Block a user