用c++编写程序实现一个简单的二值贝叶斯分类器
时间: 2024-03-16 21:42:03 浏览: 18
好的,以下是一个简单的二值贝叶斯分类器的C++代码:
```c++
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cmath>
using namespace std;
class NaiveBayes {
public:
NaiveBayes(int n_features) : n_features(n_features) {}
void fit(vector<vector<int>>& X, vector<int>& y) {
// 统计每个类别的样本数
for (int i = 0; i < y.size(); ++i) {
++class_count[y[i]];
}
// 统计每个类别每个特征的取值次数
for (int i = 0; i < X.size(); ++i) {
for (int j = 0; j < n_features; ++j) {
feature_count[{y[i], j, X[i][j]}]++;
}
}
// 计算每个类别每个特征取值的概率
for (auto& p : class_count) {
int c = p.first;
int n_samples = p.second;
for (int j = 0; j < n_features; ++j) {
for (int k = 0; k <= 1; ++k) {
int count = feature_count[{c, j, k}];
feature_prob[{c, j, k}] = (count + 1.0) / (n_samples + 2.0);
}
}
}
}
int predict(vector<int>& x) {
double max_prob = -INFINITY;
int best_class = -1;
for (auto& p : class_count) {
int c = p.first;
double prob = log(p.second);
for (int j = 0; j < n_features; ++j) {
prob += log(feature_prob[{c, j, x[j]}]);
}
if (prob > max_prob) {
max_prob = prob;
best_class = c;
}
}
return best_class;
}
private:
int n_features;
unordered_map<int, int> class_count;
unordered_map<tuple<int, int, int>, int> feature_count;
unordered_map<tuple<int, int, int>, double> feature_prob;
};
int main() {
vector<vector<int>> X = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
vector<int> y = {0, 0, 1, 1};
NaiveBayes nb(2);
nb.fit(X, y);
vector<int> x = {0, 1};
cout << nb.predict(x) << endl; // 输出 0
return 0;
}
```
这个二值贝叶斯分类器使用了朴素贝叶斯算法,假设每个特征之间相互独立,计算每个类别每个特征取值的概率,然后根据贝叶斯公式计算每个类别的后验概率,最后选择后验概率最大的类别作为预测结果。