Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample for ReplaceMissingValues. #2773

Closed
wants to merge 3 commits into from
Closed
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
@@ -0,0 +1,38 @@
using Microsoft.ML.Data;
using Microsoft.ML.SamplesUtils;

namespace Microsoft.ML.Samples.Dynamic
{
public static class ReplaceMissingValues
{
public static void Example()
{
// Creating the ML.Net IHostEnvironment object, needed for the pipeline.
Copy link
Contributor

@zeahmed zeahmed Feb 27, 2019

Choose a reason for hiding this comment

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

// Creating the ML.Net IHostEnvironment object, needed for the pipeline. [](start = 12, length = 72)

Following is the standard comments for the MLContext in the samples.
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, // as well as the source of randomness.

Please see other samples in the folder.
#Resolved

var mlContext = new MLContext();

// Download the training and validation files.
string dataFile = DatasetUtils.DownloadMslrWeb10k();
Copy link
Contributor

@zeahmed zeahmed Feb 27, 2019

Choose a reason for hiding this comment

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

DownloadMslrWeb10k [](start = 43, length = 18)

You can use the LoadFeaturizedMslrWeb10kDataset instead which will save you creating the data processing step. This is the standard in the samples. #Resolved

Copy link
Member

@wschin wschin Feb 28, 2019

Choose a reason for hiding this comment

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

My feeling is that we should use in-memory data when possible. In, #2726, we discuss some of in-memory data's advantages. #Resolved


// 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);
Copy link
Member

Choose a reason for hiding this comment

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

data [](start = 16, length = 4)

show a preview of before the transformation too.


// 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);
Copy link
Contributor

Choose a reason for hiding this comment

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

transformedData [](start = 16, length = 15)

Can you show some output after this line? e.g. the data preview or any metrics etc.

Copy link
Member

Choose a reason for hiding this comment

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

+1. Please also show how to inspect the prediction result example-by-example.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.ML.Data;
using Microsoft.ML.SamplesUtils;
using Microsoft.ML.Transforms;

namespace Microsoft.ML.Samples.Dynamic
{
public static class ReplaceMissingValuesColumnOptions
Copy link

@shmoradims shmoradims Mar 1, 2019

Choose a reason for hiding this comment

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

ReplaceMissingValuesColumnOptions [](start = 24, length = 33)

Please rename file and class to ReplaceMissingValuesWithOptions to match other samples (although this particular one doesn't sound ideal) #Resolved

{
public static void Example()
{
// Creating the ML.Net IHostEnvironment object, needed for the pipeline.
var mlContext = new MLContext();

// Download the training and validation files.
string dataFile = DatasetUtils.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(new MissingValueReplacingEstimator.ColumnOptions("Features", "Features", MissingValueReplacingEstimator.ColumnOptions.ReplacementMode.Mean)));

Choose a reason for hiding this comment

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

MissingValueReplacingEstimator [](start = 70, length = 30)

please showcase that with column option two or more columns can be processed at the same time. you can have two columns and process on with Mean and the other with Minimum.

also update the comment line above this line.


// Fit the pipeline and transform the dataset.
var transformedData = pipeline.Fit(data).Transform(data);
Copy link
Member

Choose a reason for hiding this comment

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

Please at least print out the columns affected before/after this transformation.

Copy link
Member

Choose a reason for hiding this comment

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

+1


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

}
}
}
14 changes: 14 additions & 0 deletions src/Microsoft.ML.Transforms/ExtensionsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public static MissingValueIndicatorEstimator IndicateMissingValues(this Transfor
/// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.
/// If not provided, the <paramref name="inputColumnName"/> will be replaced with the results of the transforms.</param>
/// <param name="replacementKind">The type of replacement to use as specified in <see cref="MissingValueReplacingEstimator.ColumnOptions.ReplacementMode"/></param>
/// <example>
/// <format type="text/markdown">
/// <![CDATA[
/// [!code-csharp[ReplaceMissingValues](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ReplaceMissingValues.cs)]
/// ]]>
/// </format>
/// </example>
public static MissingValueReplacingEstimator ReplaceMissingValues(this TransformsCatalog catalog,
string outputColumnName,
string inputColumnName = null,
Expand All @@ -58,6 +65,13 @@ public static MissingValueReplacingEstimator ReplaceMissingValues(this Transform
/// </summary>
/// <param name="catalog">The transform extensions' catalog.</param>
/// <param name="columns">The name of the columns to use, and per-column transformation configuraiton.</param>
/// <example>
/// <format type="text/markdown">
/// <![CDATA[
/// [!code-csharp[ReplaceMissingValuesColumnOptions](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ReplaceMissingValuesColumnOptions.cs)]
/// ]]>
/// </format>
/// </example>
public static MissingValueReplacingEstimator ReplaceMissingValues(this TransformsCatalog catalog, params MissingValueReplacingEstimator.ColumnOptions[] columns)
=> new MissingValueReplacingEstimator(CatalogUtils.GetEnvironment(catalog), columns);
}
Expand Down