feat(results): accuracy, recall and precision functions used to calculate measures

fix: read data from another repository
This commit is contained in:
Mahdi Dibaiee 2016-07-29 17:55:59 +04:30
parent eeabe4696c
commit b5b4629318
6 changed files with 49 additions and 11 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "examples/doc-classifier-data"]
path = examples/doc-classifier-data
url = git@github.com:mdibaiee/doc-classifier-data

View File

@ -26,6 +26,8 @@ module Main where
network = session inputs rnetwork labels learning_rate (iterations, epochs)
-- run inputs through the trained network
-- note: here we are using the examples in the training dataset to test the network,
-- this is here just to demonstrate the way the library works, you should not do this
results = map (`forward` network) inputs
-- compute the new cost

@ -0,0 +1 @@
Subproject commit 5b069a54a6a68efee0ef4bb15c1aa56414f12c28

View File

@ -6,24 +6,24 @@ module Main
import Data.List
import Data.Maybe
import Debug.Trace
import Data.List.Split
main = do
dataset <- readFile "examples/naivebayes-doc-classifier/data-reuters"
test <- readFile "examples/naivebayes-doc-classifier/data-reuters-test"
dataset <- readFile "examples/doc-classifier-data/data-reuters"
test <- readFile "examples/doc-classifier-data/data-reuters-test"
classes <- map (filter (/= ' ')) . lines <$> readFile "examples/naivebayes-doc-classifier/data-classes"
classes <- map (filter (/= ' ')) . lines <$> readFile "examples/doc-classifier-data/data-classes"
let intClasses = [0..length classes - 1]
documents = createDocuments classes dataset
testDocuments = createDocuments classes test
nb = initialize documents
let testResults (Document text c) =
let r = determine text nb intClasses documents
in trace (classes !! c ++ " ~ " ++ classes !! r) c == r
results = map (\(Document text c) -> (c, determine text nb intClasses documents)) testDocuments
let results = map testResults testDocuments
let showResults (c, r) = putStrLn (classes !! c ++ " ~ " ++ classes !! r)
mapM_ showResults results
putStr "Accuracy: "
putStr . show . round $ (genericLength (filter (==True) results) / genericLength results) * 100
putStrLn "%"
putStrLn $ "Recall: " ++ show (recall results) ++ "%"
putStrLn $ "Precision: " ++ show (precision results) ++ "%"
putStrLn $ "Accuracy: " ++ show (accuracy results) ++ "%"

View File

@ -50,6 +50,7 @@ executable example-naivebayes-doc-classifier
, sibe
, hmatrix
, containers
, split
default-language: Haskell2010
test-suite sibe-test

View File

@ -4,7 +4,11 @@ module Sibe.NaiveBayes
createDocuments,
initialize,
calculate,
determine
determine,
ordNub,
accuracy,
precision,
recall,
)
where
import Data.List
@ -63,3 +67,30 @@ module Sibe.NaiveBayes
go _ [] = []
go s (x:xs) = if x `Set.member` s then go s xs
else x : go (Set.insert x s) xs
accuracy :: [(Int, Int)] -> Int
accuracy results =
let correct = filter (uncurry (==)) results
in round $ genericLength correct / genericLength results * 100
recall :: [(Int, Int)] -> Int
recall results =
let classes = ordNub (map fst results)
s = sum (map rec classes) / genericLength results
in round $ 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 results =
let classes = ordNub (map fst results)
s = sum (map prec classes) / genericLength results
in round $ 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