-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathConsoleHelper.fs
More file actions
214 lines (177 loc) · 11.4 KB
/
ConsoleHelper.fs
File metadata and controls
214 lines (177 loc) · 11.4 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
namespace Common
module ConsoleHelper =
open System
open Microsoft.ML
open Microsoft.ML.Data
//open Microsoft.ML.Api
open System.Reflection
let printPrediction prediction =
printfn "*************************************************"
printfn "Predicted : %s" prediction
printfn "*************************************************"
let printRegressionPredictionVersusObserved predictionCount observedCount =
printfn "-------------------------------------------------"
printfn "Predicted : %.4f" predictionCount
printfn "Actual: %s" observedCount
printfn "-------------------------------------------------"
let printRegressionMetrics name (metrics : RegressionMetrics) =
printfn "*************************************************"
printfn "* Metrics for %s regression model " name
printfn "*------------------------------------------------"
printfn "* LossFn: %.2f" metrics.LossFunction
printfn "* R2 Score: %.2f" metrics.RSquared
printfn "* Absolute loss: %.2f" metrics.MeanAbsoluteError
printfn "* Squared loss: %.2f" metrics.MeanSquaredError
printfn "* RMS loss: %.2f" metrics.RootMeanSquaredError
printfn "*************************************************"
let printBinaryClassificationMetrics name (metrics : CalibratedBinaryClassificationMetrics) =
printfn"************************************************************"
printfn"* Metrics for %s binary classification model " name
printfn"*-----------------------------------------------------------"
printfn"* Accuracy: %.2f%%" (metrics.Accuracy * 100.)
printfn"* Area Under Curve: %.2f%%" (metrics.AreaUnderRocCurve * 100.)
printfn"* Area under Precision recall Curve: %.2f%%" (metrics.AreaUnderPrecisionRecallCurve * 100.)
printfn"* F1Score: %.2f%%" (metrics.F1Score * 100.)
printfn"* LogLogg: %.2f%%" (metrics.LogLoss)
printfn"* LogLossreduction: %.2f%%" (metrics.LogLossReduction)
printfn"* PositivePrecision: %.2f" (metrics.PositivePrecision)
printfn"* PositiveRecall: %.2f" (metrics.PositiveRecall)
printfn"* NegativePrecision: %.2f" (metrics.NegativePrecision)
printfn"* NegativeRecall: %.2f" (metrics.NegativeRecall)
printfn"************************************************************"
let printMultiClassClassificationMetrics name (metrics : MulticlassClassificationMetrics) =
printfn "************************************************************"
printfn "* Metrics for %s multi-class classification model " name
printfn "*-----------------------------------------------------------"
printfn " AccuracyMacro = %.4f, a value between 0 and 1, the closer to 1, the better" metrics.MacroAccuracy
printfn " AccuracyMicro = %.4f, a value between 0 and 1, the closer to 1, the better" metrics.MicroAccuracy
printfn " LogLoss = %.4f, the closer to 0, the better" metrics.LogLoss
printfn " LogLoss for class 1 = %.4f, the closer to 0, the better" metrics.PerClassLogLoss.[0]
printfn " LogLoss for class 2 = %.4f, the closer to 0, the better" metrics.PerClassLogLoss.[1]
printfn " LogLoss for class 3 = %.4f, the closer to 0, the better" metrics.PerClassLogLoss.[2]
printfn "************************************************************"
let private calculateStandardDeviation (values : float array) =
let average = values |> Array.average
let sumOfSquaresOfDifferences = values |> Array.map(fun v -> (v - average) * (v - average)) |> Array.sum
let standardDeviation = Math.Sqrt(sumOfSquaresOfDifferences / float (values.Length-1))
standardDeviation;
let calculateConfidenceInterval95 (values : float array) =
let confidenceInterval95 = 1.96 * calculateStandardDeviation(values) / Math.Sqrt(float (values.Length-1));
confidenceInterval95
let printMulticlassClassificationFoldsAverageMetrics algorithmName (crossValResults : TrainCatalogBase.CrossValidationResult<MulticlassClassificationMetrics>[]) =
let metricsInMultipleFolds = crossValResults |> Array.map(fun r -> r.Metrics)
let microAccuracyValues = metricsInMultipleFolds |> Array.map(fun m -> m.MicroAccuracy)
let microAccuracyAverage = microAccuracyValues |> Array.average
let microAccuraciesStdDeviation = calculateStandardDeviation microAccuracyValues
let microAccuraciesConfidenceInterval95 = calculateConfidenceInterval95 microAccuracyValues
let macroAccuracyValues = metricsInMultipleFolds |> Array.map(fun m -> m.MicroAccuracy)
let macroAccuracyAverage = macroAccuracyValues |> Array.average
let macroAccuraciesStdDeviation = calculateStandardDeviation macroAccuracyValues
let macroAccuraciesConfidenceInterval95 = calculateConfidenceInterval95 macroAccuracyValues
let logLossValues = metricsInMultipleFolds |> Array.map (fun m -> m.LogLoss)
let logLossAverage = logLossValues |> Array.average
let logLossStdDeviation = calculateStandardDeviation logLossValues
let logLossConfidenceInterval95 = calculateConfidenceInterval95 logLossValues
let logLossReductionValues = metricsInMultipleFolds |> Array.map (fun m -> m.LogLossReduction)
let logLossReductionAverage = logLossReductionValues |> Array.average
let logLossReductionStdDeviation = calculateStandardDeviation logLossReductionValues
let logLossReductionConfidenceInterval95 = calculateConfidenceInterval95 logLossReductionValues
printfn "*************************************************************************************************************"
printfn "* Metrics for %s Multi-class Classification model " algorithmName
printfn "*------------------------------------------------------------------------------------------------------------"
printfn "* Average MicroAccuracy: %.3f - Standard deviation: (%.3f) - Confidence Interval 95%%: (%.3f)" microAccuracyAverage microAccuraciesStdDeviation microAccuraciesConfidenceInterval95
printfn "* Average MacroAccuracy: %.3f - Standard deviation: (%.3f) - Confidence Interval 95%%: (%.3f)" macroAccuracyAverage macroAccuraciesStdDeviation macroAccuraciesConfidenceInterval95
printfn "* Average LogLoss: %.3f - Standard deviation: (%.3f) - Confidence Interval 95%%: (%.3f)" logLossAverage logLossStdDeviation logLossConfidenceInterval95
printfn "* Average LogLossReduction: %.3f - Standard deviation: (%.3f) - Confidence Interval 95%%: (%.3f)" logLossReductionAverage logLossReductionStdDeviation logLossReductionConfidenceInterval95
printfn "*************************************************************************************************************"
let printClusteringMetrics name (metrics : ClusteringMetrics) =
printfn "*************************************************"
printfn "* Metrics for %s clustering model " name
printfn "*------------------------------------------------"
printfn "* Average Distance: %.15f" metrics.AverageDistance
printfn "* Davies Bouldin Index is: %.15f" metrics.DaviesBouldinIndex
printfn "*************************************************"
let consoleWriteHeader line =
let defaultColor = Console.ForegroundColor
Console.ForegroundColor <- ConsoleColor.Yellow
printfn " "
printfn "%s" line
let maxLength = line.Length
printfn "%s" (new string('#', maxLength))
Console.ForegroundColor <- defaultColor
let downcastPipeline (pipeline : IEstimator<'a>) =
match pipeline with
| :? IEstimator<ITransformer> as p -> p
| _ -> failwith "The pipeline has to be an instance of IEstimator<ITransformer>."
let peekDataViewInConsole<'TObservation when 'TObservation : (new : unit -> 'TObservation) and 'TObservation : not struct> (mlContext : MLContext) (dataView : IDataView) (pipeline : IEstimator<ITransformer>) numberOfRows =
let msg = sprintf "Peek data in DataView: Showing %d rows with the columns" numberOfRows
consoleWriteHeader msg
//https://github.com/dotnet/machinelearning/blob/main/docs/code/MlNetCookBook.md#how-do-i-look-at-the-intermediate-data
let transformer = pipeline.Fit dataView
let transformedData = transformer.Transform dataView
// 'transformedData' is a 'promise' of data, lazy-loading. call Preview
//and iterate through the returned collection from preview.
transformedData.Preview(numberOfRows).RowView
|> Seq.iter
(fun row ->
row.Values
|> Array.map (function KeyValue(k,v) -> sprintf "| %s:%O" k v)
|> Array.fold (+) "Row--> "
|> printfn "%s\n"
)
let peekVectorColumnDataInConsole (mlContext : MLContext) columnName (dataView : IDataView) (pipeline : IEstimator<ITransformer>) numberOfRows =
let msg = sprintf "Peek data in DataView: : Show %d rows with just the '%s' column" numberOfRows columnName
consoleWriteHeader msg
let transformer = pipeline.Fit dataView
let transformedData = transformer.Transform dataView
// Extract the 'Features' column.
let someColumnData =
transformedData.GetColumn<float32[]>(columnName)
|> Seq.take numberOfRows
|> Seq.toList
// print to console the peeked rows
someColumnData
|> List.iter(fun row ->
let concatColumn =
row
|> Array.map string
|> String.concat ""
printfn "%s" concatColumn
)
someColumnData;
let consoleWriterSection (lines : string array) =
let defaultColor = Console.ForegroundColor
Console.ForegroundColor <- ConsoleColor.Blue
printfn " "
lines
|> Array.iter (printfn "%s")
let maxLength = lines |> Array.map(fun x -> x.Length) |> Array.max
printfn "%s" (new string('-', maxLength))
Console.ForegroundColor <- defaultColor
let consolePressAnyKey () =
let defaultColor = Console.ForegroundColor
Console.ForegroundColor <- ConsoleColor.Green
printfn " "
printfn "Press any key to finish."
Console.ForegroundColor <- defaultColor
Console.ReadKey() |> ignore
let consoleWriteException (lines : string array) =
let defaultColor = Console.ForegroundColor
Console.ForegroundColor <- ConsoleColor.Red
let exceptionTitle = "EXCEPTION"
printfn " "
printfn "%s" exceptionTitle
printfn "%s" (new string('#', exceptionTitle.Length))
Console.ForegroundColor <- defaultColor
lines
|> Array.iter (printfn "%s")
let consoleWriteWarning (lines : string array) =
let defaultColor = Console.ForegroundColor
Console.ForegroundColor <- ConsoleColor.DarkMagenta
let warningTitle = "WARNING"
printfn " "
printfn "%s" warningTitle
printfn "%s" (new string('#', warningTitle.Length))
Console.ForegroundColor <- defaultColor
lines
|> Array.iter (printfn "%s")