Skip to content

Commit e169e34

Browse files
author
jralexander
committed
Update SentimentAnalysis to 1.0-preview
1 parent 5340bee commit e169e34

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

machine-learning/tutorials/SentimentAnalysis/Program.cs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6-
using Microsoft.Data.DataView;
76
using Microsoft.ML;
87
using Microsoft.ML.Data;
8+
using static Microsoft.ML.DataOperationsCatalog;
99
using Microsoft.ML.Trainers;
1010
using Microsoft.ML.Transforms.Text;
1111
// </SnippetAddUsings>
@@ -29,7 +29,7 @@ static void Main(string[] args)
2929
// </SnippetCreateMLContext>
3030

3131
// <SnippetCallLoadData>
32-
TrainCatalogBase.TrainTestData splitDataView = LoadData(mlContext);
32+
TrainTestData splitDataView = LoadData(mlContext);
3333
// </SnippetCallLoadData>
3434

3535

@@ -53,18 +53,18 @@ static void Main(string[] args)
5353
Console.WriteLine("=============== End of process ===============");
5454
}
5555

56-
public static TrainCatalogBase.TrainTestData LoadData(MLContext mlContext)
56+
public static TrainTestData LoadData(MLContext mlContext)
5757
{
5858

5959
//Note that this case, loading your training data from a file,
6060
//is the easiest way to get started, but ML.NET also allows you
6161
//to load data from databases or in-memory collections.
6262
// <SnippetLoadData>
63-
IDataView dataView = mlContext.Data.LoadFromTextFile<SentimentData>(_dataPath,hasHeader:false);
63+
IDataView dataView = mlContext.Data.LoadFromTextFile<SentimentData>(_dataPath, hasHeader: false);
6464
// </SnippetLoadData>
6565

6666
// <SnippetSplitData>
67-
TrainCatalogBase.TrainTestData splitDataView = mlContext.BinaryClassification.TrainTestSplit(dataView, testFraction: 0.2);
67+
TrainTestData splitDataView = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
6868
// </SnippetSplitData>
6969

7070
// <SnippetReturnSplitData>
@@ -79,17 +79,17 @@ public static ITransformer BuildAndTrainModel(MLContext mlContext, IDataView spl
7979
// This is used to format and clean the data.
8080
// Convert the text column to numeric vectors (Features column)
8181
// <SnippetFeaturizeText>
82-
var pipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: DefaultColumnNames.Features, inputColumnName: nameof(SentimentData.SentimentText))
82+
var estimator = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentData.SentimentText))
8383
//</SnippetFeaturizeText>
84-
// Adds a FastTreeBinaryClassificationTrainer, the decision tree learner for this project
84+
// append the training algorithm to the estimator
8585
// <SnippetAddTrainer>
86-
.Append(mlContext.BinaryClassification.Trainers.FastTree(numLeaves: 50, numTrees: 50, minDatapointsInLeaves: 20));
86+
.Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features"));
8787
// </SnippetAddTrainer>
8888

8989
// Create and train the model based on the dataset that has been loaded, transformed.
9090
// <SnippetTrainModel>
9191
Console.WriteLine("=============== Create and Train the Model ===============");
92-
var model = pipeline.Fit(splitTrainSet);
92+
var model = estimator.Fit(splitTrainSet);
9393
Console.WriteLine("=============== End of training ===============");
9494
Console.WriteLine();
9595
// </SnippetTrainModel>
@@ -116,15 +116,14 @@ public static void Evaluate(MLContext mlContext, ITransformer model, IDataView s
116116
CalibratedBinaryClassificationMetrics metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label");
117117
// </SnippetEvaluate>
118118

119-
// The Accuracy metric gets the accuracy of a classifier, which is the proportion
119+
// The Accuracy metric gets the accuracy of a model, which is the proportion
120120
// of correct predictions in the test set.
121121

122-
// The Auc metric gets the area under the ROC curve.
123-
// The area under the ROC curve is equal to the probability that the classifier ranks
122+
// The AreaUnderROCCurve metric is equal to the probability that the algorithm ranks
124123
// a randomly chosen positive instance higher than a randomly chosen negative one
125124
// (assuming 'positive' ranks higher than 'negative').
126125

127-
// The F1Score metric gets the classifier's F1 score.
126+
// The F1Score metric gets the model's F1 score.
128127
// The F1 score is the harmonic mean of precision and recall:
129128
// 2 * precision * recall / (precision + recall).
130129

@@ -133,21 +132,24 @@ public static void Evaluate(MLContext mlContext, ITransformer model, IDataView s
133132
Console.WriteLine("Model quality metrics evaluation");
134133
Console.WriteLine("--------------------------------");
135134
Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
136-
Console.WriteLine($"Auc: {metrics.Auc:P2}");
135+
Console.WriteLine($"Auc: {metrics.AreaUnderRocCurve:P2}");
137136
Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
138137
Console.WriteLine("=============== End of model evaluation ===============");
139138
//</SnippetDisplayMetrics>
140139

141140
// Save the new model to .ZIP file
142-
// <SnippetCallSaveModel>
143-
SaveModelAsFile(mlContext, model);
144-
// </SnippetCallSaveModel>
141+
// <SnippetSaveModel>
142+
using (var fs = new FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write))
143+
mlContext.Model.Save(model, predictions.Schema, fs);
144+
// </SnippetSaveModel>
145+
146+
Console.WriteLine("The model is saved to {0}", _modelPath);
145147
}
146148

147149
private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
148150
{
149151
// <SnippetCreatePredictionEngine1>
150-
PredictionEngine<SentimentData, SentimentPrediction> predictionFunction = model.CreatePredictionEngine<SentimentData, SentimentPrediction>(mlContext);
152+
PredictionEngine<SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);
151153
// </SnippetCreatePredictionEngine1>
152154

153155
// <SnippetCreateTestIssue1>
@@ -191,9 +193,11 @@ public static void UseLoadedModelWithBatchItems(MLContext mlContext)
191193

192194
// <SnippetLoadModel>
193195
ITransformer loadedModel;
196+
DataViewSchema dataViewSchema;
197+
194198
using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
195199
{
196-
loadedModel = mlContext.Model.Load(stream);
200+
loadedModel = mlContext.Model.Load(stream, out dataViewSchema);
197201
}
198202
// </SnippetLoadModel>
199203

@@ -231,17 +235,5 @@ public static void UseLoadedModelWithBatchItems(MLContext mlContext)
231235
// </SnippetDisplayResults>
232236
}
233237

234-
// Saves the model we trained to a zip file.
235-
236-
private static void SaveModelAsFile(MLContext mlContext, ITransformer model)
237-
{
238-
// <SnippetSaveModel>
239-
using (var fs = new FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write))
240-
mlContext.Model.Save(model, fs);
241-
// </SnippetSaveModel>
242-
243-
Console.WriteLine("The model is saved to {0}", _modelPath);
244-
}
245-
246238
}
247239
}

machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.ML" Version="0.11.0" />
13+
<PackageReference Include="Microsoft.ML" Version="1.0.0-preview" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

0 commit comments

Comments
 (0)