Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added code for Bayesian algorithm #360

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions algorithms/artificial-intelligence/Bayesian/AItest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include<bits/stdc++.h>
using namespace std;

int main()
{
vector<string>test;
vector<string>feature;
vector<string>cls;
vector<string>like;
vector<string>mapc;

string str;
ifstream read("cat.txt");

while(getline (read,str))
{
test.push_back(str);
}
read.close();

/*for(int i=0;i<test.size();i++){
cout<<test[i]<<"\n";
}*/
FILE *fptr;
if ((fptr = fopen("likelihood.txt", "r")) == NULL)
{
printf("Error! opening file");
exit(1);
}

char f[100], c[100], l[100], m[100];
string c1,c2,m1,m2;
while(fscanf(fptr, "%s %s %s %s", f, c, l, m)!=EOF){
string s1="";
string s2="";
string s3="";
string s4="";

for(int i=0; i<strlen(f); i++){
s1 += f[i] ;
}
feature.push_back(s1);

for(int i=0; i<strlen(c); i++){
s2 += c[i] ;
}
cls.push_back(s2);

for(int i=0; i<strlen(l); i++){
s3 += l[i] ;
}
like.push_back(s3);

for(int i=0; i<strlen(m); i++){
s4 += m[i] ;
}
mapc.push_back(s4);
}

for(int i=0;i<test.size();i++){
for(int j=0;j<feature.size();j++){
if(test[i]==feature[j])
{
if(cls[j]=="dog")
{
c1 = cls[j];
m1 = mapc[j];
}
else
{
c2 = cls[j];
m2 = mapc[j];
}
}
}
if(m1>m2){
cout<<test[i]<<"\t--> "<<"dog"<<"\n";
}
else{
cout<<test[i]<<"\t--> "<<"cat"<<"\n";
}
}

return 0;

}
152 changes: 152 additions & 0 deletions algorithms/artificial-intelligence/Bayesian/AItraining.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include<bits/stdc++.h>
using namespace std ;

vector<string>avalue ; //attribute value
vector<string>cvalue ; //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<<avalue[j]<<"\t"<<cvalue[i]<<"\t"<<liked<<"\t\t"<<priorD*liked<<"\n";
cout<<liked<<endl;
}
else{
likec = (n*1.00)/catc;
priorC = (catc*1.00)/numberofobject;

myfile<<avalue[j]<<"\t"<<cvalue[i]<<"\t"<<likec<<"\t"<<priorC*likec<<"\n";
cout<<likec<<endl;
}
}
}
fclose(fptr);

//priorD = (dogc*1.00)/numberofobject;
//priorC = (catc*1.00)/numberofobject;

myfile<<"\n\n"<<"Prior\n-----\n"<<"P(Dog) = "<<priorD<<"\n";
myfile<<"\n"<<"P(Cat) = "<<priorC<<"\n";
myfile.close();

cout<<"\n"<<"Dog count: "<<dogc<<"\n"<<"Cat count: "<<catc<<"\n\n";

//cout<<priorC<<" "<<priorD<<"\n\n\n";


string myText;
ifstream readFile("likelihood.txt");

while (getline (readFile, myText)){
cout << myText<<"\n";
}
readFile.close();


return 0;
}

30 changes: 30 additions & 0 deletions algorithms/artificial-intelligence/Bayesian/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
big dog
big dog
big cat
big dog
small cat
small cat
small cat
small dog
small dog
small cat
big dog
big dog
big cat
big cat
big dog
small cat
big dog
small cat
big dog
small dog
small dog
small cat
small cat
big cat
big dog
medium cat
medium cat
big dog
medium dog
big dog
6 changes: 6 additions & 0 deletions algorithms/artificial-intelligence/Bayesian/testFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
big
big
small
medium
small
medium