From f44d3f6e3dd2444e9509d27311293868b3893749 Mon Sep 17 00:00:00 2001 From: Wei-Sheng Chin Date: Thu, 25 Apr 2019 15:43:34 -0700 Subject: [PATCH 1/2] Clean SamplesUtils --- .../Dynamic/DataOperations/Cache.cs | 22 +- .../Dynamic/DataOperations/TakeRows.cs | 4 +- .../Dynamic/NgramExtraction.cs | 23 +- .../Dynamic/TextTransform.cs | 23 +- .../Trainers/Regression/LightGbmAdvanced.cs | 10 +- .../Regression/LightGbmWithOptionsAdvanced.cs | 10 +- .../OrdinaryLeastSquaresAdvanced.cs | 10 +- ...OrdinaryLeastSquaresWithOptionsAdvanced.cs | 10 +- .../Transforms/Projection/VectorWhiten.cs | 41 +- .../Projection/VectorWhitenWithOptions.cs | 41 +- src/Microsoft.ML.SamplesUtils/ConsoleUtils.cs | 77 ---- .../SamplesDatasetUtils.cs | 426 +----------------- .../TrainerEstimators/FAFMEstimator.cs | 75 ++- 13 files changed, 247 insertions(+), 525 deletions(-) delete mode 100644 src/Microsoft.ML.SamplesUtils/ConsoleUtils.cs diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/Cache.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/Cache.cs index 2519ca86f7..4ea7c33723 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/Cache.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/Cache.cs @@ -43,7 +43,7 @@ private static (int lines, double columnAverage, double elapsedSeconds) TimeToSc { int lines = 0; double columnAverage = 0.0; - var enumerable = mlContext.Data.CreateEnumerable(data, reuseRowObject: true); + var enumerable = mlContext.Data.CreateEnumerable(data, reuseRowObject: true); var watch = System.Diagnostics.Stopwatch.StartNew(); foreach (var row in enumerable) { @@ -58,5 +58,25 @@ private static (int lines, double columnAverage, double elapsedSeconds) TimeToSc return (lines, columnAverage, elapsed.Seconds); } + + /// + /// A class to hold the raw housing regression rows. + /// + public sealed class HousingRegression + { + public float MedianHomeValue { get; set; } + public float CrimesPerCapita { get; set; } + public float PercentResidental { get; set; } + public float PercentNonRetail { get; set; } + public float CharlesRiver { get; set; } + public float NitricOxides { get; set; } + public float RoomsPerDwelling { get; set; } + public float PercentPre40s { get; set; } + public float EmploymentDistance { get; set; } + public float HighwayDistance { get; set; } + public float TaxRate { get; set; } + public float TeacherRatio { get; set; } + } + } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/TakeRows.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/TakeRows.cs index 5c41226f93..26a489f5e8 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/TakeRows.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/TakeRows.cs @@ -14,7 +14,7 @@ public static void Example() var mlContext = new MLContext(); // Get a small dataset as an IEnumerable. - var enumerableOfData = Microsoft.ML.SamplesUtils.DatasetUtils.GetSampleTemperatureData(10); + var enumerableOfData = GetSampleTemperatureData(10); var data = mlContext.Data.LoadFromEnumerable(enumerableOfData); // Before we apply a filter, examine all the records in the dataset. @@ -41,7 +41,7 @@ public static void Example() var filteredData = mlContext.Data.TakeRows(data, 5); // Look at the filtered data and observe that only the first 5 rows are in the resulting dataset. - var enumerable = mlContext.Data.CreateEnumerable(filteredData, reuseRowObject: true); + var enumerable = mlContext.Data.CreateEnumerable(filteredData, reuseRowObject: true); Console.WriteLine($"Date\tTemperature"); foreach (var row in enumerable) { diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs index e2bfe2ee43..e625fba9db 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs @@ -14,7 +14,7 @@ public static void Example() var ml = new MLContext(); // Get a small dataset as an IEnumerable and convert to IDataView. - IEnumerable data = Microsoft.ML.SamplesUtils.DatasetUtils.GetSentimentData(); + IEnumerable data = GetSentimentData(); var trainData = ml.Data.LoadFromEnumerable(data); // Preview of the data. @@ -71,5 +71,26 @@ public static void Example() // 'e' - 1 '' - 2 'd' - 1 '=' - 4 '=|=' - 2 '=|R' - 1 'R' - 1 'R|U' - 1 'U' - 1 'U|D' - 1 'D' - 2 ... // 'B' - 0 'B|e' - 0 'e' - 6 'e|s' - 1 's' - 3 's|t' - 1 't' - 6 't|' - 2 '' - 9 '|g' - 2 ... } + + /// + /// A dataset that contains a tweet and the sentiment assigned to that tweet: 0 - negative and 1 - positive sentiment. + /// + public class SampleSentimentData + { + public bool Sentiment { get; set; } + public string SentimentText { get; set; } + } + + /// + /// Returns a sample of the sentiment dataset. + /// + public static IEnumerable GetSentimentData() + { + var data = new List(); + data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Best game I've ever played." }); + data.Add(new SampleSentimentData { Sentiment = false, SentimentText = "==RUDE== Dude, 2" }); + data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Until the next game, this is the best Xbox game!" }); + return data; + } } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs index 6b8c507b07..173e6bdc55 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs @@ -15,7 +15,7 @@ public static void Example() var ml = new MLContext(); // Get a small dataset as an IEnumerable and convert to IDataView. - var data = Microsoft.ML.SamplesUtils.DatasetUtils.GetSentimentData(); + var data = GetSentimentData(); var trainData = ml.Data.LoadFromEnumerable(data); // Preview of the data. @@ -78,5 +78,26 @@ public static void Example() // 0.25 0.25 0.25 0.25 0.5 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.7071068 0.7071068 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.4472136 0.4472136 0.4472136 0.4472136 0.4472136 // 0 0.125 0.125 0.125 0.125 0.25 0.25 0.25 0.125 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.125 0.125 0.125 0.125 0.125 0.125 0.375 0.25 0.25 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.25 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.2672612 0.5345225 0 0 0 0 0 0.2672612 0.5345225 0.2672612 0.2672612 0.2672612 0.2672612 } } + + /// + /// A dataset that contains a tweet and the sentiment assigned to that tweet: 0 - negative and 1 - positive sentiment. + /// + public class SampleSentimentData + { + public bool Sentiment { get; set; } + public string SentimentText { get; set; } + } + + /// + /// Returns a sample of the sentiment dataset. + /// + public static IEnumerable GetSentimentData() + { + var data = new List(); + data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Best game I've ever played." }); + data.Add(new SampleSentimentData { Sentiment = false, SentimentText = "==RUDE== Dude, 2" }); + data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Until the next game, this is the best Xbox game!" }); + return data; + } } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmAdvanced.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmAdvanced.cs index 0d5f09513f..363d746305 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmAdvanced.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmAdvanced.cs @@ -53,7 +53,7 @@ public static void Example() // Evaluate how the model is doing on the test data. var dataWithPredictions = model.Transform(split.TestSet); var metrics = mlContext.Regression.Evaluate(dataWithPredictions, labelColumnName: labelName); - Microsoft.ML.SamplesUtils.ConsoleUtils.PrintMetrics(metrics); + PrintMetrics(metrics); // Expected output // L1: 4.97 @@ -62,5 +62,13 @@ public static void Example() // RMS: 7.17 // RSquared: 0.08 } + + public static void PrintMetrics(RegressionMetrics metrics) + { + Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}"); + Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}"); + Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}"); + Console.WriteLine($"RSquared: {metrics.RSquared:F2}"); + } } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmWithOptionsAdvanced.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmWithOptionsAdvanced.cs index e140cd6b52..acc48bbdf7 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmWithOptionsAdvanced.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmWithOptionsAdvanced.cs @@ -62,7 +62,7 @@ public static void Example() // Evaluate how the model is doing on the test data. var dataWithPredictions = model.Transform(split.TestSet); var metrics = mlContext.Regression.Evaluate(dataWithPredictions, labelColumnName: labelName); - Microsoft.ML.SamplesUtils.ConsoleUtils.PrintMetrics(metrics); + PrintMetrics(metrics); // Expected output // L1: 4.97 @@ -71,5 +71,13 @@ public static void Example() // RMS: 7.17 // RSquared: 0.08 } + + public static void PrintMetrics(RegressionMetrics metrics) + { + Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}"); + Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}"); + Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}"); + Console.WriteLine($"RSquared: {metrics.RSquared:F2}"); + } } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/OrdinaryLeastSquaresAdvanced.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/OrdinaryLeastSquaresAdvanced.cs index 2d61dc4aa7..c5c3b2c097 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/OrdinaryLeastSquaresAdvanced.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/OrdinaryLeastSquaresAdvanced.cs @@ -55,7 +55,7 @@ public static void Example() var dataWithPredictions = model.Transform(split.TestSet); var metrics = mlContext.Regression.Evaluate(dataWithPredictions); - ConsoleUtils.PrintMetrics(metrics); + PrintMetrics(metrics); // Expected output: // L1: 4.15 @@ -64,5 +64,13 @@ public static void Example() // RMS: 5.65 // RSquared: 0.56 } + + public static void PrintMetrics(RegressionMetrics metrics) + { + Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}"); + Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}"); + Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}"); + Console.WriteLine($"RSquared: {metrics.RSquared:F2}"); + } } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/OrdinaryLeastSquaresWithOptionsAdvanced.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/OrdinaryLeastSquaresWithOptionsAdvanced.cs index 48fa19cb6b..69728ac5b9 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/OrdinaryLeastSquaresWithOptionsAdvanced.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/OrdinaryLeastSquaresWithOptionsAdvanced.cs @@ -59,7 +59,7 @@ public static void Example() var dataWithPredictions = model.Transform(split.TestSet); var metrics = mlContext.Regression.Evaluate(dataWithPredictions); - ConsoleUtils.PrintMetrics(metrics); + PrintMetrics(metrics); // Expected output: // L1: 4.14 @@ -68,5 +68,13 @@ public static void Example() // RMS: 5.69 // RSquared: 0.56 } + + public static void PrintMetrics(RegressionMetrics metrics) + { + Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}"); + Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}"); + Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}"); + Console.WriteLine($"RSquared: {metrics.RSquared:F2}"); + } } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Projection/VectorWhiten.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Projection/VectorWhiten.cs index 0f0f1d5370..9de60d5130 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Projection/VectorWhiten.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Projection/VectorWhiten.cs @@ -17,7 +17,7 @@ public static void Example() var ml = new MLContext(); // Get a small dataset as an IEnumerable and convert it to an IDataView. - var data = Microsoft.ML.SamplesUtils.DatasetUtils.GetVectorOfNumbersData(); + var data = GetVectorOfNumbersData(); var trainData = ml.Data.LoadFromEnumerable(data); // Preview of the data. @@ -40,14 +40,14 @@ public static void Example() }; // A pipeline to project Features column into white noise vector. - var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features), + var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(SampleVectorOfNumbersData.Features), kind: Microsoft.ML.Transforms.WhiteningKind.ZeroPhaseComponentAnalysis); // The transformed (projected) data. var transformedData = whiteningPipeline.Fit(trainData).Transform(trainData); // Getting the data of the newly created column, so we can preview it. - var whitening = transformedData.GetColumn>(transformedData.Schema[nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features)]); + var whitening = transformedData.GetColumn>(transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]); - printHelper(nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features), whitening); + printHelper(nameof(SampleVectorOfNumbersData.Features), whitening); // Features column obtained post-transformation. // @@ -58,5 +58,38 @@ public static void Example() // 0.454 0.523 0.593 0.664 1.886 -0.757 -0.687 -0.022 0.176 0.310 // 0.863 0.938 1.016 1.093 -1.326 -0.096 -0.019 0.189 0.330 0.483 } + + private class SampleVectorOfNumbersData + { + [VectorType(10)] + public float[] Features { get; set; } + } + + /// + /// Returns a few rows of the infertility dataset. + /// + private static IEnumerable GetVectorOfNumbersData() + { + var data = new List(); + data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } }); + data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } }); + data.Add(new SampleVectorOfNumbersData + { + Features = new float[10] { 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 } + }); + data.Add(new SampleVectorOfNumbersData + { + Features = new float[10] { 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, } + }); + data.Add(new SampleVectorOfNumbersData + { + Features = new float[10] { 5, 6, 7, 8, 9, 0, 1, 2, 3, 4 } + }); + data.Add(new SampleVectorOfNumbersData + { + Features = new float[10] { 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 } + }); + return data; + } } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Projection/VectorWhitenWithOptions.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Projection/VectorWhitenWithOptions.cs index 4349140c62..2e4e7fe46a 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Projection/VectorWhitenWithOptions.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Projection/VectorWhitenWithOptions.cs @@ -16,7 +16,7 @@ public static void Example() var ml = new MLContext(); // Get a small dataset as an IEnumerable and convert it to an IDataView. - var data = Microsoft.ML.SamplesUtils.DatasetUtils.GetVectorOfNumbersData(); + var data = GetVectorOfNumbersData(); var trainData = ml.Data.LoadFromEnumerable(data); // Preview of the data. @@ -40,13 +40,13 @@ public static void Example() // A pipeline to project Features column into white noise vector. - var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features), kind: Microsoft.ML.Transforms.WhiteningKind.PrincipalComponentAnalysis, rank: 4); + var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(SampleVectorOfNumbersData.Features), kind: Microsoft.ML.Transforms.WhiteningKind.PrincipalComponentAnalysis, rank: 4); // The transformed (projected) data. var transformedData = whiteningPipeline.Fit(trainData).Transform(trainData); // Getting the data of the newly created column, so we can preview it. - var whitening = transformedData.GetColumn>(transformedData.Schema[nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features)]); + var whitening = transformedData.GetColumn>(transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]); - printHelper(nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features), whitening); + printHelper(nameof(SampleVectorOfNumbersData.Features), whitening); // Features column obtained post-transformation. // -0.979 0.867 1.449 1.236 @@ -56,5 +56,38 @@ public static void Example() // -0.972 -1.338 -0.028 0.614 // -0.938 -1.405 0.752 -0.967 } + + private class SampleVectorOfNumbersData + { + [VectorType(10)] + public float[] Features { get; set; } + } + + /// + /// Returns a few rows of the infertility dataset. + /// + private static IEnumerable GetVectorOfNumbersData() + { + var data = new List(); + data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } }); + data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } }); + data.Add(new SampleVectorOfNumbersData + { + Features = new float[10] { 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 } + }); + data.Add(new SampleVectorOfNumbersData + { + Features = new float[10] { 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, } + }); + data.Add(new SampleVectorOfNumbersData + { + Features = new float[10] { 5, 6, 7, 8, 9, 0, 1, 2, 3, 4 } + }); + data.Add(new SampleVectorOfNumbersData + { + Features = new float[10] { 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 } + }); + return data; + } } } diff --git a/src/Microsoft.ML.SamplesUtils/ConsoleUtils.cs b/src/Microsoft.ML.SamplesUtils/ConsoleUtils.cs deleted file mode 100644 index 7b13d3c41b..0000000000 --- a/src/Microsoft.ML.SamplesUtils/ConsoleUtils.cs +++ /dev/null @@ -1,77 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Linq; -using Microsoft.ML.Data; - -namespace Microsoft.ML.SamplesUtils -{ - /// - /// Utilities for creating console outputs in samples' code. - /// - public static class ConsoleUtils - { - /// - /// Pretty-print BinaryClassificationMetrics objects. - /// - /// Binary classification metrics. - public static void PrintMetrics(BinaryClassificationMetrics metrics) - { - 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}"); - } - - /// - /// Pretty-print CalibratedBinaryClassificationMetrics objects. - /// - /// object. - public static void PrintMetrics(CalibratedBinaryClassificationMetrics metrics) - { - PrintMetrics(metrics as BinaryClassificationMetrics); - Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}"); - Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}"); - Console.WriteLine($"Entropy: {metrics.Entropy:F2}"); - } - - /// - /// Pretty-print MulticlassClassificationMetrics objects. - /// - /// object. - public static void PrintMetrics(MulticlassClassificationMetrics metrics) - { - Console.WriteLine($"Micro Accuracy: {metrics.MicroAccuracy:F2}"); - Console.WriteLine($"Macro Accuracy: {metrics.MacroAccuracy:F2}"); - Console.WriteLine($"Log Loss: {metrics.LogLoss:F2}"); - Console.WriteLine($"Log Loss Reduction: {metrics.LogLossReduction:F2}"); - } - - /// - /// Pretty-print RegressionMetrics objects. - /// - /// Regression metrics. - public static void PrintMetrics(RegressionMetrics metrics) - { - Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}"); - Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}"); - Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}"); - Console.WriteLine($"RSquared: {metrics.RSquared:F2}"); - } - - /// - /// Pretty-print RankerMetrics objects. - /// - /// Ranker metrics. - public static void PrintMetrics(RankingMetrics metrics) - { - Console.WriteLine($"DCG: {string.Join(", ", metrics.DiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F2}").ToArray())}"); - Console.WriteLine($"NDCG: {string.Join(", ", metrics.NormalizedDiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F2}").ToArray())}"); - } - } -} diff --git a/src/Microsoft.ML.SamplesUtils/SamplesDatasetUtils.cs b/src/Microsoft.ML.SamplesUtils/SamplesDatasetUtils.cs index 634839eacf..d205baa10e 100644 --- a/src/Microsoft.ML.SamplesUtils/SamplesDatasetUtils.cs +++ b/src/Microsoft.ML.SamplesUtils/SamplesDatasetUtils.cs @@ -55,69 +55,10 @@ public static IDataView LoadHousingRegressionDataset(MLContext mlContext) } /// - /// A class to hold the raw housing regression rows. + /// Downloads the adult dataset from the ML.NET repo. /// - public sealed class HousingRegression - { - public float MedianHomeValue { get; set; } - public float CrimesPerCapita { get; set; } - public float PercentResidental { get; set; } - public float PercentNonRetail { get; set; } - public float CharlesRiver { get; set; } - public float NitricOxides { get; set; } - public float RoomsPerDwelling { get; set; } - public float PercentPre40s { get; set; } - public float EmploymentDistance { get; set; } - public float HighwayDistance { get; set; } - public float TaxRate { get; set; } - public float TeacherRatio { get; set; } - } - - /// - /// Downloads the wikipedia detox dataset from the ML.NET repo. - /// - public static string[] DownloadSentimentDataset() - { - var trainFile = Download("https://raw.githubusercontent.com/dotnet/machinelearning/76cb2cdf5cc8b6c88ca44b8969153836e589df04/test/data/wikipedia-detox-250-line-data.tsv", "sentiment.tsv"); - var testFile = Download("https://raw.githubusercontent.com/dotnet/machinelearning/76cb2cdf5cc8b6c88ca44b8969153836e589df04/test/data/wikipedia-detox-250-line-test.tsv", "sentimenttest.tsv"); - return new[] { trainFile, testFile }; - } - - /// - /// Downloads the adult dataset from the ML.NET repo. - /// - public static string DownloadAdultDataset() - => Download("https://raw.githubusercontent.com/dotnet/machinelearning/244a8c2ac832657af282aa312d568211698790aa/test/data/adult.train", "adult.txt"); - - /// - /// Downloads the wikipedia detox dataset and featurizes it to be suitable for sentiment classification tasks. - /// - /// used for data loading and processing. - /// Featurized train and test dataset. - public static IDataView[] LoadFeaturizedSentimentDataset(MLContext mlContext) - { - // Download the files - var dataFiles = DownloadSentimentDataset(); - - // Define the columns to load - var loader = mlContext.Data.CreateTextLoader( - columns: new[] - { - new TextLoader.Column("Sentiment", DataKind.Boolean, 0), - new TextLoader.Column("SentimentText", DataKind.String, 1) - }, - hasHeader: true - ); - - // Create data featurizing pipeline - var pipeline = mlContext.Transforms.Text.FeaturizeText("Features", "SentimentText"); - - var data = loader.Load(dataFiles[0]); - var model = pipeline.Fit(data); - var featurizedDataTrain = model.Transform(data); - var featurizedDataTest = model.Transform(loader.Load(dataFiles[1])); - return new[] { featurizedDataTrain, featurizedDataTest }; - } + public static string DownloadAdultDataset() + => Download("https://raw.githubusercontent.com/dotnet/machinelearning/244a8c2ac832657af282aa312d568211698790aa/test/data/adult.train", "adult.txt"); /// /// Downloads the Adult UCI dataset and featurizes it to be suitable for classification tasks. @@ -178,43 +119,6 @@ public static IDataView LoadFeaturizedAdultDataset(MLContext mlContext) return featurizedData; } - public static string DownloadMslrWeb10k() - { - var fileName = "MSLRWeb10KTrain10kRows.tsv"; - if (!File.Exists(fileName)) - Download("https://tlcresources.blob.core.windows.net/datasets/MSLR-WEB10K/MSLR-WEB10K%2BFold1.TRAIN.SMALL_10k-rows.tsv", fileName); - return fileName; - } - - public static IDataView LoadFeaturizedMslrWeb10kDataset(MLContext mlContext) - { - // Download the training and validation files. - string dataFile = DownloadMslrWeb10k(); - - // Create the loader to load the data. - var loader = mlContext.Data.CreateTextLoader( - columns: new[] - { - new TextLoader.Column("Label", DataKind.Single, 0), - new TextLoader.Column("GroupId", DataKind.String, 1), - new TextLoader.Column("Features", DataKind.Single, new[] { new TextLoader.Range(2, 138) }) - } - ); - - // Load the raw dataset. - var data = loader.Load(dataFile); - - // Create the featurization pipeline. First, hash the GroupId column. - var pipeline = mlContext.Transforms.Conversion.Hash("GroupId") - // Replace missing values in Features column with the default replacement value for its type. - .Append(mlContext.Transforms.ReplaceMissingValues("Features")); - - // Fit the pipeline and transform the dataset. - var transformedData = pipeline.Fit(data).Transform(data); - - return transformedData; - } - /// /// Downloads the breast cancer dataset from the ML.NET repo. /// @@ -283,231 +187,6 @@ private static string Download(string baseGitPath, string dataFile) return dataFile; } - /// - /// A simple set of features that help generate the Target column, according to a function. - /// Used for the transformers/estimators working on numeric data. - /// - public class SampleInput - { - public float Feature0 { get; set; } - public float Feature1 { get; set; } - public float Feature2 { get; set; } - public float Feature3 { get; set; } - public float Target { get; set; } - } - - /// - /// Returns a sample of a numeric dataset. - /// - public static IEnumerable GetInputData() - { - var data = new List(); - data.Add(new SampleInput { Feature0 = -2.75f, Feature1 = 0.77f, Feature2 = -0.61f, Feature3 = 0.14f, Target = 140.66f }); - data.Add(new SampleInput { Feature0 = -0.61f, Feature1 = -0.37f, Feature2 = -0.12f, Feature3 = 0.55f, Target = 148.12f }); - data.Add(new SampleInput { Feature0 = -0.85f, Feature1 = -0.91f, Feature2 = 1.81f, Feature3 = 0.02f, Target = 402.20f }); - - return data; - } - - /// - /// A dataset that contains a tweet and the sentiment assigned to that tweet: 0 - negative and 1 - positive sentiment. - /// - public class SampleSentimentData - { - public bool Sentiment { get; set; } - public string SentimentText { get; set; } - } - - /// - /// Returns a sample of the sentiment dataset. - /// - public static IEnumerable GetSentimentData() - { - var data = new List(); - data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Best game I've ever played." }); - data.Add(new SampleSentimentData { Sentiment = false, SentimentText = "==RUDE== Dude, 2" }); - data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Until the next game, this is the best Xbox game!" }); - - return data; - } - - /// - /// A dataset that contains one column with two set of keys assigned to a body of text: Review and ReviewReverse. - /// The dataset will be used to classify how accurately the keys are assigned to the text. - /// - public class SampleTopicsData - { - public string Review { get; set; } - public string ReviewReverse { get; set; } - public bool Label { get; set; } - } - - /// - /// Returns a sample of the topics dataset. - /// - public static IEnumerable GetTopicsData() - { - var data = new List(); - data.Add(new SampleTopicsData { Review = "animals birds cats dogs fish horse", ReviewReverse = "radiation galaxy universe duck", Label = true }); - data.Add(new SampleTopicsData { Review = "horse birds house fish duck cats", ReviewReverse = "space galaxy universe radiation", Label = false }); - data.Add(new SampleTopicsData { Review = "car truck driver bus pickup", ReviewReverse = "bus pickup", Label = true }); - data.Add(new SampleTopicsData { Review = "car truck driver bus pickup horse", ReviewReverse = "car truck", Label = false }); - - return data; - } - - public class SampleTemperatureData - { - public DateTime Date { get; set; } - public float Temperature { get; set; } - } - - public class SampleTemperatureDataWithLatitude - { - public float Latitude { get; set; } - public DateTime Date { get; set; } - public float Temperature { get; set; } - } - - /// - /// Get a fake temperature dataset. - /// - /// The number of examples to return. - /// An enumerable of . - public static IEnumerable GetSampleTemperatureData(int exampleCount) - { - var rng = new Random(1234321); - var date = new DateTime(2012, 1, 1); - float temperature = 39.0f; - - for (int i = 0; i < exampleCount; i++) - { - date = date.AddDays(1); - temperature += rng.Next(-5, 5); - yield return new SampleTemperatureData { Date = date, Temperature = temperature }; - } - } - - /// - /// Represents the column of the infertility dataset. - /// - public class SampleInfertData - { - public int RowNum { get; set; } - public string Education { get; set; } - public float Age { get; set; } - public float Parity { get; set; } - public float Induced { get; set; } - public float Case { get; set; } - - public float Spontaneous { get; set; } - public float Stratum { get; set; } - public float PooledStratum { get; set; } - } - - /// - /// Returns a few rows of the infertility dataset. - /// - public static IEnumerable GetInfertData() - { - var data = new List(); - data.Add(new SampleInfertData - { - RowNum = 0, - Education = "0-5yrs", - Age = 26, - Parity = 6, - Induced = 1, - Case = 1, - Spontaneous = 2, - Stratum = 1, - PooledStratum = 3 - }); - data.Add(new SampleInfertData - { - RowNum = 1, - Education = "0-5yrs", - Age = 42, - Parity = 1, - Induced = 1, - Case = 1, - Spontaneous = 0, - Stratum = 2, - PooledStratum = 1 - }); - data.Add(new SampleInfertData - { - RowNum = 2, - Education = "12+yrs", - Age = 39, - Parity = 6, - Induced = 2, - Case = 1, - Spontaneous = 0, - Stratum = 3, - PooledStratum = 4 - }); - data.Add(new SampleInfertData - { - RowNum = 3, - Education = "0-5yrs", - Age = 34, - Parity = 4, - Induced = 2, - Case = 1, - Spontaneous = 0, - Stratum = 4, - PooledStratum = 2 - }); - data.Add(new SampleInfertData - { - RowNum = 4, - Education = "6-11yrs", - Age = 35, - Parity = 3, - Induced = 1, - Case = 1, - Spontaneous = 1, - Stratum = 5, - PooledStratum = 32 - }); - return data; - } - - public class SampleVectorOfNumbersData - { - [VectorType(10)] - - public float[] Features { get; set; } - } - - /// - /// Returns a few rows of the infertility dataset. - /// - public static IEnumerable GetVectorOfNumbersData() - { - var data = new List(); - data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } }); - data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } }); - data.Add(new SampleVectorOfNumbersData - { - Features = new float[10] { 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 } - }); - data.Add(new SampleVectorOfNumbersData - { - Features = new float[10] { 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, } - }); - data.Add(new SampleVectorOfNumbersData - { - Features = new float[10] { 5, 6, 7, 8, 9, 0, 1, 2, 3, 4 } - }); - data.Add(new SampleVectorOfNumbersData - { - Features = new float[10] { 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 } - }); - return data; - } - private const int _simpleBinaryClassSampleFeatureLength = 10; /// @@ -607,61 +286,6 @@ public static IEnumerable GenerateFloatLabel return data; } - public class FfmExample - { - public bool Label; - - [VectorType(_simpleBinaryClassSampleFeatureLength)] - public float[] Field0; - - [VectorType(_simpleBinaryClassSampleFeatureLength)] - public float[] Field1; - - [VectorType(_simpleBinaryClassSampleFeatureLength)] - public float[] Field2; - } - - public static IEnumerable GenerateFfmSamples(int exampleCount) - { - var rnd = new Random(0); - var data = new List(); - for (int i = 0; i < exampleCount; ++i) - { - // Initialize an example with a random label and an empty feature vector. - var sample = new FfmExample() - { - Label = rnd.Next() % 2 == 0, - Field0 = new float[_simpleBinaryClassSampleFeatureLength], - Field1 = new float[_simpleBinaryClassSampleFeatureLength], - Field2 = new float[_simpleBinaryClassSampleFeatureLength] - }; - // Fill feature vector according the assigned label. - for (int j = 0; j < 10; ++j) - { - var value0 = (float)rnd.NextDouble(); - // Positive class gets larger feature value. - if (sample.Label) - value0 += 0.2f; - sample.Field0[j] = value0; - - var value1 = (float)rnd.NextDouble(); - // Positive class gets smaller feature value. - if (sample.Label) - value1 -= 0.2f; - sample.Field1[j] = value1; - - var value2 = (float)rnd.NextDouble(); - // Positive class gets larger feature value. - if (sample.Label) - value2 += 0.8f; - sample.Field2[j] = value2; - } - - data.Add(sample); - } - return data; - } - /// /// feature vector's length in . /// @@ -724,49 +348,5 @@ public static List GenerateRandomMulticlassClas } return examples; } - - // The following variables defines the shape of a matrix. Its shape is _synthesizedMatrixRowCount-by-_synthesizedMatrixColumnCount. - // Because in ML.NET key type's minimal value is zero, the first row index is always zero in C# data structure (e.g., MatrixColumnIndex=0 - // and MatrixRowIndex=0 in MatrixElement below specifies the value at the upper-left corner in the training matrix). If user's row index - // starts with 1, their row index 1 would be mapped to the 2nd row in matrix factorization module and their first row may contain no values. - // This behavior is also true to column index. - private const uint _synthesizedMatrixFirstColumnIndex = 1; - private const uint _synthesizedMatrixFirstRowIndex = 1; - private const uint _synthesizedMatrixColumnCount = 60; - private const uint _synthesizedMatrixRowCount = 100; - - // A data structure used to encode a single value in matrix - public class MatrixElement - { - // Matrix column index is at most _synthesizedMatrixColumnCount + _synthesizedMatrixFirstColumnIndex. - [KeyType(_synthesizedMatrixColumnCount + _synthesizedMatrixFirstColumnIndex)] - public uint MatrixColumnIndex; - // Matrix row index is at most _synthesizedMatrixRowCount + _synthesizedMatrixFirstRowIndex. - [KeyType(_synthesizedMatrixRowCount + _synthesizedMatrixFirstRowIndex)] - public uint MatrixRowIndex; - // The value at the column MatrixColumnIndex and row MatrixRowIndex. - public float Value; - } - - // A data structure used to encode prediction result. Comparing with MatrixElement, The field Value in MatrixElement is - // renamed to Score because Score is the default name of matrix factorization's output. - public class MatrixElementForScore - { - [KeyType(_synthesizedMatrixColumnCount + _synthesizedMatrixFirstColumnIndex)] - public uint MatrixColumnIndex; - [KeyType(_synthesizedMatrixRowCount + _synthesizedMatrixFirstRowIndex)] - public uint MatrixRowIndex; - public float Score; - } - - // Create an in-memory matrix as a list of tuples (column index, row index, value). - public static List GetRecommendationData() - { - var dataMatrix = new List(); - for (uint i = _synthesizedMatrixFirstColumnIndex; i < _synthesizedMatrixFirstColumnIndex + _synthesizedMatrixColumnCount; ++i) - for (uint j = _synthesizedMatrixFirstRowIndex; j < _synthesizedMatrixFirstRowIndex + _synthesizedMatrixRowCount; ++j) - dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i, MatrixRowIndex = j, Value = (i + j) % 5 }); - return dataMatrix; - } } } diff --git a/test/Microsoft.ML.Tests/TrainerEstimators/FAFMEstimator.cs b/test/Microsoft.ML.Tests/TrainerEstimators/FAFMEstimator.cs index 9449112605..83f1bd374c 100644 --- a/test/Microsoft.ML.Tests/TrainerEstimators/FAFMEstimator.cs +++ b/test/Microsoft.ML.Tests/TrainerEstimators/FAFMEstimator.cs @@ -2,9 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using System.Collections.Generic; using Microsoft.ML.Data; using Microsoft.ML.RunTests; -using Microsoft.ML.SamplesUtils; using Microsoft.ML.Trainers; using Xunit; @@ -16,10 +17,10 @@ public partial class TrainerEstimators : TestDataPipeBase public void FfmBinaryClassificationWithoutArguments() { var mlContext = new MLContext(seed: 0); - var data = DatasetUtils.GenerateFfmSamples(500); + var data = GenerateFfmSamples(500); var dataView = mlContext.Data.LoadFromEnumerable(data); - var pipeline = mlContext.Transforms.CopyColumns(DefaultColumnNames.Features, nameof(DatasetUtils.FfmExample.Field0)) + var pipeline = mlContext.Transforms.CopyColumns(DefaultColumnNames.Features, nameof(FfmExample.Field0)) .Append(mlContext.BinaryClassification.Trainers.FieldAwareFactorizationMachine()); var model = pipeline.Fit(dataView); @@ -37,14 +38,14 @@ public void FfmBinaryClassificationWithoutArguments() public void FfmBinaryClassificationWithAdvancedArguments() { var mlContext = new MLContext(seed: 0); - var data = DatasetUtils.GenerateFfmSamples(500); + var data = GenerateFfmSamples(500); var dataView = mlContext.Data.LoadFromEnumerable(data); var ffmArgs = new FieldAwareFactorizationMachineTrainer.Options(); // Customized the field names. - ffmArgs.FeatureColumnName = nameof(DatasetUtils.FfmExample.Field0); // First field. - ffmArgs.ExtraFeatureColumns = new[]{ nameof(DatasetUtils.FfmExample.Field1), nameof(DatasetUtils.FfmExample.Field2) }; + ffmArgs.FeatureColumnName = nameof(FfmExample.Field0); // First field. + ffmArgs.ExtraFeatureColumns = new[] { nameof(FfmExample.Field1), nameof(FfmExample.Field2) }; var pipeline = mlContext.BinaryClassification.Trainers.FieldAwareFactorizationMachine(ffmArgs); @@ -65,9 +66,10 @@ public void FieldAwareFactorizationMachine_Estimator() var data = new TextLoader(Env, GetFafmBCLoaderArgs()) .Load(GetDataPath(TestDatasets.breastCancer.trainFilename)); - var ffmArgs = new FieldAwareFactorizationMachineTrainer.Options { + var ffmArgs = new FieldAwareFactorizationMachineTrainer.Options + { FeatureColumnName = "Feature1", // Features from the 1st field. - ExtraFeatureColumns = new[] { "Feature2", "Feature3", "Feature4" }, // 2nd field's feature column, 3rd field's feature column, 4th field's feature column. + ExtraFeatureColumns = new[] { "Feature2", "Feature3", "Feature4" }, // 2nd field's feature column, 3rd field's feature column, 4th field's feature column. Shuffle = false, NumberOfIterations = 3, LatentDimension = 7, @@ -98,5 +100,62 @@ private TextLoader.Options GetFafmBCLoaderArgs() } }; } + + private const int _simpleBinaryClassSampleFeatureLength = 10; + + private class FfmExample + { + public bool Label; + + [VectorType(_simpleBinaryClassSampleFeatureLength)] + public float[] Field0; + + [VectorType(_simpleBinaryClassSampleFeatureLength)] + public float[] Field1; + + [VectorType(_simpleBinaryClassSampleFeatureLength)] + public float[] Field2; + } + + private static IEnumerable GenerateFfmSamples(int exampleCount) + { + var rnd = new Random(0); + var data = new List(); + for (int i = 0; i < exampleCount; ++i) + { + // Initialize an example with a random label and an empty feature vector. + var sample = new FfmExample() + { + Label = rnd.Next() % 2 == 0, + Field0 = new float[_simpleBinaryClassSampleFeatureLength], + Field1 = new float[_simpleBinaryClassSampleFeatureLength], + Field2 = new float[_simpleBinaryClassSampleFeatureLength] + }; + // Fill feature vector according the assigned label. + for (int j = 0; j < 10; ++j) + { + var value0 = (float)rnd.NextDouble(); + // Positive class gets larger feature value. + if (sample.Label) + value0 += 0.2f; + sample.Field0[j] = value0; + + var value1 = (float)rnd.NextDouble(); + // Positive class gets smaller feature value. + if (sample.Label) + value1 -= 0.2f; + sample.Field1[j] = value1; + + var value2 = (float)rnd.NextDouble(); + // Positive class gets larger feature value. + if (sample.Label) + value2 += 0.8f; + sample.Field2[j] = value2; + } + + data.Add(sample); + } + return data; + } } } From 936c406d51992cd9855aa907aa15be6f797079c3 Mon Sep 17 00:00:00 2001 From: Wei-Sheng Chin Date: Fri, 26 Apr 2019 08:48:14 -0700 Subject: [PATCH 2/2] Address comments --- .../Dynamic/NgramExtraction.cs | 19 ++++++------------ .../Dynamic/TextTransform.cs | 20 +++++++------------ 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs index e625fba9db..1fe2f70325 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs @@ -14,7 +14,12 @@ public static void Example() var ml = new MLContext(); // Get a small dataset as an IEnumerable and convert to IDataView. - IEnumerable data = GetSentimentData(); + var data = new List() { + new SampleSentimentData { Sentiment = true, SentimentText = "Best game I've ever played." }, + new SampleSentimentData { Sentiment = false, SentimentText = "==RUDE== Dude, 2" }, + new SampleSentimentData { Sentiment = true, SentimentText = "Until the next game, this is the best Xbox game!" } }; + + // Convert IEnumerable to IDataView. var trainData = ml.Data.LoadFromEnumerable(data); // Preview of the data. @@ -80,17 +85,5 @@ public class SampleSentimentData public bool Sentiment { get; set; } public string SentimentText { get; set; } } - - /// - /// Returns a sample of the sentiment dataset. - /// - public static IEnumerable GetSentimentData() - { - var data = new List(); - data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Best game I've ever played." }); - data.Add(new SampleSentimentData { Sentiment = false, SentimentText = "==RUDE== Dude, 2" }); - data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Until the next game, this is the best Xbox game!" }); - return data; - } } } diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs index 173e6bdc55..5c49ac6bbd 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs @@ -15,7 +15,13 @@ public static void Example() var ml = new MLContext(); // Get a small dataset as an IEnumerable and convert to IDataView. - var data = GetSentimentData(); + // Get a small dataset as an IEnumerable and convert to IDataView. + var data = new List() { + new SampleSentimentData { Sentiment = true, SentimentText = "Best game I've ever played." }, + new SampleSentimentData { Sentiment = false, SentimentText = "==RUDE== Dude, 2" }, + new SampleSentimentData { Sentiment = true, SentimentText = "Until the next game, this is the best Xbox game!" } }; + + // Convert IEnumerable to IDataView. var trainData = ml.Data.LoadFromEnumerable(data); // Preview of the data. @@ -87,17 +93,5 @@ public class SampleSentimentData public bool Sentiment { get; set; } public string SentimentText { get; set; } } - - /// - /// Returns a sample of the sentiment dataset. - /// - public static IEnumerable GetSentimentData() - { - var data = new List(); - data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Best game I've ever played." }); - data.Add(new SampleSentimentData { Sentiment = false, SentimentText = "==RUDE== Dude, 2" }); - data.Add(new SampleSentimentData { Sentiment = true, SentimentText = "Until the next game, this is the best Xbox game!" }); - return data; - } } }