-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
300 additions
and
27 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/LinearSvm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.ML; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Samples.Dynamic.Trainers.BinaryClassification | ||
{ | ||
public static class LinearSvm | ||
{ | ||
public static void Example() | ||
{ | ||
// Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
// as a catalog of available operations and as the source of randomness. | ||
// Setting the seed to a fixed number in this example to make outputs deterministic. | ||
var mlContext = new MLContext(seed: 0); | ||
|
||
// Create a list of training data points. | ||
var dataPoints = GenerateRandomDataPoints(1000); | ||
|
||
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API. | ||
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints); | ||
|
||
// Define the trainer. | ||
var pipeline = mlContext.BinaryClassification.Trainers.LinearSvm(); | ||
|
||
// Train the model. | ||
var model = pipeline.Fit(trainingData); | ||
|
||
// Create testing data. Use different random seed to make it different from training data. | ||
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed:123)); | ||
|
||
// Run the model on test data set. | ||
var transformedTestData = model.Transform(testData); | ||
|
||
// Convert IDataView object to a list. | ||
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList(); | ||
|
||
// Look at 5 predictions | ||
foreach (var p in predictions.Take(5)) | ||
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}"); | ||
|
||
// Expected output: | ||
// Label: True, Prediction: True | ||
// Label: False, Prediction: False | ||
// Label: True, Prediction: True | ||
// Label: True, Prediction: False | ||
// Label: False, Prediction: True | ||
|
||
// Evaluate the overall metrics | ||
var metrics = mlContext.BinaryClassification.EvaluateNonCalibrated(transformedTestData); | ||
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}"); | ||
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}"); | ||
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}"); | ||
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}"); | ||
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}"); | ||
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}"); | ||
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}"); | ||
|
||
// Expected output: | ||
// Accuracy: 0.53 | ||
// AUC: 0.56 | ||
// F1 Score: 0.49 | ||
// Negative Precision: 0.55 | ||
// Negative Recall: 0.58 | ||
// Positive Precision: 0.50 | ||
// Positive Recall: 0.47 | ||
} | ||
|
||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)random.NextDouble(); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
var label = randomFloat() > 0.5f; | ||
yield return new DataPoint | ||
{ | ||
Label = label, | ||
// Create random features that are correlated with the label. | ||
// For data points with false label, the feature values are slightly increased by adding a constant. | ||
Features = Enumerable.Repeat(label, 50).Select(x => x ? randomFloat() : randomFloat() + 0.03f).ToArray() | ||
}; | ||
} | ||
} | ||
|
||
// Example with label and 50 feature values. A data set is a collection of such examples. | ||
private class DataPoint | ||
{ | ||
public bool Label { get; set; } | ||
[VectorType(50)] | ||
public float[] Features { get; set; } | ||
} | ||
|
||
// Class used to capture predictions. | ||
private class Prediction | ||
{ | ||
// Original label. | ||
public bool Label { get; set; } | ||
// Predicted label from the trainer. | ||
public bool PredictedLabel { get; set; } | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/LinearSvm.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<#@ include file="BinaryClassification.ttinclude"#> | ||
<#+ | ||
string ClassName = "LinearSvm"; | ||
string Trainer = "LinearSvm"; | ||
string TrainerOptions = null; | ||
bool IsCalibrated = false; | ||
bool CacheData = false; | ||
|
||
string DataSepValue = "0.03f"; | ||
string OptionsInclude = ""; | ||
string Comments = ""; | ||
|
||
string ExpectedOutputPerInstance = @"// Expected output: | ||
// Label: True, Prediction: True | ||
// Label: False, Prediction: False | ||
// Label: True, Prediction: True | ||
// Label: True, Prediction: False | ||
// Label: False, Prediction: True"; | ||
|
||
string ExpectedOutput = @"// Expected output: | ||
// Accuracy: 0.53 | ||
// AUC: 0.56 | ||
// F1 Score: 0.49 | ||
// Negative Precision: 0.55 | ||
// Negative Recall: 0.58 | ||
// Positive Precision: 0.50 | ||
// Positive Recall: 0.47"; | ||
#> |
113 changes: 113 additions & 0 deletions
113
...amples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/LinearSvmWithOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.ML; | ||
using Microsoft.ML.Data; | ||
using Microsoft.ML.Trainers; | ||
|
||
namespace Samples.Dynamic.Trainers.BinaryClassification | ||
{ | ||
public static class LinearSvmWithOptions | ||
{ | ||
public static void Example() | ||
{ | ||
// Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
// as a catalog of available operations and as the source of randomness. | ||
// Setting the seed to a fixed number in this example to make outputs deterministic. | ||
var mlContext = new MLContext(seed: 0); | ||
|
||
// Create a list of training data points. | ||
var dataPoints = GenerateRandomDataPoints(1000); | ||
|
||
// Convert the list of data points to an IDataView object, which is consumable by ML.NET API. | ||
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints); | ||
|
||
// Define trainer options. | ||
var options = new LinearSvmTrainer.Options | ||
{ | ||
BatchSize = 10, | ||
PerformProjection = true, | ||
NumberOfIterations = 10 | ||
}; | ||
|
||
// Define the trainer. | ||
var pipeline = mlContext.BinaryClassification.Trainers.LinearSvm(options); | ||
|
||
// Train the model. | ||
var model = pipeline.Fit(trainingData); | ||
|
||
// Create testing data. Use different random seed to make it different from training data. | ||
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed:123)); | ||
|
||
// Run the model on test data set. | ||
var transformedTestData = model.Transform(testData); | ||
|
||
// Convert IDataView object to a list. | ||
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList(); | ||
|
||
// Look at 5 predictions | ||
foreach (var p in predictions.Take(5)) | ||
Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}"); | ||
|
||
// Expected output: | ||
// Label: True, Prediction: False | ||
// Label: False, Prediction: False | ||
// Label: True, Prediction: True | ||
// Label: True, Prediction: True | ||
// Label: False, Prediction: False | ||
|
||
// Evaluate the overall metrics | ||
var metrics = mlContext.BinaryClassification.EvaluateNonCalibrated(transformedTestData); | ||
Console.WriteLine($"Accuracy: {metrics.Accuracy:F2}"); | ||
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:F2}"); | ||
Console.WriteLine($"F1 Score: {metrics.F1Score:F2}"); | ||
Console.WriteLine($"Negative Precision: {metrics.NegativePrecision:F2}"); | ||
Console.WriteLine($"Negative Recall: {metrics.NegativeRecall:F2}"); | ||
Console.WriteLine($"Positive Precision: {metrics.PositivePrecision:F2}"); | ||
Console.WriteLine($"Positive Recall: {metrics.PositiveRecall:F2}"); | ||
|
||
// Expected output: | ||
// Accuracy: 0.58 | ||
// AUC: 0.67 | ||
// F1 Score: 0.28 | ||
// Negative Precision: 0.56 | ||
// Negative Recall: 0.94 | ||
// Positive Precision: 0.74 | ||
// Positive Recall: 0.18 | ||
} | ||
|
||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)random.NextDouble(); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
var label = randomFloat() > 0.5f; | ||
yield return new DataPoint | ||
{ | ||
Label = label, | ||
// Create random features that are correlated with the label. | ||
// For data points with false label, the feature values are slightly increased by adding a constant. | ||
Features = Enumerable.Repeat(label, 50).Select(x => x ? randomFloat() : randomFloat() + 0.03f).ToArray() | ||
}; | ||
} | ||
} | ||
|
||
// Example with label and 50 feature values. A data set is a collection of such examples. | ||
private class DataPoint | ||
{ | ||
public bool Label { get; set; } | ||
[VectorType(50)] | ||
public float[] Features { get; set; } | ||
} | ||
|
||
// Class used to capture predictions. | ||
private class Prediction | ||
{ | ||
// Original label. | ||
public bool Label { get; set; } | ||
// Predicted label from the trainer. | ||
public bool PredictedLabel { get; set; } | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...amples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/LinearSvmWithOptions.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<#@ include file="BinaryClassification.ttinclude"#> | ||
<#+ | ||
string ClassName = "LinearSvmWithOptions"; | ||
string Trainer = "LinearSvm"; | ||
bool IsCalibrated = false; | ||
|
||
string DataSepValue = "0.03f"; | ||
string OptionsInclude = "using Microsoft.ML.Trainers;"; | ||
string Comments = ""; | ||
bool CacheData = false; | ||
|
||
string TrainerOptions = @"LinearSvmTrainer.Options | ||
{ | ||
BatchSize = 10, | ||
PerformProjection = true, | ||
NumberOfIterations = 10 | ||
}"; | ||
|
||
string ExpectedOutputPerInstance = @"// Expected output: | ||
// Label: True, Prediction: False | ||
// Label: False, Prediction: False | ||
// Label: True, Prediction: True | ||
// Label: True, Prediction: True | ||
// Label: False, Prediction: False"; | ||
|
||
string ExpectedOutput = @"// Expected output: | ||
// Accuracy: 0.58 | ||
// AUC: 0.67 | ||
// F1 Score: 0.28 | ||
// Negative Precision: 0.56 | ||
// Negative Recall: 0.94 | ||
// Positive Precision: 0.74 | ||
// Positive Recall: 0.18"; | ||
#> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,12 @@ | ||
using System; | ||
using Samples.Dynamic.Trainers.BinaryClassification; | ||
using Samples.Dynamic; | ||
|
||
namespace Microsoft.ML.Samples | ||
{ | ||
internal static class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Prior"); | ||
Prior.Example(); | ||
Console.WriteLine("\nSdcaLogisticRegression"); | ||
SdcaLogisticRegression.Example(); | ||
Console.WriteLine("\nSdcaLogisticRegressionWithOptions"); | ||
SdcaLogisticRegressionWithOptions.Example(); | ||
Console.WriteLine("\nSdcaNonCalibrated"); | ||
SdcaNonCalibrated.Example(); | ||
Console.WriteLine("\nSdcaNonCalibratedWithOptions"); | ||
SdcaNonCalibratedWithOptions.Example(); | ||
Console.WriteLine("\nSgdCalibrated"); | ||
SgdCalibrated.Example(); | ||
Console.WriteLine("\nSgdCalibratedWithOptions"); | ||
SgdCalibratedWithOptions.Example(); | ||
Console.WriteLine("\nSgdNonCalibrated"); | ||
SgdNonCalibrated.Example(); | ||
Console.WriteLine("\nSgdNonCalibratedWithOptions"); | ||
SgdNonCalibratedWithOptions.Example(); | ||
Console.WriteLine("\nSymbolicSgdLogisticRegression"); | ||
SymbolicSgdLogisticRegression.Example(); | ||
Console.WriteLine("\nSymbolicSgdLogisticRegressionWithOptions"); | ||
SymbolicSgdLogisticRegression.Example(); | ||
|
||
Console.ReadLine(); | ||
CalculateFeatureContribution.Example(); | ||
} | ||
} | ||
} | ||
} |