k近邻算法(knn)的c语言实现

时间:2023-03-08 20:38:57
k近邻算法(knn)的c语言实现

  最近在看knn算法,顺便敲敲代码。

 

  knn属于数据挖掘的分类算法。基本思想是在距离空间里,如果一个样本的最接近的k个邻居里,绝大多数属于某个类别,则该样本也属于这个类别。俗话叫,“随大流”。

  简单来说,KNN可以看成:有那么一堆你已经知道分类的数据,然后当一个新的数据进入的时候,就开始跟训练里的每个点求距离,然后挑出离这个数据最近的K个点,看看这K个点属于什么类型,然后用少数服从多数的原则,给新数据归类。

  该算法的示意图,简单明了:

  k近邻算法(knn)的c语言实现

    下面的算法步骤取自于百度文库(文库是一个好东西),代码是参照这个思路实现的:

   k近邻算法(knn)的c语言实现

  code:

 #include<stdio.h>
#include<math.h>
#include<stdlib.h> #define K 3 //近邻数k
typedef float type; //动态创建二维数组
type **createarray(int n,int m)
{
int i;
type **array;
array=(type **)malloc(n*sizeof(type *));
array[]=(type *)malloc(n*m*sizeof(type));
for(i=;i<n;i++) array[i]=array[i-]+m;
return array;
}
//读取数据,要求首行格式为 N=数据量,D=维数
void loaddata(int *n,int *d,type ***array,type ***karray)
{
int i,j;
FILE *fp;
if((fp=fopen("data.txt","r"))==NULL) fprintf(stderr,"can not open data.txt!\n");
if(fscanf(fp,"N=%d,D=%d",n,d)!=) fprintf(stderr,"reading error!\n");
*array=createarray(*n,*d);
*karray=createarray(,K); for(i=;i<*n;i++)
for(j=;j<*d;j++)
fscanf(fp,"%f",&(*array)[i][j]); //读取数据 for(i=;i<;i++)
for(j=;j<K;j++)
(*karray)[i][j]=9999.0; //默认的最大值
if(fclose(fp)) fprintf(stderr,"can not close data.txt");
}
//计算欧氏距离
type computedistance(int n,type *avector,type *bvector)
{
int i;
type dist=0.0;
for(i=;i<n;i++)
dist+=pow(avector[i]-bvector[i],);
return sqrt(dist);
}
//冒泡排序
void bublesort(int n,type **a,int choice)
{
int i,j;
type k;
for(j=;j<n;j++)
for(i=;i<n-j-;i++){
if(==choice){
if(a[][i]>a[][i+]){
k=a[][i];
a[][i]=a[][i+];
a[][i+]=k;
k=a[][i];
a[][i]=a[][i+];
a[][i+]=k;
}
}
else if(==choice){
if(a[][i]>a[][i+]){
k=a[][i];
a[][i]=a[][i+];
a[][i+]=k;
k=a[][i];
a[][i]=a[][i+];
a[][i+]=k;
}
}
}
}
//统计有序表中的元素个数
type orderedlist(int n,type *list)
{
int i,count=,maxcount=;
type value;
for(i=;i<(n-);i++) {
if(list[i]!=list[i+]) {
//printf("count of %d is value %d\n",list[i],count);
if(count>maxcount){
maxcount=count;
value=list[i];
count=;
}
}
else
count++;
}
if(count>maxcount){
maxcount=count;
value=list[n-];
}
//printf("value %f has a Maxcount:%d\n",value,maxcount);
return value;
} int main()
{
int i,j,k;
int D,N; //维度,数据量
type **array=NULL; //数据数组
type **karray=NULL; // K近邻点的距离及其标签
type *testdata; //测试数据
type dist,maxdist; loaddata(&N,&D,&array,&karray);
testdata=(type *)malloc((D-)*sizeof(type));
printf("input test data containing %d numbers:\n",D-);
for(i=;i<(D-);i++) scanf("%f",&testdata[i]); while(){
for(i=;i<K;i++){
if(K>N) exit(-);
karray[][i]=computedistance(D-,testdata,array[i]);
karray[][i]=array[i][D-];
//printf("first karray:%6.2f %6.0f\n",karray[0][i],karray[1][i]);
} bublesort(K,karray,);
//for(i=0;i<K;i++) printf("after bublesort in first karray:%6.2f %6.0f\n",karray[0][i],karray[1][i]);
maxdist=karray[][K-]; //初始化k近邻数组的距离最大值 for(i=K;i<N;i++){
dist=computedistance(D-,testdata,array[i]);
if(dist<maxdist)
for(j=;j<K;j++){
if(dist<karray[][j]){
for(k=K-;k>j;k--){ //j后元素复制到后一位,为插入做准备
karray[][k]=karray[][k-];
karray[][k]=karray[][k-];
}
karray[][j]=dist; //插入到j位置
karray[][j]=array[i][D-];
//printf("i:%d karray:%6.2f %6.0f\n",i,karray[0][j],karray[1][j]);
break; //不比较karray后续元素
}
}
maxdist=karray[][K-];
//printf("i:%d maxdist:%6.2f\n",i,maxdist);
}
//for(i=0;i<K;i++) printf("karray:%6.2f %6.0f\n",karray[0][i],karray[1][i]);
bublesort(K,karray,);
//for(i=0;i<K;i++) printf("after bublesort in karray:%6.2f %6.0f\n",karray[0][i],karray[1][i]);
printf("\nThe data has a tag: %.0f\n\n",orderedlist(K,karray[])); printf("input test data containing %d numbers:\n",D-);
for(i=;i<(D-);i++) scanf("%f",&testdata[i]);
}
return ;
}

  实验:

  训练数据data.txt:

  N=6,D=9
  1.0 1.1 1.2 2.1 0.3 2.3 1.4 0.5 1
  1.7 1.2 1.4 2.0 0.2 2.5 1.2 0.8 1
  1.2 1.8 1.6 2.5 0.1 2.2 1.8 0.2 1
  1.9 2.1 6.2 1.1 0.9 3.3 2.4 5.5 0
  1.0 0.8 1.6 2.1 0.2 2.3 1.6 0.5 1
  1.6 2.1 5.2 1.1 0.8 3.6 2.4 4.5 0

  预测数据:

  1.0 1.1 1.2 2.1 0.3 2.3 1.4 0.5

  1.7 1.2 1.4 2.0 0.2 2.5 1.2 0.8

  1.2 1.8 1.6 2.5 0.1 2.2 1.8 0.2

  1.9 2.1 6.2 1.1 0.9 3.3 2.4 5.5

  1.0 0.8 1.6 2.1 0.2 2.3 1.6 0.5

  1.6 2.1 5.2 1.1 0.8 3.6 2.4 4.5

  

  程序测试的结果:

  1.0 1.1 1.2 2.1 0.3 2.3 1.4 0.5 类别为: 1

  1.7 1.2 1.4 2.0 0.2 2.5 1.2 0.8 类别为: 1

  1.2 1.8 1.6 2.5 0.1 2.2 1.8 0.2 类别为: 1

  1.9 2.1 6.2 1.1 0.9 3.3 2.4 5.5 类别为: 0

  1.0 0.8 1.6 2.1 0.2 2.3 1.6 0.5 类别为: 1

  1.6 2.1 5.2 1.1 0.8 3.6 2.4 4.5 类别为: 0

  实验截图:

  k近邻算法(knn)的c语言实现