Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.
This repository was archived by the owner on Nov 19, 2020. It is now read-only.

Manipulated array throws “Index was outside the bounds of the array” exception #802

@ConductedClever

Description

@ConductedClever

What would you like to submit? (put an 'x' inside the bracket that applies)

  • question
  • feature request
  • bug report

Issue description

Hi. I am developing a dot net core 2.0 application using Accord.net 3.7.0. When I give the entire array as learning input everything goes right, but when I divide the array for the purpose of "K-Fold Cross Validation" the exception of “Index was outside the bounds of the array” occurs. The mentioned source code is the following:

    public LearningResultViewModel NaiveBayes(int[][] inputs, int[] outputs, int partitionCountForTest)
    {
        LearningResultViewModel totalResult = new LearningResultViewModel();

        #region full learn and test
        // Create a new Naive Bayes learning
        var learner = new NaiveBayesLearning();

        // Learn a Naive Bayes model from the examples
        NaiveBayes nb = learner.Learn(inputs, outputs);

        #region test phase
        // Compute the machine outputs
        int[] predicted = nb.Decide(inputs);

        // Use confusion matrix to compute some statistics.
        ConfusionMatrix confusionMatrix = new ConfusionMatrix(predicted, outputs, 1, 0);
        #endregion
        #endregion

        LearningResultRecord result = new LearningResultRecord()
        {
            Distributions = nb.Distributions,
            NumberOfClasses = nb.NumberOfClasses,
            NumberOfInputs = nb.NumberOfInputs,
            NumberOfOutputs = nb.NumberOfOutputs,
            NumberOfSymbols = nb.NumberOfSymbols,
            Priors = nb.Priors,
            confusionMatrix = confusionMatrix
        };
        totalResult.FullDataLearnAndTest = result;

        #region k part learn and test
        float partitionSize = inputs.Length / partitionCountForTest;
        for (int i = 0; i < partitionCountForTest; i++)
        {
            var learnDataInputs = inputs.ToList();

            var testDataInputs = learnDataInputs
                .GetRange((int)Math.Floor(i * partitionSize)
                    , (int)Math.Floor(partitionSize));

            learnDataInputs
                .RemoveRange((int)Math.Floor(i * partitionSize)
                    , (int)Math.Floor(partitionSize));

            var learnDataOutputs = outputs.ToList();

            var testDataOutputs = learnDataOutputs
                .GetRange((int)Math.Floor(i * partitionSize)
                    , (int)Math.Floor(partitionSize));

            learnDataOutputs
                .RemoveRange((int)Math.Floor(i * partitionSize)
                    , (int)Math.Floor(partitionSize));

            // Create a new Naive Bayes learning
            var partialLearner = new NaiveBayesLearning();

            // Learn a Naive Bayes model from the examples
            NaiveBayes pnb = partialLearner.Learn(learnDataInputs.ToArray(), learnDataOutputs.ToArray());

            #region test phase
            // Compute the machine outputs
            int[] ppredicted = pnb.Decide(testDataInputs.ToArray());

            // Use confusion matrix to compute some statistics.
            ConfusionMatrix pConfusionMatrix = new ConfusionMatrix(ppredicted, testDataOutputs.ToArray(), 1, 0);
            #endregion
            totalResult.PartitionedDataLearnAndTest.Add(new LearningResultRecord()
            {
                Distributions = pnb.Distributions,
                NumberOfClasses = pnb.NumberOfClasses,
                NumberOfInputs = pnb.NumberOfInputs,
                NumberOfOutputs = pnb.NumberOfOutputs,
                NumberOfSymbols = pnb.NumberOfSymbols,
                Priors = pnb.Priors,
                confusionMatrix = pConfusionMatrix
            });
        }
        #endregion

        return totalResult;
    }

And some additional data there exists in this screen shot:

exception screen shot

Questions:

1- Is there any "K-Fold Cross Validation" functionality implemented in Accord itself?

2- What makes this exception occur? (Is it a bug?)

3- I have previously encountered same exception here. Was that solution the best one? (although current problem differs that) (Is it a bug?)

Thanks in advance. TG.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions