我用.NET 7 和 ML.NET 2.0.1. 代码如下:
// Load your data
var mlContext = new MLContext();
var data = mlContext.Data.LoadFromTextFile<StockData>("path/to/your/data.csv", separatorChar: ',');// Split your data into training and testing sets
var trainTestSplit = mlContext.Data.TrainTestSplit(data);// Define your feature selection pipeline
var pipeline = mlContext.Transforms.Concatenate("Features", "Price", "Time", "Lacation", "Sex")
.Append(mlContext.Transforms.Conversion.MapValueToKey("Label", "Trend"))
.Append(mlContext.Transforms.FeatureSelection.SelectFeaturesBasedOnMutualInformation("Features", "Label",slotsInOutput:3, numberOfBins: 2))
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));// Train your model
var model = pipeline.Fit(trainTestSplit.TrainSet);// Evaluate your model on the test set
var metrics = mlContext.BinaryClassification.Evaluate(model.Transform(trainTestSplit.TestSet));// Print the feature weights to see which indicators are most important
var featureWeights = model.LastTransformer.Model.GetFeatureWeights();
Console.WriteLine("Feature weights:");
foreach (var featureWeight in featureWeights)
{
Console.WriteLine($"{featureWeight.FeatureName}: {featureWeight.Weight}");
}
错误:
Severity Code Description Project File Line Suppression State
Error CS1061 'KeyToValueMappingTransformer' does not contain a definition for 'Model' and no accessible extension method 'Model' accepting a first argument of type 'KeyToValueMappingTransformer' could be found (are you missing a using directive or an assembly reference?) MLConsole
根据您提供的信息,错误出现在以下行上:
csharp
var featureWeights = model.LastTransformer.Model.GetFeatureWeights();
在ML.NET 2.0中,KeyToValueMappingTransformer 类型没有一个名为 Model 的公共属性或方法,因此编译器无法识别该行中的方法调用。 您可以尝试将该行更改为:
css
Copy codevar featureWeights = model.LastTransformer.GetPermutationFeatureImportance(mlContext, trainTestSplit.TestSet, numberOfPermutations: 1);
这将使用排列特征重要性方法计算每个特征的重要性,并返回一个具有特征名称和重要性得分的字典。如果您仍然希望获取特定转换器的功能权重,可以使用 SelectFeaturesBasedOnMutualInformation 方法中的 featureSelectors 参数。这个参数返回一个 FeatureSelectorTransform.Selector 类型的委托,您可以将其传递给 model.Transform() 方法以获取选择的特征。
希望这可以帮助您解决问题。
还是同样的问题:
Severity Code Description Project File Line Suppression State
Error CS1061 'BinaryPredictionTransformer<CalibratedModelParametersBase<FastTreeBinaryModelParameters, PlattCalibrator>>' does not contain a definition for 'GetPermutationFeatureImportance' and no accessible extension method 'GetPermutationFeatureImportance' accepting a first argument of type 'BinaryPredictionTransformer<CalibratedModelParametersBase<FastTreeBinaryModelParameters, PlattCalibrator>>' could be found (are you missing a using directive or an assembly reference?) MLConsole C:\D\Stocks\myMLApp\MLConsole\Program.cs 64 Active