• 大小: 1.88MB
    文件类型: .zip
    金币: 2
    下载: 0 次
    发布日期: 2024-01-24
  • 语言: C/C++
  • 标签: 算法  分类器  C实现  

资源简介

朴素贝叶斯算法(C语言实现)(来自网络)

资源截图

代码片段和文件信息


#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

//Attribute-Class relation table
class Class{
public:
  Class();
  //continue attribute
  double mu sigma;
  vector vals;
  //discrete attribute
  map val_count;
};

Class::Class(){
  mu=sigma=0;
}

class Attribute{
public:
  bool isContinue;
  vector classes;
};

class NaiveBayesian{
public:
  int D;
  vector attr;
  map classes;
  vector class_count;

  int read_attribute(char* attr_file);
  int train(char* train_file);
  int classify();

  int print();
};

int NaiveBayesian::classify(){
  string temp;
  char comma;
  double d_val; int i_val;
  Class* cp;
  vector pri;
  double pi=acos(-1.0);
  map::iterator iter;
  map::iterator iter2 idx;
  int count;
  double tmp_pri;

  for(;cin>>temp;){
    pri.clear();
    for(int i=0; i      pri.push_back(1.0);
    }

    istringstream ss(temp);
    //ss.str(temp);
    //cout<    for(int i=0; i      if(attr[i].isContinue){
//continue value
if(!(ss>>d_val))return -1;
for(int j=0; j   cp=&attr[i].classes[j];
  tmp_pri=exp(-pow(d_val-cp->mu 2.0)/(2.0*pow(cp->sigma 2.0)))/(cp->sigma*sqrt(2.0*pi));
  pri[j]*=tmp_pri;

  //cout< }
      }else{
//discrete value
if(!(ss>>i_val))return -1;
for(int j=0; j   cp=&attr[i].classes[j];
  iter=cp->val_count.find(i_val);
  if(iter==cp->val_count.end())count=0;
  else count=iter->second;
  tmp_pri=((double)count)/((double)class_count[j]);
  pri[j]*=tmp_pri;

  //cout< }
      }
      if(i ss>>comma;
    }

    /*
    for(int i=0; i      cout<    */

    iter2=idx=classes.begin();
    for(iter2++; iter2!=classes.end(); iter2++){
      if(pri[iter2->second]*class_count[iter2->second]>pri[idx->second]*class_count[idx->second])
idx=iter2;
    }
    cout<first<<‘ ‘<second]<    //cout<  }

  return 1;
}

int NaiveBayesian::print(){
  for(int i=0; i    for(int j=0; j      cout<      Class* cp=&attr[i].classes[j];
      if(attr[i].isContinue){
cout<mu<<‘ ‘<sigma<      }else{
for(map::iterator iter=cp->val_count.begin(); 
    iter!=cp->val_count.end(); iter++){
  cout<first<<“::“<second< }
      }
      cout<    }
  }

  return 1;
}

int NaiveBayesian::train(char* train_file){
  fstream fstr;
  fstr.open(train_file fstream::in);
  if(!fstr.is_open())
    return -1;

  char comma;
  string class_str;
  string temp;
  int i_val; double d_val;
  Class* cp;
  map::iterator iter;
  for(D=0; fstr>>temp; D++){
    class_str=temp.substr(te

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2010-02-26 21:02  NaiveBayesian\
     目录           0  2010-02-26 21:02  NaiveBayesian\adult\
     文件         208  2009-06-05 20:57  NaiveBayesian\adult\adult.attr
     文件     1359888  2009-06-05 21:53  NaiveBayesian\adult\adult_p.data
     文件      697878  2009-06-06 22:33  NaiveBayesian\adult\adult_p.test
     目录           0  2010-02-26 21:02  NaiveBayesian\data_proc\
     文件         208  2009-06-05 20:57  NaiveBayesian\data_proc\adult.attr
     文件     3974305  2009-06-05 20:34  NaiveBayesian\data_proc\adult.data
     文件        5262  2009-06-05 20:55  NaiveBayesian\data_proc\adult.names
     文件     2003132  2009-06-05 21:13  NaiveBayesian\data_proc\adult.test
     文件     1359888  2009-06-05 21:53  NaiveBayesian\data_proc\adult_p.data
     文件      697878  2009-06-06 22:33  NaiveBayesian\data_proc\adult_p.test
     文件          55  2009-06-05 21:26  NaiveBayesian\data_proc\Makefile
     文件      209696  2009-06-05 21:53  NaiveBayesian\data_proc\proc
     文件        1158  2009-06-05 21:53  NaiveBayesian\data_proc\proc.cpp
     目录           0  2010-02-26 21:02  NaiveBayesian\error_rate\
     文件      697878  2009-06-06 22:33  NaiveBayesian\error_rate\adult_p.test
     文件       40136  2009-06-08 12:11  NaiveBayesian\error_rate\error
     文件         612  2009-06-08 12:10  NaiveBayesian\error_rate\error.cpp
     文件          60  2009-06-08 12:11  NaiveBayesian\error_rate\Makefile
     文件      288743  2009-06-24 22:38  NaiveBayesian\error_rate\result_adult
     目录           0  2010-02-26 21:02  NaiveBayesian\Iris\
     文件         116  2009-06-03 18:49  NaiveBayesian\Iris\Iris.attr
     文件        4551  2009-06-03 15:14  NaiveBayesian\Iris\iris.data
     文件        2998  2009-06-03 15:14  NaiveBayesian\Iris\iris.names
     文件         106  2009-06-03 18:52  NaiveBayesian\Makefile
     文件      315267  2009-06-24 22:32  NaiveBayesian\naive_bayesian
     文件        4723  2009-06-24 22:31  NaiveBayesian\naive_bayesian.cpp
     文件      288743  2009-06-06 22:44  NaiveBayesian\result_adult
     文件        3481  2009-06-05 20:27  NaiveBayesian\result_Iris
     文件          47  2009-06-04 14:41  NaiveBayesian\test.attr
............此处省略2个文件信息

评论

共有 条评论