C++语言实现朴素贝叶斯算法

朴素贝叶斯(Naive Bayes)是一种简单但效果良好的分类算法,基于贝叶斯定理并假设特征之间相互独立。以下是一个使用C++语言实现朴素贝叶斯算法的基本示例。

假设我们有一个简单的分类问题,例如通过几个特征预测一封电子邮件是否是垃圾邮件。

代码实现:

#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <string>

class NaiveBayes {
public:
    NaiveBayes() {}

    // 训练模型
    void train(const std::vector<std::vector<std::string>>& X, const std::vector<std::string>& y) {
        for (size_t i = 0; i < X.size(); ++i) {
            const std::string& label = y[i];
            ++classCounts[label];
            for (const std::string& feature : X[i]) {
                ++featureCounts[std::make_pair(feature, label)];
            }
        }

        // 计算每个类别的先验概率
        for (const auto& pair : classCounts) {
            priorProbs[pair.first] = static_cast<double>(pair.second) / X.size();
        }

        // 计算条件概率
        for (const auto& pair : featureCounts) {
            const std::string& feature = pair.first.first;
            const std::string& label = pair.first.second;
            conditionalProbs[pair.first] = static_cast<double>(pair.second) / classCounts[label];
        }
    }

    // 预测新样本的类别
    std::string predict(const std::vector<std::string>& X) {
        std::string bestLabel;
        double maxProb = -1.0;

        for (const auto& pair : classCounts) {
            const std::string& label = pair.first;
            double prob = std::log(priorProbs[label]);

            for (const std::string& feature : X) {
                prob += std::log(conditionalProbs[std::make_pair(feature, label)]);
            }

            if (prob > maxProb) {
                maxProb = prob;
                bestLabel = label;
            }
        }

        return bestLabel;
    }

private:
    std::map<std::string, int> classCounts;  // 类别计数
    std::map<std::pair<std::string, std::string>, int> featureCounts;  // 特征计数
    std::map<std::string, double> priorProbs;  // 先验概率
    std::map<std::pair<std::string, std::string>, double> conditionalProbs;  // 条件概率
};

int main() {
    // 训练数据集
    std::vector<std::vector<std::string>> X = {
        {"cheap", "viagra"},
        {"win", "lottery"},
        {"cheap", "lottery"},
        {"hello", "friend"},
        {"dear", "friend"}
    };

    std::vector<std::string> y = {
        "spam",
        "spam",
        "spam",
        "ham",
        "ham"
    };

    NaiveBayes nb;
    nb.train(X, y);

    // 测试样本
    std::vector<std::string> testSample = {"cheap", "win"};
    std::string prediction = nb.predict(testSample);

    std::cout << "Prediction: " << prediction << std::endl;

    return 0;
}

代码解释:

  1. 训练数据X是一个特征矩阵,每一行表示一个样本,每一列表示一个特征;y是标签向量,表示每个样本的类别。

  2. 训练模型

    • train() 函数计算每个类别的先验概率,并计算在给定类别下,每个特征的条件概率。
  3. 预测

    • predict() 函数使用先验概率和条件概率来计算给定输入的类别,并输出最可能的类别。

总结:

该实现适用于简单的文本分类问题。对于更复杂的数据,可以使用不同的数据结构和方法来处理特征工程、平滑处理等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚丁号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值