首页 新闻 会员 周边 捐助

ML.NET 报错:'KeyToValueMappingTransformer' does not contain a definition for 'Model'

0
悬赏园豆:5 [已解决问题] 解决于 2023-06-17 05:19

我用.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 

加载中...
 
 
 
EMA
 
 
泰德的主页 泰德 | 初学一级 | 园豆:6
提问于:2023-03-02 07:17
< >
分享
最佳答案
0

根据您提供的信息,错误出现在以下行上:

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() 方法以获取选择的特征。

希望这可以帮助您解决问题。

收获园豆:5
Technologyforgood | 大侠五级 |园豆:7541 | 2023-03-28 21:55

还是同样的问题:

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

泰德 | 园豆:6 (初学一级) | 2023-03-29 13:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册