Skip to content

Instantly share code, notes, and snippets.

@rleonid
Last active September 8, 2015 16:57
Show Gist options
  • Save rleonid/8aa674101954818ce804 to your computer and use it in GitHub Desktop.
Save rleonid/8aa674101954818ce804 to your computer and use it in GitHub Desktop.
Oml classification example
# open Classify ;;
# let feature_map = function | `shortbread -> 0 | `lager -> 1 | `whiskey -> 2 | `porridge -> 3 | `football-> 4 ;;
val feature_map : [< `football | `lager | `porridge | `shortbread | `whiskey ] -> int = <fun>
# let data =
[
(`English, [`whiskey; `porridge; `football]);
(`English, [`shortbread; `whiskey; `porridge]);
(`English, [`shortbread; `lager; `football]);
(`English, [`shortbread; `lager]);
(`English, [`lager; `football]);
(`English, [`porridge]);
(`Scottish, [`shortbread; `porridge; `football]);
(`Scottish, [`shortbread; `lager; `football]);
(`Scottish, [`shortbread; `lager; `whiskey; `porridge]);
(`Scottish, [`shortbread; `lager; `porridge]);
(`Scottish, [`shortbread; `lager; `porridge; `football]);
(`Scottish, [`shortbread; `whiskey; `porridge]);
(`Scottish, [`shortbread; `whiskey])
] ;;
val data : ([> `English | `Scottish ] * [> `football | `lager | `porridge | `shortbread | `whiskey ] list) list =
...
# let to_feature l = l |> List.map feature_map |> Array.of_list ;;
val to_feature : [< `football | `lager | `porridge | `shortbread | `whiskey ] list -> int array = <fun>
# module NB = BinomialNaiveBayes(
struct
type feature = [ `shortbread | `lager | `whiskey | `porridge | `football ] list
type clas = [ `English | `Scottish]
let encoding = to_feature
let size = 5
end);;
module NB :
sig
type clas = [ `English | `Scottish ]
type feature = [ `football | `lager | `porridge | `shortbread | `whiskey ] list
type spec = Oml.Classify.binomial_spec
val default : spec
type t
val eval : t -> feature -> clas Oml.Classify.probabilities
type samples = (clas * feature) list
val estimate : ?spec:spec -> ?classes:clas list -> samples -> t
val class_probabilities : t -> clas -> float * (feature -> float array)
end
# let naiveb = NB.estimate ~spec:{NB.default with bernoulli = true} data ;;
val naiveb : NB.t = <abstr>
# let sample = [ `shortbread ; `whiskey; `porridge ] ;;
val sample : [> `porridge | `shortbread | `whiskey ] list = [`shortbread; `whiskey; `porridge]
# let result = NB.eval naiveb sample;;
val result : NB.clas Oml.Classify.probabilities = [(`English, 0.192372406057206957); (`Scottish, 0.807627593942793)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment