Skip to content

Commit b9a0b07

Browse files
authored
Towards #3204 - documentation for FeatureContributionCalculatingEstimator (#3384)
* Documentation for FeatureContributionEstimator * Address code review comments * Address code review comments
1 parent a48dcef commit b9a0b07

File tree

2 files changed

+67
-48
lines changed

2 files changed

+67
-48
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ namespace Microsoft.ML
1414
public static class ExplainabilityCatalog
1515
{
1616
/// <summary>
17-
/// Feature Contribution Calculation computes model-specific contribution scores for each feature.
18-
/// Note that this functionality is not supported by all the models. See <see cref="FeatureContributionCalculatingTransformer"/> for a list of the suported models.
17+
/// Create a <see cref="FeatureContributionCalculatingEstimator"/> that computes model-specific contribution scores for
18+
/// each feature of the input vector.
1919
/// </summary>
20-
/// <param name="catalog">The model explainability operations catalog.</param>
20+
/// <param name="catalog">The transforms catalog.</param>
2121
/// <param name="predictionTransformer">A <see cref="ISingleFeaturePredictionTransformer{TModel}"/> that supports Feature Contribution Calculation,
2222
/// and which will also be used for scoring.</param>
2323
/// <param name="numberOfPositiveContributions">The number of positive contributions to report, sorted from highest magnitude to lowest magnitude.
@@ -40,10 +40,10 @@ public static FeatureContributionCalculatingEstimator CalculateFeatureContributi
4040
=> new FeatureContributionCalculatingEstimator(CatalogUtils.GetEnvironment(catalog), predictionTransformer.Model, numberOfPositiveContributions, numberOfNegativeContributions, predictionTransformer.FeatureColumnName, normalize);
4141

4242
/// <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.
43+
/// Create a <see cref="FeatureContributionCalculatingEstimator"/> that computes model-specific contribution scores for
44+
/// each feature of the input vector.
4545
/// </summary>
46-
/// <param name="catalog">The model explainability operations catalog.</param>
46+
/// <param name="catalog">The transforms catalog.</param>
4747
/// <param name="predictionTransformer">A <see cref="ISingleFeaturePredictionTransformer{TModel}"/> that supports Feature Contribution Calculation,
4848
/// and which will also be used for scoring.</param>
4949
/// <param name="numberOfPositiveContributions">The number of positive contributions to report, sorted from highest magnitude to lowest magnitude.

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

+61-42
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,8 @@
2626
namespace Microsoft.ML.Transforms
2727
{
2828
/// <summary>
29-
/// The FeatureContributionCalculationTransformer computes model-specific per-feature contributions to the score of each example.
30-
/// See the list of currently supported models below.
29+
/// <see cref="ITransformer"/> resulting from fitting a <see cref="FeatureContributionCalculatingEstimator"/>.
3130
/// </summary>
32-
/// <remarks>
33-
/// <para>
34-
/// Scoring a dataset with a trained model produces a score, or prediction, for each example. To understand and explain these predictions
35-
/// it can be useful to inspect which features influenced them most significantly. FeatureContributionCalculationTransformer computes a model-specific
36-
/// list of per-feature contributions to the score for each example. These contributions can be positive (they make the score higher) or negative
37-
/// (they make the score lower).
38-
/// </para>
39-
/// <para>
40-
/// Feature Contribution Calculation is currently supported for the following models:
41-
/// Regression:
42-
/// OrdinaryLeastSquares, StochasticDualCoordinateAscent (SDCA), OnlineGradientDescent, PoissonRegression,
43-
/// GeneralizedAdditiveModels (GAM), LightGbm, FastTree, FastForest, FastTreeTweedie
44-
/// Binary Classification:
45-
/// AveragedPerceptron, LinearSupportVectorMachines, LogisticRegression, StochasticDualCoordinateAscent (SDCA),
46-
/// StochasticGradientDescent (SGD), SymbolicStochasticGradientDescent, GeneralizedAdditiveModels (GAM),
47-
/// FastForest, FastTree, LightGbm
48-
/// Ranking:
49-
/// FastTree, LightGbm
50-
/// </para>
51-
/// <para>
52-
/// For linear models, the contribution of a given feature is equal to the product of feature value times the corresponding weight. Similarly,
53-
/// for Generalized Additive Models (GAM), the contribution of a feature is equal to the shape function for the given feature evaluated at
54-
/// the feature value.
55-
/// </para>
56-
/// <para>
57-
/// For tree-based models, the calculation of feature contribution essentially consists in determining which splits in the tree have the most impact
58-
/// on the final score and assigning the value of the impact to the features determining the split. More precisely, the contribution of a feature
59-
/// is equal to the change in score produced by exploring the opposite sub-tree every time a decision node for the given feature is encountered.
60-
/// Consider a simple case with a single decision tree that has a decision node for the binary feature F1. Given an example that has feature F1
61-
/// equal to true, we can calculate the score it would have obtained if we chose the subtree corresponding to the feature F1 being equal to false
62-
/// while keeping the other features constant. The contribution of feature F1 for the given example is the difference between the original score
63-
/// and the score obtained by taking the opposite decision at the node corresponding to feature F1. This algorithm extends naturally to models with
64-
/// many decision trees.
65-
/// </para>
66-
/// <para>
67-
/// See the sample below for an example of how to compute feature importance using the FeatureContributionCalculatingTransformer.
68-
/// </para>
69-
/// </remarks>
7031
public sealed class FeatureContributionCalculatingTransformer : OneToOneTransformerBase
7132
{
7233
internal sealed class Options : TransformInputBase
@@ -266,9 +227,67 @@ private Delegate GetValueGetter<TSrc>(DataViewRow input, int colSrc)
266227
}
267228

268229
/// <summary>
269-
/// Estimator producing a FeatureContributionCalculatingTransformer which scores the model on an input dataset and
270-
/// computes model-specific contribution scores for each feature.
230+
/// Computes model-specific per-feature contributions to the score of each input vector.
271231
/// </summary>
232+
/// <remarks>
233+
/// <format type="text/markdown"><![CDATA[
234+
///
235+
/// ### Estimator Characteristics
236+
/// | | |
237+
/// | -- | -- |
238+
/// | Does this estimator need to look at the data to train its parameters? | No |
239+
/// | Input column data type | Known-sized vector of <xref:System.Single> |
240+
/// | Output column data type | Known-sized vector of <xref:System.Single> |
241+
///
242+
/// Scoring a dataset with a trained model produces a score, or prediction, for each example. To understand and explain these predictions
243+
/// it can be useful to inspect which features influenced them most significantly. This transformer computes a model-specific
244+
/// list of per-feature contributions to the score for each example. These contributions can be positive (they make the score higher) or negative
245+
/// (they make the score lower).
246+
///
247+
/// Feature Contribution Calculation is currently supported for the following models:
248+
/// - Regression:
249+
/// - OlsTrainer
250+
/// - SdcaRegressionTrainer
251+
/// - OnlineGradientDescentTrainer
252+
/// - LbfgsPoissonRegressionTrainer
253+
/// - GamRegressionTrainer
254+
/// - LightGbmRegressionTrainer
255+
/// - FastTreeRegressionTrainer
256+
/// - FastForestRegressionTrainer
257+
/// - FastTreeTweedieTrainer
258+
/// - Binary Classification:
259+
/// - AveragedPerceptronTrainer
260+
/// - LinearSvmTrainer
261+
/// - LbfgsLogisticRegressionBinaryTrainer
262+
/// - SdcaNonCalibratedBinaryTrainer
263+
/// - SdcaLogisticRegressionBinaryTrainer
264+
/// - SgdCalibratedTrainer
265+
/// - SgdNonCalibratedTrainer
266+
/// - SymbolicSgdLogisticRegressionBinaryTrainer
267+
/// - GamBinaryTrainer
268+
/// - FastForestBinaryTrainer
269+
/// - FastTreeBinaryTrainer
270+
/// - LightGbmBinaryTrainer
271+
/// - Ranking:
272+
/// - FastTreeRankingTrainer
273+
/// - LightGbmRankingTrainer
274+
///
275+
/// For linear models, the contribution of a given feature is equal to the product of feature value times the corresponding weight. Similarly,
276+
/// for Generalized Additive Models (GAM), the contribution of a feature is equal to the shape function for the given feature evaluated at
277+
/// the feature value.
278+
///
279+
/// For tree-based models, the calculation of feature contribution essentially consists in determining which splits in the tree have the most impact
280+
/// on the final score and assigning the value of the impact to the features determining the split. More precisely, the contribution of a feature
281+
/// is equal to the change in score produced by exploring the opposite sub-tree every time a decision node for the given feature is encountered.
282+
/// Consider a simple case with a single decision tree that has a decision node for the binary feature F1. Given an example that has feature F1
283+
/// equal to true, we can calculate the score it would have obtained if we chose the subtree corresponding to the feature F1 being equal to false
284+
/// while keeping the other features constant. The contribution of feature F1 for the given example is the difference between the original score
285+
/// and the score obtained by taking the opposite decision at the node corresponding to feature F1. This algorithm extends naturally to models with
286+
/// many decision trees.
287+
/// ]]></format>
288+
/// </remarks>
289+
/// <seealso cref="ExplainabilityCatalog.CalculateFeatureContribution(TransformsCatalog, ISingleFeaturePredictionTransformer{ICalculateFeatureContribution}, int, int, bool)"/>
290+
/// <seealso cref="ExplainabilityCatalog.CalculateFeatureContribution{TModelParameters, TCalibrator}(TransformsCatalog, ISingleFeaturePredictionTransformer{Calibrators.CalibratedModelParametersBase{TModelParameters, TCalibrator}}, int, int, bool)"/>
272291
public sealed class FeatureContributionCalculatingEstimator : TrivialEstimator<FeatureContributionCalculatingTransformer>
273292
{
274293
private readonly string _featureColumn;

0 commit comments

Comments
 (0)