Skip to content

Instantly share code, notes, and snippets.

@AndyButland
Last active April 30, 2019 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndyButland/ff56d35d727c38b7d463795be4eb0adb to your computer and use it in GitHub Desktop.
Save AndyButland/ff56d35d727c38b7d463795be4eb0adb to your computer and use it in GitHub Desktop.
private static void ReportOnFeatureImportance(MLContext context, ITransformer model, IDataView data)
{
...
var results = featureNames
.Select((t, i) => new FeatureImportance
{
Name = t,
RSquaredMean = Math.Abs(permutationMetrics[i].RSquared.Mean),
CorrelationCoefficient = CalculateSingleFactorCorrelationCoefficient(context, data, t)
})
.OrderByDescending(x => x.RSquaredMean);
...
}
private static double CalculateSingleFactorCorrelationCoefficient(MLContext context, IDataView data, string featureColumn)
{
return CalculateCorrelationCoefficientBetweenValues(GetSingleColumn(context, data, featureColumn), GetSingleColumn(context, data, PredictionLabel));
}
private static IEnumerable<float> GetSingleColumn(IHostEnvironment context, IDataView data, string columnName)
{
return data.GetColumn<float>(context, columnName);
}
private static double CalculateCorrelationCoefficientBetweenValues(IEnumerable<float> featureColumn, IEnumerable<float> resultColumn)
{
return StatsHelper.ComputeCorrellationCoefficent(featureColumn.ToArray(), resultColumn.ToArray());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment