ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

朴素贝叶斯

2021-11-06 23:30:38  阅读:138  来源: 互联网

标签:map string temp pro st 贝叶斯 朴素 size


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

using namespace std;

vector<string>split(const string& src,const string& delimiter);
void rejudge();
vector<vector<string> >vect;
map<string,int>category;
map<string,double>pro_map;

int main()
{
    string strLine;
    ifstream readfile("data.txt");
    if(!readfile)
    {
        cout<<"Fail to open file data!"<<endl;
        cout<<getchar();
        return 0;
    }
    else
    {
        cout<<"读取原数据如下:"<<endl;
        vector<vector<string> >::size_type st_x;
        vector<string>::size_type st_y;
        vector<string>temp_vect;
        while(getline(readfile,strLine))
        {
            cout<<strLine<<endl;
            temp_vect=split(strLine,",");
            vect.push_back(temp_vect);
            temp_vect.clear();
        }
        string temp_string;
        vector<string>::size_type temp_size1=vect.size()-1;
        vector<string>::size_type temp_size2=vect[0].size();
        for(st_x=1; st_x<temp_size1+1; st_x++)
        {
            for(st_y=0; st_y<temp_size2; st_y++)
            {
                if(st_y!=temp_size2-1)
                {
                    temp_string=vect[0][st_y]+"="+vect[st_x][st_y]+"|"+vect[0][temp_size2-1]+"="+vect[st_x][temp_size2-1];
                    pro_map[temp_string]++;
                }
                else
                {
                    temp_string=vect[0][temp_size2-1]+"="+vect[st_x][temp_size2-1];
                    pro_map[temp_string]++;
                    category[vect[st_x][temp_size2-1]]=1;
                }
                temp_string.erase();
            }
        }
        string::size_type st;
        cout<<"统计过程如下:"<<endl;
        for(map<string,double>::iterator it=pro_map.begin(); it!=pro_map.end(); it++)
        {
            cout<<it->first<<":"<<it->second<<endl;
            if((st=it->first.find("|"))!=string::npos)
            {
                it->second=it->second/pro_map[it->first.substr(st+1)];
            }
        }
        cout<<"计算概率过程如下:"<<endl;
        for(map<string,double>::iterator it2=pro_map.begin(); it2!=pro_map.end(); it2++)
        {
            if((st=it2->first.find("|"))!=string::npos)
            {
                pro_map[it2->first]=pro_map[it2->first]/(double)temp_size1;
            }
            cout<<it2->first<<":"<<it2->second<<endl;
        }
        rejudge();
    }
    cout<<getchar();
    return 0;
}

vector<string>split(const string& src,const string& delimiter)
{
    string::size_type st;
    if(src.empty())
    {
        throw "Empty string!";
    }
    if(delimiter.empty())
    {
        throw "Empty delimiter!";
    }
    vector<string>vect;
    string::size_type last_st=0;
    while((st=src.find_first_of(delimiter,last_st))!=string::npos)
    {
        if(st!=last_st)
        {
            vect.push_back(src.substr(last_st,st-last_st));
        }
        last_st=st+1;
    }
    if(last_st!=src.size())
    {
        vect.push_back(src.substr(last_st,string::npos));
    }
    return vect;
}

void rejudge()
{
    string temp_string;
    double temp_pro;
    map<string,double>temp_map;
    cout<<"经过朴素贝叶斯算法重新分类的结果如下:"<<endl;
    for(vector<vector<string> >::size_type st_x=1; st_x<vect.size(); st_x++)
    {
        for(map<string,int>::iterator it=category.begin(); it!=category.end(); it++)
        {
            temp_pro=1.0;
            temp_string=vect[0][vect[0].size()-1]+"="+it->first;
            temp_pro*=pro_map[temp_string];
            temp_string.erase();
            for(vector<string>::size_type st_y=0; st_y<vect[st_x].size(); st_y++)
            {
                if(it==category.begin()&&st_y!=vect[st_x].size()-1)
                {
                    cout<<vect[st_x][st_y]<<" ";
                }
                if(st_y!=vect[st_x].size()-1)
                {
                    temp_string=vect[0][st_y]+"="+vect[st_x][st_y]+"|"+vect[0][vect[0].size()-1]+"="+it->first;
                    temp_pro*=pro_map[temp_string];
                    temp_string.erase();
                }
            }
            temp_map[it->first]=temp_pro;
        }

        string temp_string2;
        temp_pro=0;
        cout<<"后验概率:";
        for(map<string,double>::iterator it2=temp_map.begin(); it2!=temp_map.end(); it2++)
        {
            cout<<it2->first<<":"<<it2->second<<" ";
            if(it2->second>temp_pro)
            {
                temp_string2.erase();
                temp_string2=it2->first;
                temp_pro=it2->second;
            }
        }
        cout<<"归类:"<<vect[0][vect[0].size()-1]<<"="<<temp_string2<<endl;
    }
}

标签:map,string,temp,pro,st,贝叶斯,朴素,size
来源: https://blog.csdn.net/miceputil/article/details/121186400

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有