Skip to content

Commit 8b5f817

Browse files
committed
minor changes
1 parent 7bdb5b7 commit 8b5f817

File tree

5 files changed

+27
-41
lines changed

5 files changed

+27
-41
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,21 @@ public static void FeatureContributionCalculationTransform_Regression()
5353

5454
var transformedData = transformPipeline.Fit(data).Transform(data);
5555

56+
// Now we train the model and score it on the transformed data.
5657
var model = learner.Fit(transformedData);
58+
var scoredData = model.Transform(transformedData);
5759

5860
// Create a Feature Contribution Calculator
59-
// Calculate the feature contributions for all features
61+
// Calculate the feature contributions for all features given trained model parameters
6062
// And don't normalize the contribution scores
6163
var featureContributionCalculator = mlContext.Model.Explainability.FeatureContributionCalculation(model.Model, model.FeatureColumn, top: 11, normalize: false);
62-
var outputData = featureContributionCalculator.Fit(transformedData).Transform(transformedData);
64+
var outputData = featureContributionCalculator.Fit(scoredData).Transform(scoredData);
6365

6466
// FeatureContributionCalculatingEstimator can be use as an intermediary step in a pipeline.
6567
// The features retained by FeatureContributionCalculatingEstimator will be in the FeatureContribution column.
6668
var pipeline = mlContext.Model.Explainability.FeatureContributionCalculation(model.Model, model.FeatureColumn, top: 11)
6769
.Append(mlContext.Regression.Trainers.OrdinaryLeastSquares(featureColumn: "FeatureContributions"));
68-
var outData = featureContributionCalculator.Fit(transformedData).Transform(transformedData);
70+
var outData = featureContributionCalculator.Fit(scoredData).Transform(scoredData);
6971

7072
// Let's extract the weights from the linear model to use as a comparison
7173
var weights = new VBuffer<float>();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ public static class ExplainabilityCatalog
1515
{
1616
/// <summary>
1717
/// Feature Contribution Calculation computes model-specific contribution scores for each feature.
18-
/// Note that this functionality is not supported by all the predictors. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported predictors.
18+
/// Note that this functionality is not supported by all the models. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported models.
1919
/// </summary>
2020
/// <param name="catalog">The model explainability operations catalog.</param>
21-
/// <param name="predictor">Trained model parameters that support Feature Contribution Calculation and which will be used for scoring.</param>
21+
/// <param name="modelParameters">Trained model parameters that support Feature Contribution Calculation and which will be used for scoring.</param>
2222
/// <param name="featureColumn">The name of the feature column that will be used as input.</param>
2323
/// <param name="top">The number of features with highest positive contributions for each data sample that will be retained in the FeatureContribution column.
2424
/// Note that if there are fewer features with positive contributions than <paramref name="top"/>, the rest will be returned as zeros.</param>
2525
/// <param name="bottom">The number of features with least negative contributions for each data sample that will be retained in the FeatureContribution column.
2626
/// Note that if there are fewer features with negative contributions than <paramref name="bottom"/>, the rest will be returned as zeros.</param>
2727
/// <param name="normalize">Whether the feature contributions should be normalized to the [-1, 1] interval.</param>
2828
public static FeatureContributionCalculatingEstimator FeatureContributionCalculation(this ModelOperationsCatalog.ExplainabilityTransforms catalog,
29-
ICalculateFeatureContribution predictor,
29+
ICalculateFeatureContribution modelParameters,
3030
string featureColumn = DefaultColumnNames.Features,
3131
int top = FeatureContributionDefaults.Top,
3232
int bottom = FeatureContributionDefaults.Bottom,
3333
bool normalize = FeatureContributionDefaults.Normalize)
34-
=> new FeatureContributionCalculatingEstimator(CatalogUtils.GetEnvironment(catalog), predictor, featureColumn, top, bottom, normalize);
34+
=> new FeatureContributionCalculatingEstimator(CatalogUtils.GetEnvironment(catalog), modelParameters, featureColumn, top, bottom, normalize);
3535
}
3636
}

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

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ namespace Microsoft.ML.Runtime.Data
2828
{
2929
/// <summary>
3030
/// The FeatureContributionCalculationTransformer computes model-specific contribution scores for each feature.
31-
/// See the list of currently supported predictors below.
31+
/// See the list of currently supported models below.
3232
/// </summary>
3333
/// <remarks>
34-
/// Feature Contribution Calculation is currently supported for the following Predictors:
34+
/// Feature Contribution Calculation is currently supported for the following models:
3535
/// Regression:
3636
/// OrdinaryLeastSquares, StochasticDualCoordinateAscent (SDCA), OnlineGradientDescent, PoissonRegression,
3737
/// GeneralizedAdditiveModels (GAM), LightGbm, FastTree, FastForest, FastTreeTweedie
@@ -95,24 +95,24 @@ private static VersionInfo GetVersionInfo()
9595

9696
/// <summary>
9797
/// Feature Contribution Calculation computes model-specific contribution scores for each feature.
98-
/// Note that this functionality is not supported by all the predictors. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported predictors.
98+
/// Note that this functionality is not supported by all the models. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported models.
9999
/// </summary>
100100
/// <param name="env">The environment to use.</param>
101-
/// <param name="predictor">Trained model parameters that support Feature Contribution Calculation and which will be used for scoring.</param>
101+
/// <param name="modelParameters">Trained model parameters that support Feature Contribution Calculation and which will be used for scoring.</param>
102102
/// <param name="featureColumn">The name of the feature column that will be used as input.</param>
103103
/// <param name="top">The number of features with highest positive contributions for each data sample that will be retained in the FeatureContribution column.
104104
/// Note that if there are fewer features with positive contributions than <paramref name="top"/>, the rest will be returned as zeros.</param>
105105
/// <param name="bottom">The number of features with least negative contributions for each data sample that will be retained in the FeatureContribution column.
106106
/// Note that if there are fewer features with negative contributions than <paramref name="bottom"/>, the rest will be returned as zeros.</param>
107107
/// <param name="normalize">Whether the feature contributions should be normalized to the [-1, 1] interval.</param>
108-
public FeatureContributionCalculatingTransformer(IHostEnvironment env, ICalculateFeatureContribution predictor,
108+
public FeatureContributionCalculatingTransformer(IHostEnvironment env, ICalculateFeatureContribution modelParameters,
109109
string featureColumn = DefaultColumnNames.Features,
110110
int top = FeatureContributionCalculatingEstimator.Defaults.Top,
111111
int bottom = FeatureContributionCalculatingEstimator.Defaults.Bottom,
112112
bool normalize = FeatureContributionCalculatingEstimator.Defaults.Normalize)
113113
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(FeatureContributionCalculatingTransformer)), new[] { (input: featureColumn, output: DefaultColumnNames.FeatureContributions) })
114114
{
115-
Host.CheckValue(predictor, nameof(predictor));
115+
Host.CheckValue(modelParameters, nameof(modelParameters));
116116
Host.CheckNonEmpty(featureColumn, nameof(featureColumn));
117117
if (top < 0)
118118
throw Host.Except($"Number of top contribution must be non negative");
@@ -121,7 +121,7 @@ public FeatureContributionCalculatingTransformer(IHostEnvironment env, ICalculat
121121

122122
// If a predictor implements ICalculateFeatureContribution, it also implements the internal interface IFeatureContributionMapper.
123123
// This is how we keep the implementation of feature contribution calculation internal.
124-
_predictor = predictor as IFeatureContributionMapper;
124+
_predictor = modelParameters as IFeatureContributionMapper;
125125
Host.AssertValue(_predictor);
126126

127127
Top = top;
@@ -227,10 +227,10 @@ protected override Delegate MakeGetter(Row input, int iinfo, Func<int, bool> act
227227

228228
// REVIEW: Assuming Feature contributions will be VBuffer<float>.
229229
// For multiclass LR it needs to be VBuffer<float>[].
230-
return Utils.MarshalInvoke(GetValueGetterVec<int>, _featureColumnType.RawType, input, ColMapNewToOld[iinfo]);
230+
return Utils.MarshalInvoke(GetValueGetter<int>, _featureColumnType.RawType, input, ColMapNewToOld[iinfo]);
231231
}
232232

233-
private Delegate GetValueGetterVec<TSrc>(Row input, int colSrc)
233+
private Delegate GetValueGetter<TSrc>(Row input, int colSrc)
234234
{
235235
Contracts.AssertValue(input);
236236
Contracts.AssertValue(_parent._predictor);
@@ -246,23 +246,6 @@ private Delegate GetValueGetterVec<TSrc>(Row input, int colSrc)
246246
map(in features, ref dst);
247247
});
248248
}
249-
250-
//private Delegate GetValueGetterVec<TSrc>(Row input, int colSrc)
251-
//{
252-
// Contracts.AssertValue(input);
253-
// Contracts.AssertValue(_parent._predictor);
254-
255-
// var featureGetter = input.GetGetter<VBuffer<TSrc>>(colSrc);
256-
257-
// var map = _parent._predictor.GetFeatureContributionMapper<VBuffer<TSrc>, VBuffer<float>>(_parent.Top, _parent.Bottom, _parent.Normalize);
258-
// var features = default(VBuffer<TSrc>);
259-
260-
// return (ValueGetter<VBuffer<float>>)((ref VBuffer<float> dst) =>
261-
// {
262-
// featureGetter(ref features);
263-
// map(in features, ref dst);
264-
// });
265-
//}
266249
}
267250
}
268251

@@ -284,26 +267,26 @@ public static class Defaults
284267

285268
/// <summary>
286269
/// Feature Contribution Calculation computes model-specific contribution scores for each feature.
287-
/// Note that this functionality is not supported by all the predictors. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported predictors.
270+
/// Note that this functionality is not supported by all the models. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported models.
288271
/// </summary>
289272
/// <param name="env">The environment to use.</param>
290-
/// <param name="predictor">Trained model parameters that support Feature Contribution Calculation and which will be used for scoring.</param>
273+
/// <param name="modelParameters">Trained model parameters that support Feature Contribution Calculation and which will be used for scoring.</param>
291274
/// <param name="featureColumn">The name of the feature column that will be used as input.</param>
292275
/// <param name="top">The number of features with highest positive contributions for each data sample that will be retained in the FeatureContribution column.
293276
/// Note that if there are fewer features with positive contributions than <paramref name="top"/>, the rest will be returned as zeros.</param>
294277
/// <param name="bottom">The number of features with least negative contributions for each data sample that will be retained in the FeatureContribution column.
295278
/// Note that if there are fewer features with negative contributions than <paramref name="bottom"/>, the rest will be returned as zeros.</param>
296279
/// <param name="normalize">Whether the feature contributions should be normalized to the [-1, 1] interval.</param>
297-
public FeatureContributionCalculatingEstimator(IHostEnvironment env, ICalculateFeatureContribution predictor,
280+
public FeatureContributionCalculatingEstimator(IHostEnvironment env, ICalculateFeatureContribution modelParameters,
298281
string featureColumn = DefaultColumnNames.Features,
299282
int top = Defaults.Top,
300283
int bottom = Defaults.Bottom,
301284
bool normalize = Defaults.Normalize)
302285
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(FeatureContributionCalculatingTransformer)),
303-
new FeatureContributionCalculatingTransformer(env, predictor, featureColumn, top, bottom, normalize))
286+
new FeatureContributionCalculatingTransformer(env, modelParameters, featureColumn, top, bottom, normalize))
304287
{
305288
_featureColumn = featureColumn;
306-
_predictor = predictor;
289+
_predictor = modelParameters;
307290
}
308291

309292
public override SchemaShape GetOutputSchema(SchemaShape inputSchema)
@@ -345,7 +328,7 @@ public static CommonOutputs.TransformOutput FeatureContributionCalculation(IHost
345328

346329
var predictor = args.PredictorModel.Predictor as ICalculateFeatureContribution;
347330
if (predictor == null)
348-
throw host.ExceptUserArg(nameof(predictor), "The provided predictor does not support feature contribution calculation.");
331+
throw host.ExceptUserArg(nameof(predictor), "The provided model parameters do not support feature contribution calculation.");
349332
var outData = new FeatureContributionCalculatingTransformer(host, predictor, args.FeatureColumn, args.Top, args.Bottom, args.Normalize).Transform(args.Data);
350333

351334
return new CommonOutputs.TransformOutput { Model = new TransformModelImpl(env, outData, args.Data), OutputData = outData};

test/BaselineOutput/Common/EntryPoints/core_manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18460,7 +18460,7 @@
1846018460
{
1846118461
"Name": "Transforms.FeatureContributionCalculationTransformer",
1846218462
"Desc": "For each data point, calculates the contribution of individual features to the model prediction.",
18463-
"FriendlyName": "Feature Contribution Transform",
18463+
"FriendlyName": "Feature Contribution Calculation",
1846418464
"ShortName": null,
1846518465
"Inputs": [
1846618466
{

test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ private string GetBuildPrefix()
249249
#endif
250250
}
251251

252-
[Fact(Skip = "Execute this test if you want to regenerate the core_manifest and core_ep_list files")]
252+
//[Fact(Skip = "Execute this test if you want to regenerate the core_manifest and core_ep_list files")]
253+
[Fact]
253254
public void RegenerateEntryPointCatalog()
254255
{
255256
var (epListContents, jObj) = BuildManifests();

0 commit comments

Comments
 (0)