700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > [Machine Learning]朴素贝叶斯(NaiveBayes)

[Machine Learning]朴素贝叶斯(NaiveBayes)

时间:2021-11-12 15:18:14

相关推荐

[Machine Learning]朴素贝叶斯(NaiveBayes)

C++ 描述:

1 #include <iostream> 2 #include <string> 3 #include <fstream> 4 #include <sstream> 5 #include <vector> 6 #include <map> 7 #include <set> 8 9 using namespace std;10 11 class NaiveBayes {12 public:13void load_data(string path);14void train_model();15int predict(const vector<int> &item);16 private:17vector<vector<int>> data;18map<pair<int, int>, double> c_p; //conditional prob19map<int, double> p_p; // prior prob20 };21 22 void NaiveBayes::load_data(string path) {23ifstream fin(path.c_str());24if (!fin) {25 cerr << "open file error" << endl;26 exit(1);27}28 29string line;30while (getline(fin, line)) {31 if (line.size() > 1) {32 stringstream sin(line);33 int elem;34 vector<int> tmp;35 while (sin >> elem) {36 tmp.push_back(elem);37 }38 data.push_back(tmp);39 }40}41fin.close();42 }43 44 void NaiveBayes::train_model() {45for (auto &d : data) {46 int len = d.size();47 p_p[d[len - 1]] += (1.0 / data.size());48}49 50for (auto &p : p_p) {51 int label = p.first;52 double prior = p.second;53 for (auto &d : data) {54 for (int i = 0; i < d.size(); ++i) {55 c_p[make_pair(d[i], label)] += (1.0 / (prior * data.size()));56 }57 }58}59 }60 61 int NaiveBayes::predict(const vector<int> &item) { 62int result;63double max_prob = 0.0;64for (auto &p : p_p) {65 int label = p.first;66 double prior = p.second;67 double prob = prior;68 for (int i = 0; i < item.size() - 1; ++i) {69 prob *= c_p[make_pair(item[i], label)];70 }71 72 if (prob > max_prob) {73 max_prob = prob;74 result = label;75 }76}77 78return result;79 }80 81 int main() {82NaiveBayes naive_bayes;83naive_bayes.load_data(string("result.txt"));84naive_bayes.train_model();85 86vector<int> item{2, 4};87cout << naive_bayes.predict(item);88return 0;89 }

数据集:

1 4 -11 5 -11 5 11 4 11 4 -12 4 -12 5 -12 5 12 6 12 6 13 6 13 5 13 5 13 6 13 6 -1

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。