Skip to content

Instantly share code, notes, and snippets.

@voronoipotato
Created May 31, 2018 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voronoipotato/0b3b2840494a11120c52acc37e2e05c8 to your computer and use it in GitHub Desktop.
Save voronoipotato/0b3b2840494a11120c52acc37e2e05c8 to your computer and use it in GitHub Desktop.
// Learn more about F# at http://fsharp.org
open Microsoft.ML
open Microsoft.ML.Runtime.Api
open Microsoft.ML.Trainers
open Microsoft.ML.Transforms
open System
//The first four properties are inputs, the label is what you are predicting which is set in training
type IrisData() =
[<Column("0")>] [<DefaultValue>]
val mutable public SepalLength: float32
[<Column("1")>] [<DefaultValue>]
val mutable public SepalWidth: float32
[<Column("2")>] [<DefaultValue>]
val mutable public PetalLength:float32
[<Column("3")>] [<DefaultValue>]
val mutable public PetalWidth:float32
[<Column("4")>] [<ColumnName("Label")>] [<DefaultValue>]
val mutable public Label:string
//Result returned from the prediction operation
type IrisPrediction() =
[<ColumnName("PredictedLabel")>] [<DefaultValue>]
val mutable public PredictedLabel : string
[<EntryPoint>]
let main argv =
//Create a pipeline
let pipeline = new LearningPipeline()
let dataPath = "iris-data.txt"
//Loads CSV data
pipeline.Add(new TextLoader<IrisData>(dataPath,separator = ","))
//Transforms the label into an "enum" because only numbers can be processed during training
pipeline.Add(new Transforms.Dictionarizer("Label"))
//Transforms all features into one vector
pipeline.Add(new Transforms.ColumnConcatenator("Features","SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
//applies a learning algorithm
pipeline.Add(new Trainers.StochasticDualCoordinateAscentClassifier())
//Transforms the label back into the original text
pipeline.Add(new Transforms.PredictedLabelColumnOriginalValueConverter(PredictedLabelColumn = "PredictedLabel"))
//Train the model
let model = pipeline.Train<IrisData, IrisPrediction>()
//Initialize the data we hope to test
let x = IrisData()
x.SepalLength <- 3.3f
x.SepalWidth <- 1.6f
x.PetalLength <- 0.2f
x.PetalWidth <- 5.1f
//predict the label from the trained model
let prediction = model.Predict(x)
printfn "Predicted flower type is: %s" prediction.PredictedLabel
Console.ReadLine () |> ignore
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment