|
1 | 1 | ---
|
2 | 2 | title: Interpret ML.NET models with Permutation Feature Importance
|
3 | 3 | description: Understand the feature importance of models with Permutation Feature Importance in ML.NET
|
4 |
| -ms.date: 10/05/2021 |
| 4 | +ms.date: 12/06/2022 |
5 | 5 | author: luisquintanilla
|
6 | 6 | ms.author: luquinta
|
7 | 7 | ms.custom: mvc,how-to
|
@@ -105,38 +105,31 @@ string[] featureColumnNames =
|
105 | 105 | .Select(column => column.Name)
|
106 | 106 | .Where(columnName => columnName != "Label").ToArray();
|
107 | 107 |
|
108 |
| -// 2. Define estimator with data pre-processing steps |
109 |
| -IEstimator<ITransformer> dataPrepEstimator = |
| 108 | +// 2. Define training pipeline |
| 109 | +IEstimator<ITransformer> sdcaEstimator = |
110 | 110 | mlContext.Transforms.Concatenate("Features", featureColumnNames)
|
111 |
| - .Append(mlContext.Transforms.NormalizeMinMax("Features")); |
| 111 | + .Append(mlContext.Transforms.NormalizeMinMax("Features")) |
| 112 | + .Append(mlContext.Regression.Trainers.Sdca()); |
112 | 113 |
|
113 |
| -// 3. Create transformer using the data pre-processing estimator |
114 |
| -ITransformer dataPrepTransformer = dataPrepEstimator.Fit(data); |
115 |
| - |
116 |
| -// 4. Pre-process the training data |
117 |
| -IDataView preprocessedTrainData = dataPrepTransformer.Transform(data); |
118 |
| - |
119 |
| -// 5. Define Stochastic Dual Coordinate Ascent machine learning estimator |
120 |
| -var sdcaEstimator = mlContext.Regression.Trainers.Sdca(); |
121 |
| - |
122 |
| -// 6. Train machine learning model |
123 |
| -var sdcaModel = sdcaEstimator.Fit(preprocessedTrainData); |
| 114 | +// 3. Train machine learning model |
| 115 | +var sdcaModel = sdcaEstimator.Fit(data); |
124 | 116 | ```
|
125 | 117 |
|
126 | 118 | ## Explain the model with Permutation Feature Importance (PFI)
|
127 | 119 |
|
128 | 120 | In ML.NET use the [`PermutationFeatureImportance`](xref:Microsoft.ML.PermutationFeatureImportanceExtensions) method for your respective task.
|
129 | 121 |
|
130 | 122 | ```csharp
|
| 123 | +// Use the model to make predictions |
| 124 | +var transformedData = sdcaModel.Transform(data); |
| 125 | + |
| 126 | +// Calculate feature importance |
131 | 127 | ImmutableArray<RegressionMetricsStatistics> permutationFeatureImportance =
|
132 | 128 | mlContext
|
133 | 129 | .Regression
|
134 |
| - .PermutationFeatureImportance(sdcaModel, preprocessedTrainData, permutationCount:3); |
| 130 | + .PermutationFeatureImportance(sdcaModel, transformedData, permutationCount:3); |
135 | 131 | ```
|
136 | 132 |
|
137 |
| -> [!NOTE] |
138 |
| -> For pipelines that combine the preprocessing transforms and trainer, assuming that the trainer is at the end of the pipeline, you'll need to extract it using the `LastTransformer` property. |
139 |
| -
|
140 | 133 | The result of using [`PermutationFeatureImportance`](xref:Microsoft.ML.PermutationFeatureImportanceExtensions) on the training dataset is an [`ImmutableArray`](xref:System.Collections.Immutable.ImmutableArray) of [`RegressionMetricsStatistics`](xref:Microsoft.ML.Data.RegressionMetricsStatistics) objects. [`RegressionMetricsStatistics`](xref:Microsoft.ML.Data.RegressionMetricsStatistics) provides summary statistics like mean and standard deviation for multiple observations of [`RegressionMetrics`](xref:Microsoft.ML.Data.RegressionMetrics) equal to the number of permutations specified by the `permutationCount` parameter.
|
141 | 134 |
|
142 | 135 | The metric used to measure feature importance depends on the machine learning task used to solve your problem. For example, regression tasks may use a common evaluation metric such as R-squared to measure importance. For more information on model evaluation metrics, see [evaluate your ML.NET model with metrics](../resources/metrics.md).
|
@@ -179,6 +172,7 @@ Taking a look at the five most important features for this dataset, the price of
|
179 | 172 |
|
180 | 173 | ## Next steps
|
181 | 174 |
|
| 175 | +- [Use Permutation Feature Importance (PFI) with AutoML](how-to-use-the-automl-api.md#determine-feature-importance) |
182 | 176 | - [Make predictions with a trained model](machine-learning-model-predictions-ml-net.md)
|
183 | 177 | - [Retrain a model](retrain-model-ml-net.md)
|
184 | 178 | - [Deploy a model in an ASP.NET Core Web API](serve-model-web-api-ml-net.md)
|
0 commit comments