|
4 | 4 |
|
5 | 5 | using System;
|
6 | 6 | using System.IO;
|
| 7 | +using Microsoft.ML.Calibrators; |
7 | 8 | using Microsoft.ML.Data;
|
8 | 9 | using Microsoft.ML.Data.IO;
|
9 | 10 | using Microsoft.ML.Internal.Utilities;
|
@@ -174,36 +175,62 @@ public void TestGAMBinary()
|
174 | 175 | }
|
175 | 176 |
|
176 | 177 | private void TestFeatureContribution(
|
177 |
| - ITrainerEstimator<ISingleFeaturePredictionTransformer<IPredictor>, IPredictor> trainer, |
| 178 | + ITrainerEstimator<ISingleFeaturePredictionTransformer<ICalculateFeatureContribution>, ICalculateFeatureContribution> trainer, |
178 | 179 | IDataView data,
|
179 | 180 | string testFile,
|
180 | 181 | int precision = 6)
|
181 | 182 | {
|
182 | 183 | // Train the model.
|
183 | 184 | var model = trainer.Fit(data);
|
184 | 185 |
|
185 |
| - // Extract the predictor, check that it supports feature contribution. |
186 |
| - var fccModel = model as ISingleFeaturePredictionTransformer<ICalculateFeatureContribution>; |
187 |
| - Assert.NotNull(fccModel); |
| 186 | + // Calculate feature contributions. |
| 187 | + var est = ML.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 3, numberOfNegativeContributions: 0) |
| 188 | + .Append(ML.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 0, numberOfNegativeContributions: 3)) |
| 189 | + .Append(ML.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 1, numberOfNegativeContributions: 1)) |
| 190 | + .Append(ML.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 1, numberOfNegativeContributions: 1, normalize: false)); |
| 191 | + |
| 192 | + TestEstimatorCore(est, data); |
| 193 | + |
| 194 | + // Verify output. |
| 195 | + CheckOutput(est, data, testFile, precision); |
| 196 | + Done(); |
| 197 | + } |
| 198 | + |
| 199 | + private void TestFeatureContribution<TModelParameters, TCalibrator>( |
| 200 | + ITrainerEstimator<ISingleFeaturePredictionTransformer<CalibratedModelParametersBase<TModelParameters, TCalibrator>>, CalibratedModelParametersBase<TModelParameters, TCalibrator>> trainer, |
| 201 | + IDataView data, |
| 202 | + string testFile, |
| 203 | + int precision = 6) |
| 204 | + where TModelParameters : class, ICalculateFeatureContribution |
| 205 | + where TCalibrator : class, ICalibrator |
| 206 | + { |
| 207 | + // Train the model. |
| 208 | + var model = trainer.Fit(data); |
188 | 209 |
|
189 | 210 | // Calculate feature contributions.
|
190 |
| - var est = ML.Transforms.CalculateFeatureContribution(fccModel, numberOfPositiveContributions: 3, numberOfNegativeContributions: 0) |
191 |
| - .Append(ML.Transforms.CalculateFeatureContribution(fccModel, numberOfPositiveContributions: 0, numberOfNegativeContributions: 3)) |
192 |
| - .Append(ML.Transforms.CalculateFeatureContribution(fccModel, numberOfPositiveContributions: 1, numberOfNegativeContributions: 1)) |
193 |
| - .Append(ML.Transforms.CalculateFeatureContribution(fccModel, numberOfPositiveContributions: 1, numberOfNegativeContributions: 1, normalize: false)); |
| 211 | + var est = ML.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 3, numberOfNegativeContributions: 0) |
| 212 | + .Append(ML.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 0, numberOfNegativeContributions: 3)) |
| 213 | + .Append(ML.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 1, numberOfNegativeContributions: 1)) |
| 214 | + .Append(ML.Transforms.CalculateFeatureContribution(model, numberOfPositiveContributions: 1, numberOfNegativeContributions: 1, normalize: false)); |
194 | 215 |
|
195 | 216 | TestEstimatorCore(est, data);
|
| 217 | + |
196 | 218 | // Verify output.
|
| 219 | + CheckOutput(est, data, testFile, precision); |
| 220 | + Done(); |
| 221 | + } |
| 222 | + |
| 223 | + private void CheckOutput(IEstimator<ITransformer> estimator, IDataView data, string testFile, int precision = 6) |
| 224 | + { |
197 | 225 | var outputPath = GetOutputPath("FeatureContribution", testFile + ".tsv");
|
198 | 226 | using (var ch = Env.Start("save"))
|
199 | 227 | {
|
200 | 228 | var saver = new TextSaver(ML, new TextSaver.Arguments { Silent = true, OutputHeader = false });
|
201 |
| - var savedData = ML.Data.TakeRows(est.Fit(data).Transform(data), 4); |
| 229 | + var savedData = ML.Data.TakeRows(estimator.Fit(data).Transform(data), 4); |
202 | 230 | using (var fs = File.Create(outputPath))
|
203 | 231 | DataSaverUtils.SaveDataView(ch, saver, savedData, fs, keepHidden: true);
|
204 | 232 | }
|
205 | 233 | CheckEquality("FeatureContribution", testFile + ".tsv", digitsOfPrecision: precision);
|
206 |
| - Done(); |
207 | 234 | }
|
208 | 235 |
|
209 | 236 | /// <summary>
|
|
0 commit comments