Bayes Classifier 分类

在模式识别的实际应用中,贝叶斯方法绝非就是post正比于prior*likelihood这个公式这么简单,一般而言我们都会用正态分布拟合likelihood来实现。
用正态分布拟合是什么意思呢?贝叶斯方法式子的右边有两个量,一个是prior先验概率,这个求起来很简单,就是一大堆数据中求某一类数据占的百分比就可以了,比如300个一堆的数据中A类数据占100个,那么A的先验概率就是1/3。第二个就是likelihood,likelihood可以这么理解:对于每一类的训练数据,我们都用一个multivariate正态分布来拟合它们(即通过求得某一分类训练数据的平均值和协方差矩阵来拟合出一个正态分布),然后当进入一个新的测试数据之后,就分别求取这个数据点在每个类别的正态分布中的大小,然后用这个值乘以原先的prior便是所要求得的后验概率post了。

C++实现简单贝叶斯分类

 
 
所用的数据文件为:weather.csv

outlook temperature humidity windy play
sunny hot high FALSE no
sunny hot high TRUE no
overcast hot high FALSE yes
rainy mild high FALSE yes
rainy cool normal FALSE yes
rainy cool normal TRUE no
overcast cool normal TRUE yes
sunny mild high FALSE no
sunny cool normal FALSE yes
rainy mild normal FALSE yes
sunny mild normal TRUE yes
overcast mild high TRUE yes
overcast hot normal FALSE yes
rainy mild high TRUE no

源代码:

/*
实现简单贝叶斯算法
Changfengmingzhi
*/
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<map>
usingnamespace std;
vector<string> split(conststring& src,conststring& delimiter);//根据定界符分离字符串
void rejudge();//重新判断原输入数据的类别
vector<vector<string>> vect;//二维容器
map<string,int> category;//存放类别
map<string,double> pro_map;//存放各种概率的map容器
int main()
{
string strLine;
ifstream readfile(".\\weather.csv");
if(!readfile)//打开文件失败!
{
cout<<"Fail to open file weather!"<<endl;
return0;
}
else
{
cout<<"读取原始数据如下:"<<endl;
vector<vector<string>>::size_type st_x;//二维容器x坐标
vector<string>::size_type st_y;//二维容器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();//总行数
vector<string>::size_type temp_size2=vect[0].size();//总列数
for(st_x=1;st_x<temp_size1;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]++;//计数加1
}
else//处理每一行的类别,统计类别的个数
{
temp_string=vect[0][temp_size2-1]+"="+vect[st_x][temp_size2-1];
pro_map[temp_string]++;//计数加1
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();
}

return0;
}
vector<string> split(conststring& src,conststring& 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)//2个标记间的字符串为一个子字符串
{
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++)//遍历类别,取出p(x|c1)和p(x|c2)等的概率值
{
temp_pro=1.0;
temp_string=vect[0][vect[0].size()-1]+"="+it->first;
temp_pro*=pro_map[temp_string];//乘上p(ci)
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)//乘上p(xi|cj),跳过最后一列,因为是类别而非属性
{
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];//乘上p(xi|cj)
temp_string.erase();
}
}
temp_map[it->first]=temp_pro;//存下概率
}
//////////根据概率最大判断哪个该条记录应属于哪个类别
string temp_string2;
temp_pro=0;//初始化概率为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;//输出该条记录所属的类别
}
}

http://blog.sina.com.cn/s/blog_4fb4d8d40100nrcx.html

http://blog.csdn.net/xlm289348/article/details/8876862

Bayes Classifier 分类

在模式识别的实际应用中,贝叶斯方法绝非就是post正比于prior*likelihood这个公式这么简单,一般而言我们都会用正态分布拟合likelihood来实现。
用正态分布拟合是什么意思呢?贝叶斯方法式子的右边有两个量,一个是prior先验概率,这个求起来很简单,就是一大堆数据中求某一类数据占的百分比就可以了,比如300个一堆的数据中A类数据占100个,那么A的先验概率就是1/3。第二个就是likelihood,likelihood可以这么理解:对于每一类的训练数据,我们都用一个multivariate正态分布来拟合它们(即通过求得某一分类训练数据的平均值和协方差矩阵来拟合出一个正态分布),然后当进入一个新的测试数据之后,就分别求取这个数据点在每个类别的正态分布中的大小,然后用这个值乘以原先的prior便是所要求得的后验概率post了。

C++实现简单贝叶斯分类

 
 
所用的数据文件为:weather.csv

outlook temperature humidity windy play
sunny hot high FALSE no
sunny hot high TRUE no
overcast hot high FALSE yes
rainy mild high FALSE yes
rainy cool normal FALSE yes
rainy cool normal TRUE no
overcast cool normal TRUE yes
sunny mild high FALSE no
sunny cool normal FALSE yes
rainy mild normal FALSE yes
sunny mild normal TRUE yes
overcast mild high TRUE yes
overcast hot normal FALSE yes
rainy mild high TRUE no

源代码:

/*
实现简单贝叶斯算法
Changfengmingzhi
*/
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<map>
usingnamespace std;
vector<string> split(conststring& src,conststring& delimiter);//根据定界符分离字符串
void rejudge();//重新判断原输入数据的类别
vector<vector<string>> vect;//二维容器
map<string,int> category;//存放类别
map<string,double> pro_map;//存放各种概率的map容器
int main()
{
string strLine;
ifstream readfile(".\\weather.csv");
if(!readfile)//打开文件失败!
{
cout<<"Fail to open file weather!"<<endl;
return0;
}
else
{
cout<<"读取原始数据如下:"<<endl;
vector<vector<string>>::size_type st_x;//二维容器x坐标
vector<string>::size_type st_y;//二维容器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();//总行数
vector<string>::size_type temp_size2=vect[0].size();//总列数
for(st_x=1;st_x<temp_size1;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]++;//计数加1
}
else//处理每一行的类别,统计类别的个数
{
temp_string=vect[0][temp_size2-1]+"="+vect[st_x][temp_size2-1];
pro_map[temp_string]++;//计数加1
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();
}

return0;
}
vector<string> split(conststring& src,conststring& 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)//2个标记间的字符串为一个子字符串
{
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++)//遍历类别,取出p(x|c1)和p(x|c2)等的概率值
{
temp_pro=1.0;
temp_string=vect[0][vect[0].size()-1]+"="+it->first;
temp_pro*=pro_map[temp_string];//乘上p(ci)
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)//乘上p(xi|cj),跳过最后一列,因为是类别而非属性
{
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];//乘上p(xi|cj)
temp_string.erase();
}
}
temp_map[it->first]=temp_pro;//存下概率
}
//////////根据概率最大判断哪个该条记录应属于哪个类别
string temp_string2;
temp_pro=0;//初始化概率为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;//输出该条记录所属的类别
}
}

模式识别之bayes---bayes 简单天气预测实现实例的更多相关文章

  1. 一个简单的Android小实例

    原文:一个简单的Android小实例 一.配置环境 1.下载intellij idea15 2.安装Android SDK,通过Android SDK管理器安装或卸载Android平台   3.安装J ...

  2. mongodb 简单部署方案及实例

    mongodb 简单部署方案及实例 转载:http://my.oschina.net/zhuzhu0129/blog/53290 第一节 准备工作 一 安装mongodb  我这里选用rehl 5.6 ...

  3. Linux下简单的socket通信实例

    Linux下简单的socket通信实例 If you spend too much time thinking about a thing, you’ll never get it done. —Br ...

  4. 一个简单的jQuery插件开发实例

    两年前写的一个简单的jQuery插件开发实例,还是可以看看的: <script type="text/javascript" src="jquery-1.7.2.m ...

  5. springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目

    一个简单的用户登录系统 用户有账号密码,登录ip,登录时间 打开登录页面,输入用户名密码 登录日志,可以记录登陆的时间,登陆的ip 成功登陆了的话,就更新用户的最后登入时间和ip,同时记录一条登录记录 ...

  6. [WCF REST] 一个简单的REST服务实例

    Get:http://www.cnblogs.com/artech/archive/2012/02/04/wcf-rest-sample.html [01] 一个简单的REST服务实例 [02] We ...

  7. PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例

    前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...

  8. Hibernate入门2.简单的项目开发实例

    Hibernate入门2.简单的项目开发实例 这一节通过一个简单的项目学习Hibernate项目的配置 代码下载 : 链接: http://pan.baidu.com/s/1zlgjl 密码: p34 ...

  9. 简单的Slony-I设置实例 II

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页 接前面例子, 简单的Slony-I设置实例 这次我 ...

随机推荐

  1. 每日英语:Proactive Advice for Dealing With Grief: Seek Out New Experiences

    When her husband died of cancer 10 years ago, Becky Aikman says she experienced grief and adapted to ...

  2. tensorflow笔记3:CRF函数:tf.contrib.crf.crf_log_likelihood()

    在分析训练代码的时候,遇到了,tf.contrib.crf.crf_log_likelihood,这个函数,于是想简单理解下: 函数的目的:使用crf 来计算损失,里面用到的优化方法是:最大似然估计 ...

  3. 【Kryo】简单地使用Kryo

    公司用Kryo,先接触下,简单记录下. 引入包 <dependencies> <dependency> <groupId>com.esotericsoftware& ...

  4. Dubbo实践笔记

    注意的地方 默认情况下,cluster=failover.retries=2,意为失败重试两次,不包含原生调用.如需配置不重试,需设置retries=-1,或者使用failfast(快速失败)模式 如 ...

  5. Android 支付宝接口调用

    在近期,公司需要开发一个关于在线支付的模块,所以需要用到第三方支付平台 转载请注明出处:http://blog.csdn.net/ht_android/article/details/45307165 ...

  6. Django服务端读取excel文件并且传输到接口

    path_name = "opboss_download_" + str(int(time.time())) + ".csv" print(path_name) ...

  7. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  8. Redis 相关操作

    1.安装 下载地址:http://www.redis.cn/ 在使用Redis时,开始就遇到了问题,客户端打不开,原因是需要先开启服务端,这需要先配置—— 1.下载好redis安装包,解压安装之后,复 ...

  9. iOS开发中的火星坐标系及各种坐标系转换算法

    原文地址:http://m.oschina.net/blog/619183?ref=myread 其原理是这样的:保密局开发了一个系统,能将实际的坐标转换成虚拟的坐标.所有在中国销售的数字地图必须使用 ...

  10. Virtual DOM 虚拟DOM的理解(转)

    作者:戴嘉华 转载请注明出处并保留原文链接( #13 )和作者信息. 目录: 1 前言 2 对前端应用状态管理思考 3 Virtual DOM 算法 4 算法实现 4.1 步骤一:用JS对象模拟DOM ...