Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatasetUtils.HousingRegression>(data, reuseRowObject: true);
var enumerable = mlContext.Data.CreateEnumerable<HousingRegression>(data, reuseRowObject: true);
var watch = System.Diagnostics.Stopwatch.StartNew();
foreach (var row in enumerable)
{
Expand All @@ -58,5 +58,25 @@ private static (int lines, double columnAverage, double elapsedSeconds) TimeToSc

return (lines, columnAverage, elapsed.Seconds);
}

/// <summary>
/// A class to hold the raw housing regression rows.
/// </summary>
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; }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Microsoft.ML.SamplesUtils.DatasetUtils.SampleTemperatureData>(filteredData, reuseRowObject: true);
var enumerable = mlContext.Data.CreateEnumerable<SampleTemperatureData>(filteredData, reuseRowObject: true);
Console.WriteLine($"Date\tTemperature");
foreach (var row in enumerable)
{
Expand Down
16 changes: 15 additions & 1 deletion docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public static void Example()
var ml = new MLContext();

// Get a small dataset as an IEnumerable and convert to IDataView.
IEnumerable<Microsoft.ML.SamplesUtils.DatasetUtils.SampleSentimentData> data = Microsoft.ML.SamplesUtils.DatasetUtils.GetSentimentData();
var data = new List<SampleSentimentData>() {
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.
Expand Down Expand Up @@ -71,5 +76,14 @@ 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 ...
}

/// <summary>
/// A dataset that contains a tweet and the sentiment assigned to that tweet: 0 - negative and 1 - positive sentiment.
/// </summary>
public class SampleSentimentData
{
public bool Sentiment { get; set; }
public string SentimentText { get; set; }
}
}
}
17 changes: 16 additions & 1 deletion docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = Microsoft.ML.SamplesUtils.DatasetUtils.GetSentimentData();
// Get a small dataset as an IEnumerable and convert to IDataView.
var data = new List<SampleSentimentData>() {
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.
Expand Down Expand Up @@ -78,5 +84,14 @@ 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 }
}

/// <summary>
/// A dataset that contains a tweet and the sentiment assigned to that tweet: 0 - negative and 1 - positive sentiment.
/// </summary>
public class SampleSentimentData
{
public bool Sentiment { get; set; }
public string SentimentText { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}");
}

@shmoradims shmoradims Apr 25, 2019

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for trainers, we should modify the .ttinclude template file, not the cs file directly. #Resolved

@wschin wschin Apr 25, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*Advanced.cs are not API samples. They are legacy end-to-end pipelines saved for future use. #Resolved

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<VBuffer<float>>(transformedData.Schema[nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features)]);
var whitening = transformedData.GetColumn<VBuffer<float>>(transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]);

printHelper(nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features), whitening);
printHelper(nameof(SampleVectorOfNumbersData.Features), whitening);

// Features column obtained post-transformation.
//
Expand All @@ -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; }
}

/// <summary>
/// Returns a few rows of the infertility dataset.
/// </summary>
private static IEnumerable<SampleVectorOfNumbersData> GetVectorOfNumbersData()
{
var data = new List<SampleVectorOfNumbersData>();
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 }
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar change. please create the data inline with initializers, similar to NgramExtraction.


In reply to: 278763353 [](ancestors = 278763353)

return data;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<VBuffer<float>>(transformedData.Schema[nameof(Microsoft.ML.SamplesUtils.DatasetUtils.SampleVectorOfNumbersData.Features)]);
var whitening = transformedData.GetColumn<VBuffer<float>>(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
Expand All @@ -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; }
}

/// <summary>
/// Returns a few rows of the infertility dataset.
/// </summary>
private static IEnumerable<SampleVectorOfNumbersData> GetVectorOfNumbersData()
{
var data = new List<SampleVectorOfNumbersData>();
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;
}
}
}
Loading