代码之家  ›  专栏  ›  技术社区  ›  ConductedClever

一致net NaiveBayesLearning“索引超出数组界限”

  •  6
  • ConductedClever  · 技术社区  · 9 年前

    我正在使用Accord。net 3.7.0在dot net core 1.1中。

    我使用的算法是朴素贝叶斯。学习机制的源代码如下:

        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;
        }
    

    发生错误。

    因为我无法在 Learn

    Run-time error screen shot

    没有额外的信息,没有内在的例外,没有想法!!!

    TG。

    //更新_1***

    Inputs

    每行有4列。手工检查(如果需要,我也可以分享它的视频!!!)

    输出阵列为180阵列,如下所示:

    Outputs

    NaiveBayesinLearning

    本页底部有更多示例:

    More examples

    和 learn

    learn method doc

    1 回复  |  直到 9 年前
        1
  •  4
  •   ConductedClever    9 年前

    根据他们的评论和想法,我怀疑矩阵的价值。所以我调查了一下:

    problem

    如上图所示,一些行的值低于零值。输入矩阵是通过编码生成的,在这里的示例中使用:

    NaiveBayes

    Codification docs

    编码-1的值为null。如下图所示:

    one of problematic records

    所以我的解决方案是替换 null "null" . 但可能有更好的解决方案。

    现在,包含固定数据的调用方方法如下所示:

        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;
        }