module Data.DecisionTree (
build,
decide,
Datum(D, dName, attributes),
PreLabeled,
Attribute(A, aName, possibleValues),
DecisionTree) where
import Data.Maybe (fromJust)
import Data.List hiding (partition)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Function (on)
type PreLabeled a b= (b, Datum a)
data DecisionTree a b= Leaf b
| Node {
att ::Attribute a,
child :: a -> (DecisionTree a b)
}
data Attribute a = A {
aName :: String,
possibleValues :: [a]
}
data Datum a= D {
dName :: String,
attributes :: [(Attribute a,a)]
} deriving Show
instance (Show a, Show b) => Show (DecisionTree a b) where
show x = showTree x ""
showTree :: (Show a, Show b) => DecisionTree a b -> ShowS
showTree (Leaf x) = shows x
showTree (Node att child) = ('<':).shows att.("|\n"++).showList [child a | a <- possibleValues att].('>':)
instance Eq (Attribute a) where
(==) = (==) `on` aName
instance Show (Attribute a) where
show = aName
build :: (Ord a, Ord b) => [Attribute a] -> [PreLabeled a b] -> DecisionTree a b
build atts dataset = case inf of
0 -> Leaf dominantLabel
_ -> Node {
att = bAtt,
child = safeLookup
}
where
(inf,bAtt) = bestAttribute dataset atts