3 people like it.

numl Machine Learning

This is the getting started sample for the numl machine learning library available at http://numl.net/ written in F#.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
#r @"..\packages\numl.0.7.5\lib\net40\numl.dll"

open numl
open numl.Model
open numl.Supervised.DecisionTree

type Outlook =
    | Sunny = 0
    | Overcast = 1
    | Rainy = 2

type Temperature =
    | Low = 0
    | High = 1

type Tennis =
    {
        [<Feature>] Outlook : Outlook
        [<Feature>] Temperature : Temperature
        [<Feature>] Windy : bool
        [<Label>] Play : bool
    }

    static member New outlook temperature windy play =
        box {
            Outlook = outlook
            Temperature = temperature
            Windy = windy
            Play = play
        }

let data =
    [
        Tennis.New Outlook.Sunny Temperature.Low true true
        Tennis.New Outlook.Sunny Temperature.High true false
        Tennis.New Outlook.Sunny Temperature.High false false
        Tennis.New Outlook.Overcast Temperature.Low true true
        Tennis.New Outlook.Overcast Temperature.High false true
        Tennis.New Outlook.Overcast Temperature.Low false true
        Tennis.New Outlook.Rainy Temperature.Low true false
        Tennis.New Outlook.Rainy Temperature.Low false true
    ]

let descriptor = Descriptor.Create<Tennis>()
let generator = DecisionTreeGenerator descriptor
generator.SetHint false
let model = Learner.Learn(data, 0.8, 1000, generator)

printfn "%A" model

//Learning Model:
//  Generator numl.Supervised.DecisionTree.DecisionTreeGenerator
//  Model:
//	[Outlook, 0,3380]
//	 |- Sunny
//	 |	[Temperature, 1,0000]
//	 |	 |- Low
//	 |	 |	 +(True, 1)
//	 |	 |- High
//	 |	 |	 +(False, -1)
//	 |- Overcast
//	 |	 +(True, 1)
//	 |- Rainy
//	 |	[Windy, 1,0000]
//	 |	 |- False
//	 |	 |	 +(True, 1)
//	 |	 |- True
//	 |	 |	 +(False, -1)
//
//  Accuracy: 100,00 %
namespace numl
namespace numl.Model
namespace numl.Supervised
namespace numl.Supervised.DecisionTree
type Outlook =
  | Sunny = 0
  | Overcast = 1
  | Rainy = 2

Full name: Script.Outlook
Outlook.Sunny: Outlook = 0
Outlook.Overcast: Outlook = 1
Outlook.Rainy: Outlook = 2
type Temperature =
  | Low = 0
  | High = 1

Full name: Script.Temperature
Temperature.Low: Temperature = 0
Temperature.High: Temperature = 1
type Tennis =
  {Outlook: Outlook;
   Temperature: Temperature;
   Windy: bool;
   Play: bool;}
  static member New : outlook:Outlook -> temperature:Temperature -> windy:bool -> play:bool -> obj

Full name: Script.Tennis
Multiple items
type FeatureAttribute =
  inherit NumlAttribute
  new : unit -> FeatureAttribute

Full name: numl.Model.FeatureAttribute

--------------------
FeatureAttribute() : unit
Multiple items
Tennis.Outlook: Outlook

--------------------
type Outlook =
  | Sunny = 0
  | Overcast = 1
  | Rainy = 2

Full name: Script.Outlook
Multiple items
Tennis.Temperature: Temperature

--------------------
type Temperature =
  | Low = 0
  | High = 1

Full name: Script.Temperature
Tennis.Windy: bool
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
Multiple items
type LabelAttribute =
  inherit NumlAttribute
  new : unit -> LabelAttribute
  member GenerateProperty : property:PropertyInfo -> Property

Full name: numl.Model.LabelAttribute

--------------------
LabelAttribute() : unit
Tennis.Play: bool
static member Tennis.New : outlook:Outlook -> temperature:Temperature -> windy:bool -> play:bool -> obj

Full name: Script.Tennis.New
val outlook : Outlook
val temperature : Temperature
val windy : bool
val play : bool
val box : value:'T -> obj

Full name: Microsoft.FSharp.Core.Operators.box
val data : obj list

Full name: Script.data
static member Tennis.New : outlook:Outlook -> temperature:Temperature -> windy:bool -> play:bool -> obj
val descriptor : Descriptor

Full name: Script.descriptor
Multiple items
type Descriptor =
  new : unit -> Descriptor
  member At : i:int -> Property
  member ColumnAt : i:int -> string
  member Convert : item:obj -> IEnumerable<float> + 2 overloads
  member Features : Property[] with get, set
  member GetColumns : unit -> IEnumerable<string>
  member GetSchema : unit -> XmlSchema
  member Item : int -> Property
  member Item : string -> Property
  member Label : Property with get, set
  ...

Full name: numl.Model.Descriptor

--------------------
type Descriptor<'T> =
  inherit Descriptor
  new : unit -> Descriptor<'T>
  member Learn : property:Expression<Func<'T, obj>> -> Descriptor<'T>
  member With : property:Expression<Func<'T, obj>> -> Descriptor<'T>
  member WithDateTime : property:Expression<Func<'T, DateTime>> * features:DateTimeFeature -> Descriptor<'T> + 1 overload
  member WithEnumerable : property:Expression<Func<'T, IEnumerable>> * length:int -> Descriptor<'T>
  member WithString : property:Expression<Func<'T, string>> * splitType:StringSplitType * ?separator:string * ?asEnum:bool * ?exclusions:string -> Descriptor<'T>

Full name: numl.Model.Descriptor<_>

--------------------
Descriptor() : unit

--------------------
Descriptor() : unit
Descriptor.Create<'T (requires reference type)>() : Descriptor
Descriptor.Create(t: System.Type) : Descriptor
val generator : DecisionTreeGenerator

Full name: Script.generator
Multiple items
type DecisionTreeGenerator =
  inherit Generator
  new : descriptor:Descriptor -> DecisionTreeGenerator + 1 overload
  member Depth : int with get, set
  member Generate : x:Matrix * y:Vector -> IModel
  member Hint : float with get, set
  member Impurity : Impurity
  member ImpurityType : Type with get, set
  member SetHint : o:obj -> unit
  member Width : int with get, set

Full name: numl.Supervised.DecisionTree.DecisionTreeGenerator

--------------------
DecisionTreeGenerator(descriptor: Descriptor) : unit
DecisionTreeGenerator(?depth: int, ?width: int, ?descriptor: Descriptor, ?impurityType: System.Type, ?hint: float) : unit
DecisionTreeGenerator.SetHint(o: obj) : unit
val model : LearningModel

Full name: Script.model
type Learner =
  static member Best : models:IEnumerable<LearningModel> -> LearningModel
  static member Learn : examples:IEnumerable<obj> * trainingPercentage:float * repeat:int * [<ParamArray>] generators:IGenerator[] -> LearningModel[] + 3 overloads

Full name: numl.Learner
Learner.Learn<'T>(examples: System.Collections.Generic.IEnumerable<'T>, trainingPercentage: float, repeat: int, generator: Supervised.IGenerator) : LearningModel
Learner.Learn(examples: System.Collections.Generic.IEnumerable<obj>, trainingPercentage: float, repeat: int, generator: Supervised.IGenerator) : LearningModel
Learner.Learn(examples: System.Data.DataTable, trainingPercentage: float, repeat: int, generator: Supervised.IGenerator) : LearningModel
Learner.Learn(examples: System.Collections.Generic.IEnumerable<obj>, trainingPercentage: float, repeat: int, [<System.ParamArray>] generators: Supervised.IGenerator []) : LearningModel []
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn

More information

Link:http://fssnip.net/lz
Posted:3 years ago
Author:Taha Hachana
Tags: numl , machine , learning