-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.fs
74 lines (57 loc) · 2.49 KB
/
Program.fs
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
71
72
73
74
open System
open Microsoft.ML
open Microsoft.ML.Data
/// A type that holds a single iris flower.
[<CLIMutable>]
type IrisData = {
[<LoadColumn(0)>] SepalLength : float32
[<LoadColumn(1)>] SepalWidth : float32
[<LoadColumn(2)>] PetalLength : float32
[<LoadColumn(3)>] PetalWidth : float32
[<LoadColumn(4)>] Label : string
}
/// A type that holds a single model prediction.
[<CLIMutable>]
type IrisPrediction = {
PredictedLabel : uint32
Score : float32[]
}
/// file paths to data files (assumes os = windows!)
let dataPath = sprintf "%s\\iris-data.csv" Environment.CurrentDirectory
[<EntryPoint>]
let main argv =
// get the machine learning context
let context = new MLContext();
// read the iris flower data from a text file
let data = context.Data.LoadFromTextFile<IrisData>(dataPath, hasHeader = false, separatorChar = ',')
// split the data into a training and testing partition
let partitions = context.Data.TrainTestSplit(data, testFraction = 0.2)
// set up a learning pipeline
let pipeline =
EstimatorChain()
// step 1: concatenate features into a single column
.Append(context.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
// step 2: use k-means clustering to find the iris types
.Append(context.Clustering.Trainers.KMeans(numberOfClusters = 3))
// train the model on the training data
let model = partitions.TrainSet |> pipeline.Fit
// get predictions and compare to ground truth
let metrics = partitions.TestSet |> model.Transform |> context.Clustering.Evaluate
// show results
printfn "Nodel results"
printfn " Average distance: %f" metrics.AverageDistance
printfn " Davies Bouldin index: %f" metrics.DaviesBouldinIndex
// set up a prediction engine
let engine = context.Model.CreatePredictionEngine model
// grab 3 flowers from the dataset
let flowers = context.Data.CreateEnumerable<IrisData>(partitions.TestSet, reuseRowObject = false) |> Array.ofSeq
let testFlowers = [ flowers.[0]; flowers.[10]; flowers.[20] ]
// show predictions for the three flowers
printfn "Predictions for the 3 test flowers:"
printfn " Label\t\t\tPredicted\tScores"
testFlowers |> Seq.iter(fun f ->
let p = engine.Predict f
printf " %-15s\t%i\t\t" f.Label p.PredictedLabel
p.Score |> Seq.iter(fun s -> printf "%f\t" s)
printfn "")
0 // return value