Skip to content

Commit 1011711

Browse files
kevmalCESARDELATORRE
authored andcommitted
migrate F# samples to 0.10 (dotnet#244)
* migrate F# samples to 0.10 * post merge updates
1 parent 17b7a4a commit 1011711

File tree

35 files changed

+125
-105
lines changed

35 files changed

+125
-105
lines changed

samples/fsharp/common_v0.9/ConsoleHelper.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module ConsoleHelper =
44
open System
55
open Microsoft.ML
66
open Microsoft.ML.Data
7-
open Microsoft.ML.Data
87
open Microsoft.ML.Core.Data
98
//open Microsoft.ML.Api
109
open System.Reflection
10+
open Microsoft.Data.DataView
1111

1212
let printPrediction prediction =
1313
printfn "*************************************************"
@@ -139,7 +139,7 @@ module ConsoleHelper =
139139
// 'transformedData' is a 'promise' of data, lazy-loading. Let's actually read it.
140140
// Convert to an enumerable of user-defined type.
141141
let someRows =
142-
transformedData.AsEnumerable<'TObservation>(mlContext, reuseRowObject = false)
142+
mlContext.CreateEnumerable<'TObservation>(transformedData, reuseRowObject = false)
143143
// Take the specified number of rows
144144
|> Seq.take numberOfRows
145145
// Convert to List

samples/fsharp/common_v0.9/ModelBuilder.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open Microsoft.ML.Transforms
77
open Microsoft.ML.Data
88

99
module ModelBuilder =
10+
open Microsoft.Data.DataView
1011

1112
let create (mlContext : MLContext) (pipeline : IEstimator<ITransformer>) =
1213
(mlContext, pipeline)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.ML" Version="0.9.0" />
13+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
1414
</ItemGroup>
1515

1616
</Project>

samples/fsharp/getting-started/CreditCardFraudDetection/CreditCardFraudDetection/Program.fs renamed to samples/fsharp/getting-started/BinaryClassification_CreditCardFraudDetection/CreditCardFraudDetection/Program.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ let main _ =
123123
let loaderArgs = TextLoader.Arguments()
124124
loaderArgs.Column <- columns
125125
loaderArgs.HasHeader <- true
126-
loaderArgs.Separator <- ","
126+
loaderArgs.Separators <- [| ',' |]
127127

128128
let reader = TextLoader (mlContext, loaderArgs)
129129

130-
let classification = BinaryClassificationContext mlContext
130+
let classification = BinaryClassificationCatalog mlContext
131131

132132
(*
133133
Split the data 80:20 into train and test files,
@@ -207,15 +207,15 @@ let main _ =
207207
trainFile,
208208
columnsPlus,
209209
loaderArgs.HasHeader,
210-
loaderArgs.Separator.ToCharArray().[0]
210+
loaderArgs.Separators.[0]
211211
)
212212

213213
let testData =
214214
mlContext.Data.ReadFromTextFile(
215215
testFile,
216216
columnsPlus,
217217
loaderArgs.HasHeader,
218-
loaderArgs.Separator.ToCharArray().[0]
218+
loaderArgs.Separators.[0]
219219
)
220220

221221
trainData, testData
@@ -237,8 +237,8 @@ let main _ =
237237
|> fun x ->
238238
x.Append (
239239
mlContext.Transforms.Normalize (
240-
"Features",
241240
"FeaturesNormalizedByMeanVar",
241+
"Features",
242242
NormalizingEstimator.NormalizerMode.MeanVariance
243243
)
244244
)
@@ -279,7 +279,7 @@ let main _ =
279279
let testData = mlContext.Data.ReadFromTextFile (testFile, columnsPlus, hasHeader = true, separatorChar = ',')
280280

281281
printfn "Making predictions"
282-
testData.AsEnumerable<TransactionObservation>(mlContext, reuseRowObject = false)
282+
mlContext.CreateEnumerable<TransactionObservation>(testData, reuseRowObject = false)
283283
|> Seq.filter (fun x -> x.Label = true)
284284
// use 5 observations from the test data
285285
|> Seq.take 5

samples/fsharp/getting-started/BinaryClassification_SentimentAnalysis/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
| ML.NET version | API type | Status | App Type | Data type | Scenario | ML Task | Algorithms |
44
|----------------|-------------------|-------------------------------|-------------|-----------|---------------------|---------------------------|-----------------------------|
5-
| v0.9 | Dynamic API | README.md updated | Console app | .tsv files | Sentiment Analysis | Two-class classification | Linear Classification |
5+
| v0.10 | Dynamic API | README.md updated | Console app | .tsv files | Sentiment Analysis | Two-class classification | Linear Classification |
66

77
------------------------------------
88

@@ -45,7 +45,7 @@ The initial code is similar to the following:
4545
```fsharp
4646
// STEP 1: Common data loading configuration
4747
let textLoader =
48-
mlContext.Data.CreateTextReader (
48+
mlContext.Data.CreateTextLoader(
4949
columns =
5050
[|
5151
TextLoader.Column("Label", Nullable DataKind.Bool, 0)
@@ -60,7 +60,7 @@ The initial code is similar to the following:
6060
6161
6262
// STEP 2: Common data process configuration with pipeline data transformations
63-
let dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("Text", "Features")
63+
let dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("Features", "Text")
6464
6565
// STEP 3: Set the training algorithm, then create and config the modelBuilder
6666
let trainer = mlContext.BinaryClassification.Trainers.FastTree(labelColumn = "Label", featureColumn = "Features")

samples/fsharp/getting-started/BinaryClassification_SentimentAnalysis/SentimentAnalysis/SentimentAnalysisConsoleApp/Program.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let buildTrainEvaluateAndSaveModel (mlContext : MLContext) =
2727
let testDataView = mlContext.Data.ReadFromTextFile<SentimentIssue>(testDataPath, hasHeader = true)
2828

2929
// STEP 2: Common data process configuration with pipeline data transformations
30-
let dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("Text", "Features")
30+
let dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("Features", "Text")
3131

3232
// (OPTIONAL) Peek data (such as 2 records) in training DataView after applying the ProcessPipeline's transformations into "Features"
3333
Common.ConsoleHelper.peekDataViewInConsole<SentimentIssue> mlContext trainingDataView dataProcessPipeline 2 |> ignore

samples/fsharp/getting-started/BinaryClassification_SentimentAnalysis/SentimentAnalysis/SentimentAnalysisConsoleApp/SentimentAnalysisConsoleApp.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Microsoft.ML" Version="$(MicrosoftMLVersion)" />
21+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

samples/fsharp/getting-started/BinaryClassification_SpamDetection/SpamDetectionConsoleApp/Program.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let main _argv =
4747
// Set up the MLContext, which is a catalog of components in ML.NET.
4848
let mlContext = MLContext(seed = Nullable 1)
4949
let reader =
50-
mlContext.Data.CreateTextReader(
50+
mlContext.Data.CreateTextLoader(
5151
columns =
5252
[|
5353
TextLoader.Column("LabelText" , Nullable DataKind.Text, 0)
@@ -58,12 +58,11 @@ let main _argv =
5858

5959
let data = reader.Read(trainDataPath)
6060

61-
// Create the estimator which converts the text label to a key sorted by value (with ham < spam we get ham -> false and spam -> true)
62-
// then featurizes the text, and add a linear trainer.
61+
// Create the estimator which converts the text label to a bool then featurizes the text, and add a linear trainer.
6362
let estimator =
6463
EstimatorChain()
65-
.Append(mlContext.Transforms.Conversion.MapValueToKey("LabelText", "Label", sort = ValueToKeyMappingTransformer.SortOrder.Value))
66-
.Append(mlContext.Transforms.Text.FeaturizeText("Message","Features"))
64+
.Append(mlContext.Transforms.Conversion.ValueMap(["ham"; "spam"], [false; true],[| struct ("Label", "LabelText") |]))
65+
.Append(mlContext.Transforms.Text.FeaturizeText("Features", "Message"))
6766
.AppendCacheCheckpoint(mlContext)
6867
.Append(mlContext.BinaryClassification.Trainers.StochasticDualCoordinateAscent("Label", "Features"))
6968

0 commit comments

Comments
 (0)