This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathAItraining.cpp
More file actions
152 lines (120 loc) · 3.8 KB
/
AItraining.cpp
File metadata and controls
152 lines (120 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#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;
}