Skip to content
This repository has been 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

Closed
2 of 3 tasks
ConductedClever opened this issue Aug 23, 2017 · 14 comments
Closed
2 of 3 tasks
Labels
Milestone

Comments

@ConductedClever
Copy link

ConductedClever commented Aug 23, 2017

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.

@cesarsouza
Copy link
Member

cesarsouza commented Aug 23, 2017

Hi TG,

Thanks for opening the issue!

Yes, Accord does offer an implementation for cross-validation. You can find it in the Accord.MachineLearning.Performance namespace, and an example is available at bottom of the aforelinked documentation page.

Regarding the exception, I think this might be happening because one of your folds didn't contain all the possible class labels or symbol values in its training set, but did contain them in the testing set. In other words, the testing set contained either classes or symbols not actually expected by the model because it had never seen them during the training phase.

To overcome this situation, it should be possible to create the NaiveBayes model manually and specify how many classes and symbols it should be actually expecting. Then, you can pass it to the "Model" property of the NaiveBayesLearning object you have just created. Something like this:

... 

var partialLearner = new NaiveBayesLearning()
{
    Model = new NaiveBayes(numberOfClasses, numberOfSymbolsForVar1, numberOfSymbolsForVar2, ... etc)
};

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

Please let me know if it helps. The exception should have thrown a more descriptive message if this is the case.

Regards,
Cesar

@cesarsouza
Copy link
Member

cesarsouza commented Aug 23, 2017

Just make sure that you retrieve the number of classes and number of symbols from your entire training set and not compute it from the fold. If you have used a codebook to codify your dataset, you can retrieve those values from there, for example (as long as you have created this codebook outside of the cross-validation scheme).

@ConductedClever
Copy link
Author

Hi cesar. I will use the cross validation of Accord and inform you about being successful. But in my problem the exception occurs while the learn method gets called for the first fold. Please check the image I've added in the question. So the problem should be somewhere else. As the screen shot shows the length of input and output arrays are the same. Any other ideas to check?

@cesarsouza
Copy link
Member

Hi @ConductedClever, it should be possible to have more classes or variable symbols in the testing set than in the training set in any of the folds, including the first. If you still get the issue even after specifying the model parameters manually, do you think you can provide an example of your data such I can reproduce the issue here?

You can use the .ToCSharp() extension methods (provided by the Matrix class) to convert your double[][] and int[][] matrices and arrays into C#, such that you could paste them here (unless they may contain sensitive information you would like to avoid posting online).

@ConductedClever
Copy link
Author

As I noted above, the exception occurs in the learning phase not in the testing (Please check this screen shot again). So your idea about class count and variable count, I think does not point the problem. Yes of course; this is the data (and the source code is the same of main question):

The entire input data:

new double[][] {
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 2 },
    new double[] { 1, 1, 2, 4 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 3, 7 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 4, 2 },
    new double[] { 1, 1, 4, 5 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 9 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 11 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 2 },
    new double[] { 1, 1, 2, 4 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 4, 2 },
    new double[] { 1, 1, 4, 5 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 1, 3, 5, 12 },
    new double[] { 1, 3, 5, 13 },
    new double[] { 1, 3, 5, 13 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 2, 9 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 2 },
    new double[] { 1, 1, 2, 4 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 2 },
    new double[] { 1, 1, 2, 4 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 14 },
    new double[] { 0, 4, 6, 15 },
    new double[] { 0, 4, 6, 15 },
    new double[] { 0, 4, 6, 15 },
    new double[] { 0, 5, 0, 13 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 1, 3 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 1, 8 },
    new double[] { 1, 1, 1, 5 },
    new double[] { 1, 1, 1, 6 },
    new double[] { 1, 1, 1, 3 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 1, 8 },
    new double[] { 1, 1, 1, 5 },
    new double[] { 1, 1, 1, 6 },
    new double[] { 1, 1, 4, 2 },
    new double[] { 1, 1, 4, 4 },
    new double[] { 1, 1, 4, 8 },
    new double[] { 1, 1, 4, 5 },
    new double[] { 1, 1, 4, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 6, 0, 16 },
    new double[] { 0, 4, 6, 15 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 4, 2 },
    new double[] { 1, 1, 4, 4 },
    new double[] { 1, 1, 4, 8 },
    new double[] { 1, 1, 4, 5 },
    new double[] { 1, 1, 4, 6 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 1, 2, 0, 10 },
    new double[] { 1, 2, 0, 10 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 7, 5 },
    new double[] { 1, 1, 1, 3 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 1, 8 },
    new double[] { 1, 1, 1, 5 },
    new double[] { 1, 1, 1, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 1, 2, 0, 10 },
    new double[] { 0, 6, 0, 16 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 1, 3 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 1, 8 },
    new double[] { 1, 1, 1, 5 },
    new double[] { 1, 1, 1, 6 },
    new double[] { 0, 2, 0, 10 } 
};

The entire output data:

new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 };

The first fold learn input data:

new double[][] {
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 11 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 2 },
    new double[] { 1, 1, 2, 4 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 4, 2 },
    new double[] { 1, 1, 4, 5 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 1, 3, 5, 12 },
    new double[] { 1, 3, 5, 13 },
    new double[] { 1, 3, 5, 13 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 2, 9 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 2 },
    new double[] { 1, 1, 2, 4 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 2, 3 },
    new double[] { 1, 1, 2, 2 },
    new double[] { 1, 1, 2, 4 },
    new double[] { 1, 1, 2, 5 },
    new double[] { 1, 1, 2, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 3 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 8 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 14 },
    new double[] { 0, 4, 6, 15 },
    new double[] { 0, 4, 6, 15 },
    new double[] { 0, 4, 6, 15 },
    new double[] { 0, 5, 0, 13 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 1, 3 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 1, 8 },
    new double[] { 1, 1, 1, 5 },
    new double[] { 1, 1, 1, 6 },
    new double[] { 1, 1, 1, 3 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 1, 8 },
    new double[] { 1, 1, 1, 5 },
    new double[] { 1, 1, 1, 6 },
    new double[] { 1, 1, 4, 2 },
    new double[] { 1, 1, 4, 4 },
    new double[] { 1, 1, 4, 8 },
    new double[] { 1, 1, 4, 5 },
    new double[] { 1, 1, 4, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 6, 0, 16 },
    new double[] { 0, 4, 6, 15 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 1, 1, 4, 2 },
    new double[] { 1, 1, 4, 4 },
    new double[] { 1, 1, 4, 8 },
    new double[] { 1, 1, 4, 5 },
    new double[] { 1, 1, 4, 6 },
    new double[] { 1, 1, 3, 2 },
    new double[] { 1, 1, 3, 4 },
    new double[] { 1, 1, 3, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 1, 2, 0, 10 },
    new double[] { 1, 2, 0, 10 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 7, 5 },
    new double[] { 1, 1, 1, 3 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 1, 8 },
    new double[] { 1, 1, 1, 5 },
    new double[] { 1, 1, 1, 6 },
    new double[] { 0, 2, 0, 10 },
    new double[] { 1, 2, 0, 10 },
    new double[] { 0, 6, 0, 16 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 0 },
    new double[] { 0, 0, 0, 1 },
    new double[] { 1, 1, 1, 3 },
    new double[] { 1, 1, 1, 2 },
    new double[] { 1, 1, 1, 8 },
    new double[] { 1, 1, 1, 5 },
    new double[] { 1, 1, 1, 6 },
    new double[] { 0, 2, 0, 10 } 
};

The first fold learn output data:

new double[] { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 };

Thanks for your time spending.

@ConductedClever
Copy link
Author

@cesarsouza if the data before codification also helps tell me to add it. Thanks.

@cesarsouza
Copy link
Member

Hi @ConductedClever, obviously you are completely right. I misunderstood the situation yesterday. I believe I will be able to try out your data examples (many thanks for that) in the next few hours.

However, it can be that my initial suggestion about how it could be solved may still be right. What can be happening is that NaiveBayes (or rather, GeneralDiscreteDistribution that is used by NaiveBayes) is not being able to recognize gaps between symbols in your discrete data set. Specifying the number of symbols directly instead of letting the learning algorithm figure it out might be a workaround.

I will investigate and report soon. Many thanks for including the example data in the issue.

Regards,
Cesar

@ConductedClever
Copy link
Author

Hi @cesarsouza. You're very welcome. So if your suggestion goes right, moving the codification phase after partitioning phase willl correct the problem. I will try this solution and inform you about that. Thanks.

@cesarsouza
Copy link
Member

cesarsouza commented Aug 24, 2017

Hi @ConductedClever,

For example, using the following code might work around the problem you have encountered:

var teacher = new NaiveBayesLearning()
{
    Model = new NaiveBayes(2, 2, 7, 8, 17)
};

This should make sure that any Naive Bayes models are created using the correct number of symbols for each of the variables being learned (as the number of symbols in your 4 variables are, respectively, 2, 7, 8, and 17 - and the first 2 in the constructor are the number of classes).

Regards,
Cesar

@cesarsouza
Copy link
Member

cesarsouza commented Aug 24, 2017

Anyways, I guess this could have been handled better by the framework itself, I will include some updates in the next version to make this more automatic. Thanks again for opening the issue.

@cesarsouza
Copy link
Member

By the way, as a last question: can I include the data you have provided as a unit test in the framework under the LGPL license?

@ConductedClever
Copy link
Author

Hi @cesarsouza. I will try your solution tomorrow (about 7 hours later) and notify you from the result.

Yes, with that update developers will have a better life 😀.

Of course you can. It is my proud and pleasure. Thanks.

@ConductedClever
Copy link
Author

Hi @cesarsouza , I have tested your solution and everything just went right.

I was just forget to reflect the result here.

Thanks a lot.

@cesarsouza
Copy link
Member

cesarsouza commented Oct 22, 2017

Available in 3.8.0.

@cesarsouza cesarsouza added this to the 3.8 milestone Oct 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants