我使用了trainingDataView中的一部分列在ML.net 1.0.0中构建了一个简单的BinaryClassification FastTree模型。现在,我想执行PFI分析,但似乎无法仅隔离模型中使用的列/功能与IDataView中的所有列。

我一直在this link PFI的二进制分类中引用该示例。

var trainingDataView = mlContext.Data.LoadFromTextFile<FPPCNTKData>(TrainDataPath, hasHeader: false, separatorChar: ' ');

Var pipeline = mlContext.Transforms.Concatenate("Features",
                                                "mCalc_FPP_Legs_Range",
                                                "mCalc_FPP_Legs_Ticks",
                                                "mCalc_FPP_Legs_Bars",
                                                "mCalc_FPP_Legs_TMins",
                                                "mCalc_FPP_Diag_RangeBars",
                                                "mCalc_FPP_Diag_RangeTMins",
                                                "mCalc_FPP_Diag_TicksBars",
                                                "mCalc_FPP_Diag_TicksTMins",
                                                "mCalc_XD_XA_Mult_Ticks",
                                                "mCalc_AB_XA_Mult_Ticks",
                                                "mCalc_AD_XA_Mult_Ticks",
                                                "mCalc_BC_XA_Mult_Ticks",
                                                "mCalc_BC_AB_Mult_Ticks",
                                                "mCalc_CD_AB_Mult_Ticks",
                                                "mCalc_CD_BC_Mult_Ticks",
                                                "mCalc_CD_BD_Mult_Ticks")
     .Append(mlContext.BinaryClassification.Trainers.FastTree(labelColumnName: "mHiProfitOneHot", featureColumnName: "Features"));

var trainedModel = pipeline.Fit(trainingDataView);



如下所示,由于我是从原始trainingDataView收集特征名称的,而不是从模型中使用的名称,因此PFI项的标签不正确。

//// Compute the permutation metrics using the properly normalized data.
var linearPredictor = trainedModel.LastTransformer;
var transformedData = trainedModel.Transform(trainingDataView);
var permutationMetrics = mlContext.BinaryClassification.PermutationFeatureImportance(
                linearPredictor, transformedData, labelColumnName: "mHiProfitOneHot", permutationCount: 3);

// Now let's look at which features are most important to the model overall.
// Get the feature indices sorted by their impact on AUC.
var sortedIndices = permutationMetrics.Select((MetricStatistics, index) => new { index, metrics.AreaUnderRocCurve })
                .OrderByDescending(feature => Math.Abs(feature.AreaUnderRocCurve))
                .Select(feature => feature.index);

// Get the feature names from the training set
var featureNames =
    trainingDataView.Schema.AsEnumerable()
    .Select(column => column.Name) // Get the column names
    .Where(name => name != "mHiProfitOneHot") // Drop the Label
    .ToArray();


Console.WriteLine("Feature\tModel Weight\tChange in AUC\t95% Confidence in the Mean Change in AUC");
var auc = permutationMetrics.Select(x => x.AreaUnderRocCurve).ToArray();
foreach (int i in sortedIndices)
{
    Console.WriteLine("{0}\t{1:0.00}\t{2:G4}\t{3:G4}",
         featureNames[i],
         linearPredictor.Model.SubModel.TrainedTreeEnsemble.TreeWeights[i],
         auc[i].Mean,
         1.96 * auc[i].StandardError);
}


是否可以直接从模型中提取特征名称的子集?谢谢。

最佳答案

您可以搜索模型(假设它是TransformerChain,在您的情况下似乎如此),以查找ColumnConcatenatingTransformer并获取输入列名。

string[] columnNames = (model
                    .FirstOrDefault(t => t is ColumnConcatenatingTransformer) as ColumnConcatenatingTransformer)
                    ?.Columns
                    ?.FirstOrDefault(c => c.outputColumnName == "Features")
                    .inputColumnNames;
Console.WriteLine(String.Join(", ", columnNames));

关于c# - 获取BinaryClassification FastTree功能名称以进行PFI分析,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55656712/

10-10 12:35