Skip to content

Instantly share code, notes, and snippets.

@JeroMiya
Created February 21, 2019 19:26
Show Gist options
  • Save JeroMiya/6510e7733640ad7653bd21b5591ab6e6 to your computer and use it in GitHub Desktop.
Save JeroMiya/6510e7733640ad7653bd21b5591ab6e6 to your computer and use it in GitHub Desktop.
open System
open System.ComponentModel.DataAnnotations.Schema
open Microsoft.ML.Data
open Microsoft.ML
open Microsoft.Data.DataView
type IrisData() =
[<LoadColumn(0);DefaultValue>]
val mutable public SepalLength: float32
[<LoadColumn(1);DefaultValue>]
val mutable public SepalWidth: float32
[<LoadColumn(2);DefaultValue>]
val mutable public PetalLength: float32
[<LoadColumn(3);DefaultValue>]
val mutable public PetalWidth: float32
[<LoadColumn(4);DefaultValue>]
val mutable public Label: string
type IrisPrediction() =
[<ColumnName("PredictedLabel");DefaultValue>]
val mutable public PredictedLabel: string
[<EntryPoint>]
let main argv =
let dataPath = "./iris-data.txt"
// STEP 2: Create a ML.NET environment
let mlContext = new MLContext()
// If working in Visual Studio, make sure to 'Copy to Output Directory'
// property of iris-data.txt is set to 'Copy always'
let trainingDataView: IDataView = mlContext.Data.ReadFromTextFile<IrisData>(path = dataPath, hasHeader = false, separatorChar = ',')
// STEP 3: Transform your data and add a learner
// Assign numeric values to text in the "Label" column, because only
// numbers can be processed during model training.
// Add a learning algorithm to the pipeline. e.g. (What type of iris is this?)
// Convert the Label back into original text (after converting to number in step 3)
let transforms = mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
let pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
// ERROR on this line: Type constraint mismatch. Type type 'Transforms.Conversions.ValueToKeyMappingEstimator' is not compatible with type 'Core.Data.IEstimator<Core.Data.ITransformer>'
.Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
.AppendCacheCheckpoint(mlContext)
.Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumn = "Label", featureColumn = "Features"))
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"))
// STEP 4
let model = pipeline.Fit(trainingDataView)
// STEP 5: Use your model to make a prediction
// You can change these numbers to test different predictions
let sampleData = new IrisData(SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f)
let prediction = model.CreatePredictionEngine<IrisData, IrisPrediction>(mlContext).Predict(sampleData)
Console.WriteLine("Predicted flower type is: " + prediction.PredictedLabel)
Console.WriteLine("Press any key to exit...")
Console.ReadLine()
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment