【问题标题】:Can i possible create cross validation to ID3 algorithm in accord.net?我可以在accord.net 中创建对 ID3 算法的交叉验证吗?
【发布时间】:2014-05-07 22:25:46
【问题描述】:

我的代码快照:(完整版:http://pastebin.com/7ALhSKgX

        var crossvalidation = new CrossValidation(size: data.Rows.Count, folds: 7);

        crossvalidation.Fitting = 
             delegate(int k, int[] indicesTrain, int[] indicesValidation)
        {
            //omitted declarations for clarity
            DecisionTree tree = new DecisionTree(attributes, classCount);

            //omitted
            double trainingError = 
               id3learning.ComputeError(trainingInputs, trainingOutputs);
            double validationError = 
               id3learning.ComputeError(validationInputs, validationOutputs);
            return new CrossValidationValues<DecisionTree>
               (tree, trainingError, validationError);
        };

错误在这一行:

          return new CrossValidationValues<DecisionTree>
                        (tree, trainingError, validationError);

及其给出错误: 无法将匿名方法转换为委托类型“Accord.MachineLearning.CrossValidationFittingFunction”,因为块中的某些返回类型不能隐式转换为委托返回类型

【问题讨论】:

  • 您的返回类型似乎与 watherver 兼容的类型不同。期待拟合。

标签: c# id3 cross-validation accord.net


【解决方案1】:

问题是您使用非泛型构造函数CrossValidation 来初始化crossvalidation 变量。 CrossValidation 类继承自 CrossValidation&lt;object&gt;

Fitting 属性是 CrossValidationFittingFunction<TModel> 委托,其中非泛型 CrossValidation 类的 TModelobject 而不是 DecisionTree

根据您的意图,您可以通过使用 more 特定的构造函数来解决此问题:

var crossvalidation = new CrossValidation<DecisionTree>(size: data.Rows.Count, folds: 7);

返回less特定的交叉验证值:

return new CrossValidationValues<object>(tree, trainingError, validationError);

【讨论】:

    【解决方案2】:

    从 3.7.0 版开始,现在可以创建使用交叉验证,而无需编写自己的拟合函数。示例如下:

    // Ensure we have reproducible results
    Accord.Math.Random.Generator.Seed = 0;
    
    // Get some data to be learned: Here we will download and use Wiconsin's
    // (Diagnostic) Breast Cancer dataset, where the goal is to determine
    // whether the characteristics extracted from a breast cancer exam
    // correspond to a malignant or benign type of cancer. In order to do
    // this using the Accord.NET Framework, all we have to do is:
    var data = new WisconsinDiagnosticBreastCancer();
    
    // Now, we can import the input features and output labels using
    double[][] input = data.Features; // 569 samples, 30-dimensional features
    int[] output = data.ClassLabels;  // 569 samples, 2 different class labels
    
    // Now, let's say we want to measure the cross-validation performance of
    // a decision tree with a maximum tree height of 5 and where variables
    // are able to join the decision path at most 2 times during evaluation:
    var cv = CrossValidation.Create(
    
        k: 10, // We will be using 10-fold cross validation
    
        learner: (p) => new C45Learning() // here we create the learning algorithm
        {
            Join = 2,
            MaxHeight = 5
        },
    
        // Now we have to specify how the tree performance should be measured:
        loss: (actual, expected, p) => new ZeroOneLoss(expected).Loss(actual),
    
        // This function can be used to perform any special
        // operations before the actual learning is done, but
        // here we will just leave it as simple as it can be:
        fit: (teacher, x, y, w) => teacher.Learn(x, y, w),
    
        // Finally, we have to pass the input and output data
        // that will be used in cross-validation. 
        x: input, y: output
    );
    
    // After the cross-validation object has been created,
    // we can call its .Learn method with the input and 
    // output data that will be partitioned into the folds:
    var result = cv.Learn(input, output);
    
    // We can grab some information about the problem:
    int numberOfSamples = result.NumberOfSamples; // should be 569
    int numberOfInputs = result.NumberOfInputs;   // should be 30
    int numberOfOutputs = result.NumberOfOutputs; // should be 2
    
    double trainingError = result.Training.Mean; // should be 0
    double validationError = result.Validation.Mean; // should be 0.089661654135338359
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 2021-06-06
      • 1970-01-01
      • 2021-03-19
      • 2013-03-04
      • 2011-07-24
      • 2016-02-04
      相关资源
      最近更新 更多