Accord.net NaiveBayesLearning "The index was outside the array"

I am using Accord.net 3.7.0 in the 1.1 point kernel.

The algorithm used is naive Bayesian. And the source code of the learning mechanism is as follows:

public LearningResultViewModel NaiveBayes(int[][] inputs, int[] outputs) { // 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 LearningResultViewModel result = new LearningResultViewModel() { Distributions = nb.Distributions, NumberOfClasses = nb.NumberOfClasses, NumberOfInputs = nb.NumberOfInputs, NumberOfOutputs = nb.NumberOfOutputs, NumberOfSymbols = nb.NumberOfSymbols, Priors = nb.Priors, confusionMatrix = confusionMatrix }; return result; } 

I tested this piece of code on small data, but as the data grew

The index was outside the array

An error has occurred.

Since I can’t navigate the Learn method, so I don’t know what to do. screenshot of runtime:

Runtime Error Screenshot

No further information, no internal exception. No IDEA !!!

TG.

// UPDATE_1 ***

The input array is a 180 by 4 matrix (array), as shown below:

Inputs

which has 4 columns in each row. checked manually (I can share my video too if necessary !!!)

The output of the array is 180, as shown below:

Outputs

which contains only 0 and 1 (I can share my video too!).

And about the NaiveBayesinLearning document here:

NaiveBayesinLearning

Other examples at the bottom of this page:

Additional examples

And the Learn method is here:

learn doc method

+5
source share
1 answer

In accordance with the comments and ideas from them, I suspected the values ​​of the matrix. So I researched it:

problem

As shown in the figure above, some lines have zero values. The input matrix is ​​generated by Codification, which is used in the examples here:

NaiveBayes

with documents below:

Coding Documentation

codification -1 was null. Like the screenshot below:

one of the problematic entries

So my solution replaced null with "null" . But there may be better solutions.

Now the calling method containing fixed data looks like this:

  public LearningResultViewModel Learn(EMVDBContext dBContext, string userId, LearningAlgorithm learningAlgorithm) { var learningDataRaw = dBContext.Mutants .Include(mu => mu.MutationOperator) .Where(mu => mu.Equivalecy == 0 || mu.Equivalecy == 10); string[] featureTitles = new string[] { "ChangeType", "OperatorName", "OperatorBefore", "OperatorAfter", }; string[][] learningInputNotCodified = learningDataRaw.Select(ldr => new string[] { ldr.ChangeType.ToString(), ldr.MutationOperator.Name??"null", ldr.MutationOperator.Before??"null", ldr.MutationOperator.After??"null", }).ToArray(); int[] learningOutputNotCodified = learningDataRaw.Select(ldr => ldr.Equivalecy == 0 ? 0 : 1).ToArray(); #region Codification phase // Create a new codification codebook to // convert strings into discrete symbols Codification codebook = new Codification(featureTitles, learningInputNotCodified); // Extract input and output pairs to train int[][] learningInput = codebook.Transform(learningInputNotCodified); switch (learningAlgorithm) { case LearningAlgorithm.NaiveBayesian: return learningService.NaiveBayes(learningInput, learningOutputNotCodified); break; case LearningAlgorithm.SVM: break; default: break; } #endregion return null; } 

I would like this to help others facing the same problem.

+3
source

Source: https://habr.com/ru/post/1271115/


All Articles