Skip to content

Commit 1a89468

Browse files
authored
Clean FeatureContributionCalculation and PermutationFeatureImportance (#2966)
1 parent 0831865 commit 1a89468

File tree

9 files changed

+171
-141
lines changed

9 files changed

+171
-141
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/FeatureContributionCalculationTransform.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public static void Example()
3535
// Create a Feature Contribution Calculator
3636
// Calculate the feature contributions for all features given trained model parameters
3737
// And don't normalize the contribution scores
38-
var featureContributionCalculator = mlContext.Model.Explainability.FeatureContributionCalculation(model.Model, model.FeatureColumnName, numPositiveContributions: 11, normalize: false);
38+
var featureContributionCalculator = mlContext.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 11, normalize: false);
3939
var outputData = featureContributionCalculator.Fit(scoredData).Transform(scoredData);
4040

4141
// FeatureContributionCalculatingEstimator can be use as an intermediary step in a pipeline.
4242
// The features retained by FeatureContributionCalculatingEstimator will be in the FeatureContribution column.
43-
var pipeline = mlContext.Model.Explainability.FeatureContributionCalculation(model.Model, model.FeatureColumnName, numPositiveContributions: 11)
43+
var pipeline = mlContext.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 11)
4444
.Append(mlContext.Regression.Trainers.Ols(featureColumnName: "FeatureContributions"));
4545
var outData = featureContributionCalculator.Fit(scoredData).Transform(scoredData);
4646

docs/samples/Microsoft.ML.Samples/Dynamic/PermutationFeatureImportance/PFIRegressionExample.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void Example()
3131
// Compute the permutation metrics using the properly normalized data.
3232
var transformedData = model.Transform(data);
3333
var permutationMetrics = mlContext.Regression.PermutationFeatureImportance(
34-
linearPredictor, transformedData, label: labelName, features: "Features", permutationCount: 3);
34+
linearPredictor, transformedData, labelColumnName: labelName, permutationCount: 3);
3535

3636
// Now let's look at which features are most important to the model overall
3737
// Get the feature indices sorted by their impact on R-Squared

docs/samples/Microsoft.ML.Samples/Dynamic/PermutationFeatureImportance/PfiBinaryClassificationExample.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void Example()
3535
// Compute the permutation metrics using the properly normalized data.
3636
var transformedData = model.Transform(data);
3737
var permutationMetrics = mlContext.BinaryClassification.PermutationFeatureImportance(
38-
linearPredictor, transformedData, label: labelName, features: "Features", permutationCount: 3);
38+
linearPredictor, transformedData, labelColumnName: labelName, permutationCount: 3);
3939

4040
// Now let's look at which features are most important to the model overall.
4141
// Get the feature indices sorted by their impact on AreaUnderRocCurve.

src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs

-18
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ public sealed class ModelOperationsCatalog : IInternalCatalog
2222
IHostEnvironment IInternalCatalog.Environment => _env;
2323
private readonly IHostEnvironment _env;
2424

25-
public ExplainabilityTransforms Explainability { get; }
26-
2725
internal ModelOperationsCatalog(IHostEnvironment env)
2826
{
2927
Contracts.AssertValue(env);
3028
_env = env;
31-
32-
Explainability = new ExplainabilityTransforms(this);
3329
}
3430

3531
/// <summary>
@@ -228,20 +224,6 @@ public ITransformer LoadWithDataLoader(Stream stream, out IDataLoader<IMultiStre
228224
return new TransformerChain<ITransformer>();
229225
}
230226

231-
/// <summary>
232-
/// The catalog of model explainability operations.
233-
/// </summary>
234-
public sealed class ExplainabilityTransforms : IInternalCatalog
235-
{
236-
IHostEnvironment IInternalCatalog.Environment => _env;
237-
private readonly IHostEnvironment _env;
238-
239-
internal ExplainabilityTransforms(ModelOperationsCatalog owner)
240-
{
241-
_env = owner._env;
242-
}
243-
}
244-
245227
/// <summary>
246228
/// Create a prediction engine for one-time prediction.
247229
/// </summary>

src/Microsoft.ML.Data/Transforms/ExplainabilityCatalog.cs

+47-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Calibrators;
56
using Microsoft.ML.Data;
67
using Microsoft.ML.Trainers;
78
using Microsoft.ML.Transforms;
@@ -17,19 +18,53 @@ public static class ExplainabilityCatalog
1718
/// Note that this functionality is not supported by all the models. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported models.
1819
/// </summary>
1920
/// <param name="catalog">The model explainability operations catalog.</param>
20-
/// <param name="modelParameters">Trained model parameters that support Feature Contribution Calculation and which will be used for scoring.</param>
21-
/// <param name="featureColumnName">The name of the feature column that will be used as input.</param>
22-
/// <param name="numPositiveContributions">The number of positive contributions to report, sorted from highest magnitude to lowest magnitude.
23-
/// Note that if there are fewer features with positive contributions than <paramref name="numPositiveContributions"/>, the rest will be returned as zeros.</param>
24-
/// <param name="numNegativeContributions">The number of negative contributions to report, sorted from highest magnitude to lowest magnitude.
25-
/// Note that if there are fewer features with negative contributions than <paramref name="numNegativeContributions"/>, the rest will be returned as zeros.</param>
21+
/// <param name="predictionTransformer">A <see cref="ISingleFeaturePredictionTransformer{TModel}"/> that supports Feature Contribution Calculation,
22+
/// and which will also be used for scoring.</param>
23+
/// <param name="numberOfPositiveContributions">The number of positive contributions to report, sorted from highest magnitude to lowest magnitude.
24+
/// Note that if there are fewer features with positive contributions than <paramref name="numberOfPositiveContributions"/>, the rest will be returned as zeros.</param>
25+
/// <param name="numberOfNegativeContributions">The number of negative contributions to report, sorted from highest magnitude to lowest magnitude.
26+
/// Note that if there are fewer features with negative contributions than <paramref name="numberOfNegativeContributions"/>, the rest will be returned as zeros.</param>
2627
/// <param name="normalize">Whether the feature contributions should be normalized to the [-1, 1] interval.</param>
27-
public static FeatureContributionCalculatingEstimator FeatureContributionCalculation(this ModelOperationsCatalog.ExplainabilityTransforms catalog,
28-
ICalculateFeatureContribution modelParameters,
29-
string featureColumnName = DefaultColumnNames.Features,
30-
int numPositiveContributions = FeatureContributionDefaults.NumPositiveContributions,
31-
int numNegativeContributions = FeatureContributionDefaults.NumNegativeContributions,
28+
/// <example>
29+
/// <format type="text/markdown">
30+
/// <![CDATA[
31+
/// [!code-csharp[FCT](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/FeatureContributionCalculationTransform.cs)]
32+
/// ]]>
33+
/// </format>
34+
/// </example>
35+
public static FeatureContributionCalculatingEstimator CalculateFeatureContribution(this TransformsCatalog catalog,
36+
ISingleFeaturePredictionTransformer<ICalculateFeatureContribution> predictionTransformer,
37+
int numberOfPositiveContributions = FeatureContributionDefaults.NumberOfPositiveContributions,
38+
int numberOfNegativeContributions = FeatureContributionDefaults.NumberOfNegativeContributions,
3239
bool normalize = FeatureContributionDefaults.Normalize)
33-
=> new FeatureContributionCalculatingEstimator(CatalogUtils.GetEnvironment(catalog), modelParameters, featureColumnName, numPositiveContributions, numNegativeContributions, normalize);
40+
=> new FeatureContributionCalculatingEstimator(CatalogUtils.GetEnvironment(catalog), predictionTransformer.Model, numberOfPositiveContributions, numberOfNegativeContributions, predictionTransformer.FeatureColumnName, normalize);
41+
42+
/// <summary>
43+
/// Feature Contribution Calculation computes model-specific contribution scores for each feature.
44+
/// Note that this functionality is not supported by all the models. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported models.
45+
/// </summary>
46+
/// <param name="catalog">The model explainability operations catalog.</param>
47+
/// <param name="predictionTransformer">A <see cref="ISingleFeaturePredictionTransformer{TModel}"/> that supports Feature Contribution Calculation,
48+
/// and which will also be used for scoring.</param>
49+
/// <param name="numberOfPositiveContributions">The number of positive contributions to report, sorted from highest magnitude to lowest magnitude.
50+
/// Note that if there are fewer features with positive contributions than <paramref name="numberOfPositiveContributions"/>, the rest will be returned as zeros.</param>
51+
/// <param name="numberOfNegativeContributions">The number of negative contributions to report, sorted from highest magnitude to lowest magnitude.
52+
/// Note that if there are fewer features with negative contributions than <paramref name="numberOfNegativeContributions"/>, the rest will be returned as zeros.</param>
53+
/// <param name="normalize">Whether the feature contributions should be normalized to the [-1, 1] interval.</param>
54+
/// <example>
55+
/// <format type="text/markdown">
56+
/// <![CDATA[
57+
/// [!code-csharp[FCT](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/FeatureContributionCalculationTransform.cs)]
58+
/// ]]>
59+
/// </format>
60+
/// </example>
61+
public static FeatureContributionCalculatingEstimator CalculateFeatureContribution<TModelParameters, TCalibrator>(this TransformsCatalog catalog,
62+
ISingleFeaturePredictionTransformer<CalibratedModelParametersBase<TModelParameters, TCalibrator>> predictionTransformer,
63+
int numberOfPositiveContributions = FeatureContributionDefaults.NumberOfPositiveContributions,
64+
int numberOfNegativeContributions = FeatureContributionDefaults.NumberOfNegativeContributions,
65+
bool normalize = FeatureContributionDefaults.Normalize)
66+
where TModelParameters : class, ICalculateFeatureContribution
67+
where TCalibrator : class, ICalibrator
68+
=> new FeatureContributionCalculatingEstimator(CatalogUtils.GetEnvironment(catalog), predictionTransformer.Model.SubModel, numberOfPositiveContributions, numberOfNegativeContributions, predictionTransformer.FeatureColumnName, normalize);
3469
}
3570
}

0 commit comments

Comments
 (0)