2 people like it.

Minimal AutoML Binary Classification Sample

Shows how to use ML.Net AutoML capability for binary classification. Now with progress reporting

 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: 
#load "SetupML.fsx" //get it from here: http://fssnip.net/7Wi

open System.Threading
open Microsoft.ML
open Microsoft.ML.AutoML

let ctx = MLContext()

//data from here: https://www.kaggle.com/mlg-ulb/creditcardfraud
let trainFile = @"C:\Users\fwaris\Downloads\creditcardfraud\creditcard.csv"
let labelCol = "Class"

let colSelection = ctx.Auto().InferColumns(trainFile, labelCol)

let textLoader = ctx.Data.CreateTextLoader(colSelection.TextLoaderOptions)

let trainView = textLoader.Load(trainFile)

let cts = new CancellationTokenSource()

let expSettings = BinaryExperimentSettings()
expSettings.OptimizingMetric <- BinaryClassificationMetric.AreaUnderRocCurve
expSettings.CancellationToken <- cts.Token

let experiment = ctx.Auto().CreateBinaryClassificationExperiment(expSettings)

let progressReport =
    {new System.IProgress<RunDetail<Data.BinaryClassificationMetrics>> with 
        member x.Report v =
            let auc = v.ValidationMetrics.AreaUnderRocCurve 
            printfn "%s %f" v.TrainerName auc
    }

let results = ref None                                                                                      
async { 
    let r = experiment.Execute(trainView, colSelection.ColumnInformation, progressHandler=progressReport)
    results := Some r
    }
    |> Async.Start

(*
cts.Cancel()      // cancel to get results (can still take a while)



results.Value.Value.RunDetails
|> Seq.sortByDescending (fun x->x.ValidationMetrics.AreaUnderRocCurve)
|> Seq.iter (fun dtl -> printfn "%s %f" dtl.TrainerName dtl.ValidationMetrics.AreaUnderRocCurve)

*)
namespace System
namespace System.Threading
namespace Microsoft
val ctx : obj

Full name: Script.ctx
val trainFile : string

Full name: Script.trainFile
val labelCol : string

Full name: Script.labelCol
val colSelection : obj

Full name: Script.colSelection
val textLoader : obj

Full name: Script.textLoader
namespace Microsoft.FSharp.Data
val trainView : obj

Full name: Script.trainView
val cts : CancellationTokenSource

Full name: Script.cts
Multiple items
type CancellationTokenSource =
  new : unit -> CancellationTokenSource
  member Cancel : unit -> unit + 1 overload
  member Dispose : unit -> unit
  member IsCancellationRequested : bool
  member Token : CancellationToken
  static member CreateLinkedTokenSource : [<ParamArray>] tokens:CancellationToken[] -> CancellationTokenSource + 1 overload

Full name: System.Threading.CancellationTokenSource

--------------------
CancellationTokenSource() : unit
val expSettings : obj

Full name: Script.expSettings
Multiple items
type CancellationToken =
  struct
    new : canceled:bool -> CancellationToken
    member CanBeCanceled : bool
    member Equals : other:CancellationToken -> bool + 1 overload
    member GetHashCode : unit -> int
    member IsCancellationRequested : bool
    member Register : callback:Action -> CancellationTokenRegistration + 3 overloads
    member ThrowIfCancellationRequested : unit -> unit
    member WaitHandle : WaitHandle
    static member None : CancellationToken
  end

Full name: System.Threading.CancellationToken

--------------------
CancellationToken()
CancellationToken(canceled: bool) : unit
property CancellationTokenSource.Token: CancellationToken
val experiment : obj

Full name: Script.experiment
val progressReport : obj

Full name: Script.progressReport
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val results : obj option ref

Full name: Script.results
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
union case Option.None: Option<'T>
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val r : obj
union case Option.Some: Value: 'T -> Option<'T>
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task -> Async<unit>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken

Full name: Microsoft.FSharp.Control.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
static member Async.Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit

More information

Link:http://fssnip.net/7Ws
Posted:5 years ago
Author:Faisal Waris
Tags: machine learning , ml.net , automl