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

问题描述

不久前我问了一个关于 Accord.net 朴素贝叶斯算法抛出错误的问题。事实证明,这是因为我使用了离散值输入列,但没有为我为该列列出的所有值提供足够的训练数据。

现在我得到了完全相同的错误,只是这一次只有当我对输出列使用连续值时才会触发它。特别是整数数据类型的输出列。因为它是一个整数,所以 Codification 类没有翻译它,所以值直接传递给朴素贝叶斯算法,而算法显然无法处理这个问题。

如果我手动将列数据类型更改为字符串并通过 Codification 类将其发送以进行编码,然后通过它正常工作的算法发送结果。

这个算法不能将连续数据类型作为输出处理有什么特别的原因吗?是否需要启用某些设置才能使其正常工作?

一些示例代码:

        DataTable symbols = TrainingCodebook.Apply(DataTraining, AllAttributeNames);
        double[][] inputs = symbols.ToJagged<double>(KeptAttributeNames.ToArray());
        // *** The line that is breaking ***
        int[] outputs = symbols.ToArray<int>(outputCol);

        // *** The replacement test code that does work ***
        // DataStringTraining is the same as DataTraining, but all values are strings
        //Codification codeee = new Codification(DataStringTraining, outputCol);
        //var sym = codeee.Apply(DataStringTraining, outputCol);
        //int[] outputs = sym.ToArray<int>(outputCol);

        /*
         * 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],
        };

        NaiveBayes<IUnivariateFittableDistribution> alg = null;
        try
        {
            ProgPerformStep("Computing and training algorithm");
            alg = learner.Learn(inputs, outputs);
        }
        catch (Exception ex)
        {
            ProgPerformStep($"ERROR: Naive Bayes: {ex.Message}", ex);
            return;
        }

标签: machine-learningnaivebayesaccord.net

解决方案


我对此没有很好的答案,但是我相信正在发生的是我正在使用的算法在accord.net 网站上列为分类算法。

根据这里的一些阅读,我认为分类算法无法处理连续的输出值。

我可能需要切换到使用回归算法来获得该特定功能。

鉴于此,该算法的解决方案是手动编码输出列,或先将其转换为字符串,以便编码库为我完​​成这项工作。


推荐阅读