1

A snapshot of my code: (full version: 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);
        };

The error is on this line:

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

and its giving an error : Cannot convert anonymous method to delegate type 'Accord.MachineLearning.CrossValidationFittingFunction' because some of the return types in the block are not implicitly convertible to the delegate return type

Stefan
  • 17,448
  • 11
  • 60
  • 79
Thinker
  • 99
  • 1
  • 3
  • 13
  • It seems your return type is not of the same type that's compatible by watherver .Fitting is expecting. – Stefan May 07 '14 at 23:40

2 Answers2

1

The problem is that you are using the non-generic constructor CrossValidation to initialize the crossvalidation variable. The CrossValidation class inherits from CrossValidation<object>.

The Fitting property is a CrossValidationFittingFunction<TModel> delegate, where TModel for the non-generic CrossValidation class is object rather than DecisionTree.

Depending on your intentions, you could solve this either by using a more specific constructor:

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

or return less specific cross-validation values:

return new CrossValidationValues<object>(tree, trainingError, validationError);
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
0

As of version 3.7.0, it is now possible to create use Cross-Validation without having to write your own Fitting function. An example is shown below:

// 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
Cesar
  • 2,059
  • 25
  • 30