From 07ab4f6936d77526eaff45a3426a6e611f8686b5 Mon Sep 17 00:00:00 2001 From: farhan Date: Sun, 22 Jan 2023 23:33:06 +0600 Subject: [PATCH] added code for Bayesian algorithm --- .../Bayesian/AItest.cpp | 86 ++++++++++ .../Bayesian/AItraining.cpp | 152 ++++++++++++++++++ .../artificial-intelligence/Bayesian/data.txt | 30 ++++ .../Bayesian/testFile.txt | 6 + 4 files changed, 274 insertions(+) create mode 100644 algorithms/artificial-intelligence/Bayesian/AItest.cpp create mode 100644 algorithms/artificial-intelligence/Bayesian/AItraining.cpp create mode 100644 algorithms/artificial-intelligence/Bayesian/data.txt create mode 100644 algorithms/artificial-intelligence/Bayesian/testFile.txt diff --git a/algorithms/artificial-intelligence/Bayesian/AItest.cpp b/algorithms/artificial-intelligence/Bayesian/AItest.cpp new file mode 100644 index 00000000..d437c8ac --- /dev/null +++ b/algorithms/artificial-intelligence/Bayesian/AItest.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +int main() +{ + vectortest; + vectorfeature; + vectorcls; + vectorlike; + vectormapc; + + string str; + ifstream read("cat.txt"); + + while(getline (read,str)) + { + test.push_back(str); + } + read.close(); + + /*for(int i=0;im2){ + cout< "<<"dog"<<"\n"; + } + else{ + cout< "<<"cat"<<"\n"; + } + } + + return 0; + +} diff --git a/algorithms/artificial-intelligence/Bayesian/AItraining.cpp b/algorithms/artificial-intelligence/Bayesian/AItraining.cpp new file mode 100644 index 00000000..76e25a2d --- /dev/null +++ b/algorithms/artificial-intelligence/Bayesian/AItraining.cpp @@ -0,0 +1,152 @@ +#include +using namespace std ; + +vectoravalue ; //attribute value +vectorcvalue ; //class value + +int numberofobject = 0; +double priorD=0, priorC=0; + +int findOccurance(string att, string cl) +{ + FILE *fptr; + char a[100], c[100] ; //a-attribute, c-class + int count = 0 ; + if ((fptr = fopen("data.txt", "r")) == NULL) { + printf("Error! opening file"); + exit(1); + } + while (fscanf(fptr, "%s %s", a, c)!= EOF){ + string s = "" ; + string s2 = "" ; + + for(int i = 0 ; i < strlen(a) ; i++){ //converting char array into string + s += a[i] ; + } + for(int i = 0 ; i < strlen(c) ; i++){ + s2 += c[i] ; + } + if(s == att && s2 == cl) count++ ; + } + fclose(fptr); + return count ; +} + +int main() { + char a[100], c[100]; + FILE *fptr; + if ((fptr = fopen("data.txt", "r")) == NULL){ + printf("Error! opening file"); + exit(1); + } + string s = "" ; + + int dogc=0, catc=0; + + while (fscanf(fptr, "%s %s", a, c)!= EOF){ + //printf("%s %s", a, c); + //printf("\n") ; + numberofobject++ ; //counting total objects. + + bool flag = false ; + string s = "" ; + for(int i = 0 ; i < strlen(a) ; i++){ + s += a[i] ; + } + + for(int i = 0 ; i < avalue.size() ; i++){ + if(s == avalue[i]) { + flag = true ; + } + } + if(!flag) + avalue.push_back(s) ; + + + flag = false ; + s = "" ; + for(int i = 0 ; i < strlen(c) ; i++){ + s += c[i] ; + } + + for(int i = 0 ; i < cvalue.size() ; i++){ + if(s == cvalue[i]) { + flag = true ; + } + } + if(!flag) + cvalue.push_back(s) ; + + if(s=="dog"){ //counting number of dogs and cats. + dogc++; + } + if(s=="cat"){ + catc++; + } + } + + printf("%d\n\n", numberofobject) ; + for(int i = 0 ; i < avalue.size() ; i++){ + + cout<< avalue[i] << endl ; + } + + for(int i = 0 ; i < cvalue.size() ; i++){ + cout<< cvalue[i] << endl ; + } + + //calculating likelihood and writing in a file + ofstream myfile("likelihood.txt"); + myfile<<"Feature"<<"\t"<<"Class"<<"\t"<<"Likelihood"<<"\t"<<"Classifier"<<"\n"; + myfile<<"-------"<<"\t"<<"-----"<<"\t"<<"-----------"<<"\t"<<"----------"<<"\n"; + + for(int i = 0 ; i < cvalue.size() ; i++){ + for(int j = 0 ; j < avalue.size() ; j++){ + int n = findOccurance(avalue[j], cvalue[i]) ; //finding occurences of big-dog, small-cat, medium-dog etc. + + cout << avalue[j] << "|" << cvalue[i] << " = "<< n << endl; + + //calculating likelihood + double liked=0, likec=0; + if(cvalue[i]=="dog"){ + liked = (n*1.00)/dogc; + priorD = (dogc*1.00)/numberofobject; + + myfile<