|
| 1 | +// Learn more about F# at http://fsharp.org |
| 2 | + |
| 3 | +open System |
| 4 | +open System.IO |
| 5 | +open Microsoft.ML |
| 6 | +open Microsoft.ML.Data |
| 7 | +open Microsoft.ML.Core.Data |
| 8 | + |
| 9 | +[<CLIMutable>] |
| 10 | +type HeartData = |
| 11 | + { |
| 12 | + Age : float32 |
| 13 | + Sex : float32 |
| 14 | + Cp : float32 |
| 15 | + TrestBps : float32 |
| 16 | + Chol : float32 |
| 17 | + Fbs : float32 |
| 18 | + RestEcg : float32 |
| 19 | + Thalac : float32 |
| 20 | + Exang : float32 |
| 21 | + OldPeak : float32 |
| 22 | + Slope : float32 |
| 23 | + Ca : float32 |
| 24 | + Thal : float32 |
| 25 | + } |
| 26 | + |
| 27 | +let heartSampleData = |
| 28 | + [ |
| 29 | + {Age=36.f; Sex=1.f; Cp=4.f; TrestBps=135.f; Chol=321.f; Fbs=1.f; RestEcg=0.f; Thalac=158.f; Exang=0.f; OldPeak=1.3f; Slope=0.f; Ca=0.f; Thal=3.f} |
| 30 | + {Age=95.f; Sex=1.f; Cp=4.f; TrestBps=135.f; Chol=321.f; Fbs=1.f; RestEcg=0.f; Thalac=158.f; Exang=0.f; OldPeak=1.3f; Slope=0.f; Ca=0.f; Thal=3.f} |
| 31 | + {Age=45.f; Sex=0.f; Cp=1.f; TrestBps=140.f; Chol=221.f; Fbs=1.f; RestEcg=1.f; Thalac=150.f; Exang=0.f; OldPeak=2.3f; Slope=3.f; Ca=0.f; Thal=6.f} |
| 32 | + {Age=45.f; Sex=0.f; Cp=1.f; TrestBps=140.f; Chol=221.f; Fbs=1.f; RestEcg=1.f; Thalac=150.f; Exang=0.f; OldPeak=2.3f; Slope=3.f; Ca=0.f; Thal=6.f} |
| 33 | + {Age=88.f; Sex=0.f; Cp=1.f; TrestBps=140.f; Chol=221.f; Fbs=1.f; RestEcg=1.f; Thalac=150.f; Exang=0.f; OldPeak=2.3f; Slope=3.f; Ca=0.f; Thal=6.f} |
| 34 | + ] |
| 35 | + |
| 36 | +[<CLIMutable>] |
| 37 | +type HeartDataImport = |
| 38 | + { |
| 39 | + [<LoadColumn(0)>] Age : float32 |
| 40 | + [<LoadColumn(1)>] Sex : float32 |
| 41 | + [<LoadColumn(2)>] Cp : float32 |
| 42 | + [<LoadColumn(3)>] TrestBps : float32 |
| 43 | + [<LoadColumn(4)>] Chol : float32 |
| 44 | + [<LoadColumn(5)>] Fbs : float32 |
| 45 | + [<LoadColumn(6)>] RestEcg : float32 |
| 46 | + [<LoadColumn(7)>] Thalac : float32 |
| 47 | + [<LoadColumn(8)>] Exang : float32 |
| 48 | + [<LoadColumn(9)>] OldPeak : float32 |
| 49 | + [<LoadColumn(10)>] Slope : float32 |
| 50 | + [<LoadColumn(11)>] Ca : float32 |
| 51 | + [<LoadColumn(12)>] Thal : float32 |
| 52 | + [<LoadColumn(13)>] Label : float32 |
| 53 | + } |
| 54 | + |
| 55 | +[<CLIMutable>] |
| 56 | +type HeartPrediction = { Score : float32 [] } |
| 57 | + |
| 58 | +let appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs().[0]) |
| 59 | + |
| 60 | +let baseDatasetsLocation = @"../../../../Data" |
| 61 | +let trainDataPath = sprintf @"%s/HeartTraining.csv" baseDatasetsLocation |
| 62 | +let testDataPath = sprintf @"%s/HeartTest.csv" baseDatasetsLocation |
| 63 | + |
| 64 | +let baseModelsPath = @"../../../../MLModels"; |
| 65 | +let modelPath = sprintf @"%s/HeartClassification.zip" baseModelsPath |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +let read (dataPath : string) (dataLoader : TextLoader) = |
| 70 | + dataLoader.Read dataPath |
| 71 | + |
| 72 | +let downcastPipeline (x : IEstimator<_>) = |
| 73 | + match x with |
| 74 | + | :? IEstimator<ITransformer> as y -> y |
| 75 | + | _ -> failwith "downcastPipeline: expecting a IEstimator<ITransformer>" |
| 76 | + |
| 77 | + |
| 78 | +let buildTrainEvaluateAndSaveModel (mlContext : MLContext) = |
| 79 | + // STEP 1: Common data loading configuration |
| 80 | + let trainingDataView = mlContext.Data.ReadFromTextFile<HeartDataImport>(trainDataPath, hasHeader = true, separatorChar = ',') |
| 81 | + let testDataView = mlContext.Data.ReadFromTextFile<HeartDataImport>(testDataPath, hasHeader = true, separatorChar = ',') |
| 82 | + |
| 83 | + // STEP 2: Common data process configuration with pipeline data transformations |
| 84 | + let dataProcessPipeline = |
| 85 | + EstimatorChain() |
| 86 | + .Append(mlContext.Transforms.Concatenate |
| 87 | + (DefaultColumnNames.Features, "Age", "Sex", |
| 88 | + "Cp", "TrestBps", "Chol", "Fbs", "RestEcg", "Thalac", |
| 89 | + "Exang", "OldPeak", "Slope", "Ca", "Thal")) |
| 90 | + .AppendCacheCheckpoint(mlContext) |
| 91 | + |> downcastPipeline |
| 92 | + |
| 93 | + // (OPTIONAL) Peek data (such as 5 records) in training DataView after applying the ProcessPipeline's transformations into "Features" |
| 94 | + Common.ConsoleHelper.peekDataViewInConsole<HeartData> mlContext trainingDataView dataProcessPipeline 5 |> ignore |
| 95 | + Common.ConsoleHelper.peekVectorColumnDataInConsole mlContext DefaultColumnNames.Features trainingDataView dataProcessPipeline 5 |> ignore |
| 96 | + |
| 97 | + let trainer = mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumn = DefaultColumnNames.Label, featureColumn = DefaultColumnNames.Features) |
| 98 | + let trainingPipeline = dataProcessPipeline.Append(trainer) |
| 99 | + |
| 100 | + printfn "=============== Training the model ===============" |
| 101 | + let trainedModel = trainingPipeline.Fit(trainingDataView) |
| 102 | + printfn "=============== Finish the train model.===============" |
| 103 | + |
| 104 | + printfn "===== Evaluating Model's accuracy with Test data =====" |
| 105 | + let predictions = trainedModel.Transform testDataView |
| 106 | + let metrics = |
| 107 | + mlContext.MulticlassClassification.Evaluate |
| 108 | + (data = predictions, |
| 109 | + label = DefaultColumnNames.Label, |
| 110 | + score = DefaultColumnNames.Score, |
| 111 | + predictedLabel = DefaultColumnNames.PredictedLabel, |
| 112 | + topK = 0) |
| 113 | + |
| 114 | + Common.ConsoleHelper.printMultiClassClassificationMetrics (trainer.ToString()) metrics |
| 115 | + |
| 116 | + printfn "=============== Saving the model to a file ===============" |
| 117 | + use fs = new FileStream(modelPath, FileMode.Create, FileAccess.Write, FileShare.Write) |
| 118 | + mlContext.Model.Save(trainedModel, fs) |
| 119 | + |
| 120 | + printfn "=============== Model Saved ============= " |
| 121 | + |
| 122 | + |
| 123 | +let testPrediction (mlContext : MLContext) = |
| 124 | + let trainedModel = |
| 125 | + use stream = new FileStream(modelPath, FileMode.Open, FileAccess.Read, FileShare.Read) |
| 126 | + mlContext.Model.Load(stream) |
| 127 | + let predEngine = trainedModel.CreatePredictionEngine<HeartData, HeartPrediction>(mlContext) |
| 128 | + |
| 129 | + heartSampleData |
| 130 | + |> List.iter |
| 131 | + (fun x -> |
| 132 | + predEngine.Predict(x).Score |
| 133 | + |> Seq.iteri (fun i s -> printfn " %d: %0.3f" i s) |
| 134 | + printfn "" |
| 135 | + ) |
| 136 | + |
| 137 | +[<EntryPoint>] |
| 138 | +let main argv = |
| 139 | + let mlContext = MLContext() |
| 140 | + buildTrainEvaluateAndSaveModel mlContext |
| 141 | + |
| 142 | + testPrediction mlContext |
| 143 | + printfn "=============== End of process, hit any key to finish ===============" |
| 144 | + Console.ReadKey() |> ignore |
| 145 | + |
| 146 | + 0 // return an integer exit code |
0 commit comments