赞
踩
计算欧氏距离代码:
float calDistance(Digit digit1, Digit digit2)
{
int i, squareSum = 0.0;
for (i = 0; i<1024; i++)
{
squareSum += pow(digit1.pixel[i] - digit2.pixel[i], 2.0);
}
return sqrtf(squareSum);
int prediction(int K, Digit in, Digit *train, int nt)/*利用训练数据预测一个数据digit*/
{
int i, it;
Distance distance[1133];
for (it = 0; it<nt; it++)
{
distance[it].distance = calDistance(in, train[it]);
distance[it].label = train[it].label;
}
int predict = 0;
int b0[10] = { 0 };
selectSort(distance, nt);
for (i = 0; i<K; i++)
{
switch (distance[i].label)
{
case 0:
b0[0]++;
break;
case 1:
b0[1]++;
break;
case 2:
b0[2]++;
break;
case 3:
b0[3]++;
break;
case 4:
b0[4]++;
break;
case 5:
b0[5]++;
break;
case 6:
b0[6]++;
break;
case 7:
b0[7]++;
break;
case 8:
b0[8]++;
break;
case 9:
b0[9]++;
break;
default:
break;
}
}
int max = 0;
for (int m = 0; m < 10; m++) {
if (b0[m] >= max) {
max = b0[m];
predict = m;
}
}
return predict;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。