Skip to content

Commit

Permalink
Samples: exceptions / nits (dotnet#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
daholste authored and Dmitry-A committed Aug 22, 2019
1 parent 355366f commit e71ab96
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
13 changes: 6 additions & 7 deletions src/Samples/AutoTrainBinaryClassification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ public class AutoTrainBinaryClassification
private static string TrainDataPath = $"{BaseDatasetsLocation}/wikipedia-detox-250-line-data.tsv";
private static string TestDataPath = $"{BaseDatasetsLocation}/wikipedia-detox-250-line-test.tsv";
private static string ModelPath = $"{BaseDatasetsLocation}/SentimentModel.zip";
private static string LabelColumnName = "Label";
private static string LabelColumnName = "Sentiment";

public static void Run()
{
//Create ML Context with seed for repeteable/deterministic results
MLContext mlContext = new MLContext(seed: 0);
MLContext mlContext = new MLContext();

// STEP 1: Infer columns
var columnInference = mlContext.Data.InferColumns(TrainDataPath, LabelColumnName, '\t');
Expand All @@ -32,16 +31,16 @@ public static void Run()

// STEP 3: Auto featurize, auto train and auto hyperparameter tuning
Console.WriteLine($"Invoking BinaryClassification.AutoFit");
var autoFitResults = mlContext.BinaryClassification.AutoFit(trainDataView, timeoutInSeconds: 60);
var autoFitResults = mlContext.BinaryClassification.AutoFit(trainDataView, LabelColumnName, timeoutInSeconds: 60);

// STEP 4: Print metric from the best model
var best = autoFitResults.Best();
Console.WriteLine($"Accuracy of best model from validation data {best.Metrics.Accuracy}");
Console.WriteLine($"Accuracy of best model from validation data: {best.Metrics.Accuracy}");

// STEP 5: Evaluate test data
IDataView testDataViewWithBestScore = best.Model.Transform(testDataView);
var testMetrics = mlContext.Regression.Evaluate(testDataViewWithBestScore, label: DefaultColumnNames.Label, DefaultColumnNames.Score);
Console.WriteLine($"Accuracy of best model from test data {best.Metrics.Accuracy}");
var testMetrics = mlContext.BinaryClassification.EvaluateNonCalibrated(testDataViewWithBestScore, label: LabelColumnName, DefaultColumnNames.Score);
Console.WriteLine($"Accuracy of best model from test data: {best.Metrics.Accuracy}");

// STEP 6: Save the best model for later deployment and inferencing
using (var fs = File.Create(ModelPath))
Expand Down
11 changes: 5 additions & 6 deletions src/Samples/AutoTrainMulticlassClassification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public class AutoTrainMulticlassClassification

public static void Run()
{
//Create ML Context with seed for repeteable/deterministic results
MLContext mlContext = new MLContext(seed: 0);
MLContext mlContext = new MLContext();

// STEP 1: Infer columns
var columnInference = mlContext.Data.InferColumns(TrainDataPath, LabelColumnName, '\t');
Expand All @@ -32,16 +31,16 @@ public static void Run()

// STEP 3: Auto featurize, auto train and auto hyperparameter tuning
Console.WriteLine($"Invoking MulticlassClassification.AutoFit");
var autoFitResults = mlContext.MulticlassClassification.AutoFit(trainDataView, timeoutInSeconds: 1);
var autoFitResults = mlContext.MulticlassClassification.AutoFit(trainDataView, timeoutInSeconds: 60);

// STEP 4: Print metric from the best model
var best = autoFitResults.Best();
Console.WriteLine($"AccuracyMacro of best model from validation data {best.Metrics.AccuracyMacro}");
Console.WriteLine($"AccuracyMacro of best model from validation data: {best.Metrics.AccuracyMacro}");

// STEP 5: Evaluate test data
IDataView testDataViewWithBestScore = best.Model.Transform(testDataView);
var testMetrics = mlContext.Regression.Evaluate(testDataViewWithBestScore, label: DefaultColumnNames.Label, DefaultColumnNames.Score);
Console.WriteLine($"AccuracyMacro of best model from test data {best.Metrics.AccuracyMacro}");
var testMetrics = mlContext.MulticlassClassification.Evaluate(testDataViewWithBestScore, label: DefaultColumnNames.Label, DefaultColumnNames.Score);
Console.WriteLine($"AccuracyMacro of best model from test data: {best.Metrics.AccuracyMacro}");

// STEP 6: Save the best model for later deployment and inferencing
using (var fs = File.Create(ModelPath))
Expand Down
9 changes: 4 additions & 5 deletions src/Samples/AutoTrainRegression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ static class AutoTrainRegression

public static void Run()
{
//Create ML Context with seed for repeteable/deterministic results
MLContext mlContext = new MLContext(seed: 0);
MLContext mlContext = new MLContext();

// STEP 1: Common data loading configuration
var columnInference = mlContext.Data.InferColumns(TrainDataPath, LabelColumnName, ',');
Expand All @@ -32,16 +31,16 @@ public static void Run()

// STEP 3: Auto featurize, auto train and auto hyperparameter tuning
Console.WriteLine($"Invoking Regression.AutoFit");
var autoFitResults = mlContext.Regression.AutoFit(trainDataView, LabelColumnName, timeoutInSeconds:1);
var autoFitResults = mlContext.Regression.AutoFit(trainDataView, LabelColumnName, timeoutInSeconds: 60);

// STEP 4: Compare and print actual value vs predicted value for top 5 rows from validation data
var best = autoFitResults.Best();
Console.WriteLine($"RSquared of best model from validation data {best.Metrics.RSquared}");
Console.WriteLine($"RSquared of best model from validation data: {best.Metrics.RSquared}");

// STEP 5: Evaluate test data
IDataView testDataViewWithBestScore = best.Model.Transform(testDataView);
var testMetrics = mlContext.Regression.Evaluate(testDataViewWithBestScore, label: DefaultColumnNames.Label, DefaultColumnNames.Score);
Console.WriteLine($"RSquared of best model from test data {best.Metrics.RSquared}");
Console.WriteLine($"RSquared of best model from test data: {best.Metrics.RSquared}");

// STEP 6: Save the best model for later deployment and inferencing
using (var fs = File.Create(ModelPath))
Expand Down
2 changes: 1 addition & 1 deletion src/Samples/Data/iris-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Label Sepal length Sepal width Petal length Petal width
Label Sepal length Sepal width Petal length Petal width
0 5.1 3.5 1.4 0.2
0 4.9 3.0 1.4 0.2
0 4.7 3.2 1.3 0.2
Expand Down
2 changes: 1 addition & 1 deletion src/Samples/Data/iris-train.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Label Sepal length Sepal width Petal length Petal width
Label Sepal length Sepal width Petal length Petal width
0 5.4 3.7 1.5 0.2
0 4.8 3.4 1.6 0.2
0 4.8 3.0 1.4 0.1
Expand Down

0 comments on commit e71ab96

Please sign in to comment.