首页 > 解决方案 > 朴素贝叶斯 - 没有类别标签 1 的样本

问题描述

我正在使用accord.net。我已经成功实现了两个决策树算法 ID3 和 C4.5,现在我正在尝试实现 Naive Bays 算法。虽然网站上有很多示例代码,但其中大部分似乎已经过时,或者存在各种问题。

到目前为止我在网站上找到的最好的示例代码在这里: http ://accord-framework.net/docs/html/T_Accord_MachineLearning_Bayes_NaiveBayes_1.htm

但是,当我尝试针对我的数据运行该代码时,我得到:

类标签 1 没有样本。请确保类标签是连续的,并且每个标签至少有一个训练样本。

当我在我的代码中调用 learner.learn(inputs, outputs) 时,来自该文件的第 228 行: https ://github.com/accord-net/framework/blob/master/Sources/Accord.MachineLearning/Tools.cs。

在实现其他两个回归树时,我已经遇到了一致的 Null 错误,并且我的数据已经针对该问题进行了清理。

是否有任何accord.net 专家知道什么会触发此错误?

我的代码摘录:

    var codebook = new Codification(fulldata, AllAttributeNames);

    /*
     * Get list of all possible combinations
     * Status software blows up if it encounters a value it has not seen before.
     */
    var attributList = new List<IUnivariateFittableDistribution>();
    foreach (var attr in DeciAttributeNames)
    {
        {
            /*
             * By default we'll use a standard static list of values for this column
             */
            var cntLst = codebook[attr].NumberOfSymbols;

            // no decisions can be made off of the variable if it is a constant value
            if (cntLst > 1)
            {
                KeptAttributeNames.Add(attr);
                attributList.Add(new GeneralDiscreteDistribution(cntLst));
            }
        }
    }

    var data = fulldata.Copy(); // this is a datatable

    /*
     * Translate our training data into integer symbols using our codebook
     */
    DataTable symbols = codebook.Apply(data, AllAttributeNames);
    double[][] inputs = symbols.ToJagged<double>(KeptAttributeNames.ToArray());
    int[] outputs = symbols.ToArray<int>(OutAttributeName);
    progBar.PerformStep();

    /*
     * Create a new instance of the learning algorithm
     * and build the algorithm
     */
    var learner = new NaiveBayesLearning<IUnivariateFittableDistribution>()
    {
        // Tell the learner how to initialize the distributions
        Distribution = (classIndex, variableIndex) => attributList[variableIndex]
    };

    var alg = learner.Learn(inputs, outputs);

编辑:经过进一步的实验,似乎这个错误只发生在我处理一定数量的行时。如果我处理 60 行或更少,我很好,如果我处理 500 行或更多,那么我很好。但是在这个范围之间我抛出了这个错误。根据我选择的数据量,错误消息中的索引号可以改变,我看到它的范围是 0 到 2。

所有数据都来自同一个 sql server 数据源,我唯一要调整的是查询的 Select Top ### 部分。

标签: machine-learningnaivebayesaccord.net

解决方案


当您定义没有任何样本数据的标签时,您将在多类场景中收到此错误。使用小数据集,您的随机抽样可能会偶然排除具有给定标签的所有观察结果。


推荐阅读