-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaplaceBayes.c
198 lines (154 loc) · 4.03 KB
/
LaplaceBayes.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SAMPLE_CNT 150
#define ATTR_CNT 4
#define CLASS_CNT 3
struct Iris{
double attr[ATTR_CNT];
int label;
};
char pr[20]; //鐢ㄤ簬瀛樻斁绫诲埆
struct Iris Iris[SAMPLE_CNT];
int labnum[CLASS_CNT]={0};
double classId[SAMPLE_CNT-1]; //存放训练样本的类号
double AllEachattr[SAMPLE_CNT-1]; //所有训练样本某一属性
//Iris-setosa=1
//Iris-versicolor=2
//Iris-virginica=3
void ReadFile()
{
FILE *fi;
int i=0;
fi=fopen("Iris.txt","rb");
while(!feof(fi))
{
fscanf(fi,"%lf,%lf,%lf,%lf,%s",&Iris[i].attr[0],&Iris[i].attr[1],&Iris[i].attr[2],&Iris[i].attr[3],pr);
if(!strcmp(pr,"Iris-setosa"))
Iris[i].label=1;
if(!strcmp(pr,"Iris-versicolor"))
Iris[i].label=2;
if(!strcmp(pr,"Iris-virginica"))
Iris[i].label=3;
labnum[Iris[i].label-1]++;
i++;
}
}
void swap(double *a,double *b)
{
double tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void Bubblesort(double a[],int p,int n)
{//p表示起始下标,n为个数
int i, j, temp;
for (j = p; j <p + n - 1; j++)
for (i = p; i <p + n - 1 - j; i++)
if(a[i] > a[i + 1])
{
swap(&a[i],&a[i+1]);
swap(&AllEachattr[i],&AllEachattr[i+1]);
}
}
void Bubblesort1(double a[],int p,int n)
{//p表示起始下标,n为个数
int i, j, temp;
for (j = p; j <p + n - 1; j++)
for (i = p; i <p + n - 1 - j; i++)
if(a[i] > a[i + 1])
{
swap(&a[i],&a[i+1]);
}
}
int DifferNum(double *w,int p,int n)
{
int j,count=1;
for(j=p;j<p+n-1;j++)
if(w[j]!=w[j+1])
count++;
return count;
}
void process()
{
int i,j,k,t;
int plabel; //棰勬祴鐨勭被鍒�
int predT=0; //棰勬祴姝g‘
int label; //瀛樻斁娴嬭瘯鏍锋湰绫诲埆
int count[CLASS_CNT];
int h=0;
double attr[ATTR_CNT]; //瀛樻斁娴嬭瘯鏍锋湰灞炴��
double attrNum[CLASS_CNT]; //姣忎竴绫诲拰娴嬭瘯鏍锋湰灞炴�х浉鍚岀殑涓暟
double prob[CLASS_CNT];
double maxscore;
ReadFile();
for(i=0;i<SAMPLE_CNT;i++)
{
//娴嬭瘯鏍锋湰 (浣跨敤鐣欎竴娉�)
label = Iris[i].label;
labnum[label-1]--;
//绫诲埆姒傜巼
prob[0]=(double)labnum[0]/(SAMPLE_CNT+CLASS_CNT-1); //Laplace smooth
prob[1]=(double)labnum[1]/(SAMPLE_CNT+CLASS_CNT-1);
prob[2]=(double)labnum[2]/(SAMPLE_CNT+CLASS_CNT-1);
for(k=0;k<SAMPLE_CNT-1;k++)
classId[k]=0;
//娴嬭瘯鏍锋湰绗琷涓睘鎬�
for(j=0;j<ATTR_CNT;j++)
{
t=0;
attrNum[0]=0;
attrNum[1]=0;
attrNum[2]=0;
//璁粌鏍锋湰涓拰娴嬭瘯鏍锋湰绗琷涓睘鎬х浉鍚岀殑涓暟鏀惧叆attrNum
for(k=0;k<SAMPLE_CNT;k++)
{
if((k!=i))
{
if(Iris[i].attr[j]==Iris[k].attr[j])
attrNum[Iris[k].label-1]++;
if(j==0)
classId[t]=Iris[k].label;
// printf("sdfsdfadf\n");
AllEachattr[t++]=Iris[k].attr[j];
}
}
//对类号排序,属性值也跟着动
Bubblesort(classId,0,SAMPLE_CNT-1);
for(k=0;k<CLASS_CNT;k++)
{
if(k==0)
{
Bubblesort1(AllEachattr,0,labnum[k]);
count[k]=DifferNum(AllEachattr,0,labnum[k]);
}
else
{
Bubblesort1(AllEachattr,labnum[k-1],labnum[k]);
count[k]=DifferNum(AllEachattr,labnum[k-1],labnum[k]);
}
prob[k]*=(double)(attrNum[k]+1)/(labnum[k]+count[k]);
}
}
maxscore=(prob[0]>prob[1]?prob[0]:prob[1])>prob[2]?(prob[0]>prob[1]?prob[0]:prob[1]):prob[2]; //鏈�澶у��
if(prob[0]==maxscore)
plabel=1;
if(prob[1]==maxscore)
plabel=2;
if(prob[2]==maxscore)
plabel=3;
if(label==plabel)
predT++;
printf("第%d个样本,实际样本为%d\n",i,label);
printf("%.8lf %.8lf%.8lf\n",prob[0],prob[1],prob[2]);
labnum[label-1]+=1; //鎭㈠鍚勭被鏍锋湰鏁�
}
printf("true rate is%lf\n",(double)predT/SAMPLE_CNT);
}
int main()
{
process();
getch();
return 1;
}