/
program2.cpp
134 lines (115 loc) · 3.02 KB
/
program2.cpp
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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <utility>
#include <fstream>
#include <stdlib.h>
#include <cmath>
using namespace std;
#define px 28
#define laplace 0.1
char testImages[] = "testimages.txt",
testLabels[] = "testlabels.txt",
trainingImages[] = "trainingimages.txt",
trainingLabels[] = "traininglabels.txt";
int arr[10][28][28][3] = {0}, answers[1009], results[1009], trainResults[5009], countLabels[10] = {0};
double likelihood[10][28][28][3];
ofstream fout;
ifstream fin;
void getData(){
int ithlabel;
char line[30];
fin.open(trainingLabels);
for(int i=0; i<5000; i++){
fin>>ithlabel;
trainResults[i] = ithlabel; //storing the resultant digit of training images
countLabels[ithlabel]++; //storing frequency of each class
}
fin.close(); //close training labels
fin.open(trainingImages);
for(int i=0; i<5000; i++){
for(int j=0; j<28; j++){
fin.getline(line, 30);
for(int k=0; k<28; k++){
if(line[k] == ' ') arr[ trainResults[i] ][j][k][0]++;
else if(line[k] == '#') arr[ trainResults[i] ][j][k][1]++;
else arr[ trainResults[i] ][j][k][2]++;
}
}
}
fin.close(); //close training images
}
void train(){
double a,b;
for(int c=0; c<10; c++){
for(int i=0; i<28; i++){
for(int j=0; j<28; j++){
a = (double)( arr[c][i][j][0] + laplace);
b = (double)( countLabels[c] + laplace*2);
likelihood[c][i][j][0] = a/b; //no of times pixel(i,j) has value=0 for class c / no of occurences of class c
a = (double)( arr[c][i][j][1] + laplace);
likelihood[c][i][j][1] = a/b; //no of times pixel(i,j) has value=1 for class c / no of occurences of class c
a = (double)( arr[c][i][j][2] + laplace);
likelihood[c][i][j][2] = a/b; //no of times pixel(i,j) has value=2 for class c / no of occurences of class c
}
}
}
}
void getTestLabels(){
int ithlabel;
fin.open(testLabels);
for(int i=0; i<1000; i++){
fin>>ithlabel;
answers[i] = ithlabel;
}
fin.close();
}
void testing(){
char line[30][30];
double val=1, max=-5.0;
fin.open(testImages);
for(int img=0; img<1000; img++){
max = -5.0;
for(int i=0; i<28; i++)
fin.getline(line[i], 100);
for(int cls=0; cls<10; cls++){
val = 1.7e300;
for(int i=0; i<28; i++){
//cout<<line[i]<<endl;
for(int j=0; j<28; j++){
if(line[i][j] == ' ')
val = val * likelihood[cls][i][j][0];
else if(line[i][j] == '+')
val = val * likelihood[cls][i][j][2];
else
val = val * likelihood[cls][i][j][1];
}
}
//cout<<img<<":"<<cls<<":"<<val<<endl;
if(val > max){
//cout<<img<<":"<<cls<<":"<<val<<endl;
max = val;
results[img] = cls;
}
}
}
fin.close();
}
void getAccuracy(){
int ct=0;
for(int i=0; i<1000; i++){
if(answers[i] == results[i]) ct++;
if(i%100 == 0) cout<<ct<<"/"<<i<<endl;
//if(answers[i] != results[i]) cout<<i<<":"<<answers[i]<<":"<<results[i]<<endl;
}
cout<<"accuracy is "<<ct<<"/1000\n";
}
int main(int argc, char *argv[]){
getData();
train();
getTestLabels();
testing();
getAccuracy();
return 0;
}