c语言 实现KNN算法

c语言实现KNN算法,
可以实现任意维度,任意数据量,任意分类数据的KNN算法。
K值的修改在KNN.h文件

main.c
#include <stdio.h>
#include <stdlib.h>
#include "KNN.h"
#include "array.h"

int main(void)
{
	
	//初始化 ,读取数据 
	//count为分类数
	//N为数据个数,M为数据维度,dist为数据间距
	//array为导入的原始数据,testdata为测试数据,karray为测试数据到各个原始数据之间的距离  
	int i,j,count=1;
	int N=0,M=0; 
	float temp;
	float dist;
	float **array = NULL,**karray = NULL,*testdata; 
	KNN_load_data(&N,&M,&array,&karray);
	printf("读取数据个数:%d,数据维度:%d\n",N,M-1);
	printf("读入的数据为:\n");
	print_2Darray(*array,N,M);
	
	//判断所给数据共有几个类
	float a[N-1];
	for(i=0;i<N-1;i++)
	{
		a[i] = array[i][M-1];
	}
	bubble_sort(a,N);
	for(i=0;i<N-1;i++)
	{
		if(a[i]!=a[i+1])
		{
			count++;
		}
	}
	printf("所给数据的分类数为:%d\n",count);

	//写入待测数据点
	testdata = (float *)malloc((M-1) * sizeof(float));
	printf("写入一个%d维的待测数据点:",M-1);
	for(i=0;i<M-1;i++)
	{
		scanf("%f",&testdata[i]);
	}
	
	//计算距离
	for(i=0;i<N;i++)
	{
		dist = distance(M-1,testdata,array[i]);
		karray[i][0] = dist;
		karray[i][1] = array[i][M-1];
	}
	printf("待测点到各个点的距离:\n");
	print_2Darray(*karray,N,2);
	
	//冒泡排序
	for(i=0;i<N-1;i++)
	{
		for(j=0;j<N-i-1;j++)
		{
			if(karray[j][0]>karray[j+1][0])
			{
				temp = karray[j][0];
				karray[j][0] = karray[j+1][0];
				karray[j+1][0] = temp;
				
				temp = karray[j][1];
				karray[j][1] = karray[j+1][1];
				karray[j+1][1] = temp; 
			}
		}
	}
	printf("排序后的待测点到各个点的距离:\n");
	print_2Darray(*karray,N,2);
	

	//找出前K个距离最近的进行分类
	//result储存各个分类出现的频率 
	float *result;
	int b = count;
	result = (float *)malloc(count*sizeof(float));
	for(i=0;i<K;i++)
	{
		for(b = count;b>0;b--)
		{
			if((int)(karray[i][1]) == b-1)
			{
				result[b-1] ++;
			}
		}
	}
	printf("K取%d\n",K);
	printf("最终各分类出现的次数:");
	print_1Darray(result,count);
	
	//最终结果输出 
	int index;
	index = max_index(result,count);
	printf("\n最终判断改测试数据属于%d类",index);
	return 0;
}




array.h
void selection_sort(float *array , int len);
void bubble_sort(float *array , int len);
void print_1Darray(float *array,int len);
void print_2Darray(float *array,int N,int M);
void swap(float *a,float *b);
int max_index(float *a,int len);
float distance(int dimension,float *av,float *bv);
array.c
#include<stdio.h>
#include"array.h"
#include<math.h>
void print_1Darray(float *array,int len)
{
	//array是需要打印的数组,len为该数组的长度 
	int i;
	printf("[");
	for(i=0;i<len;i++)
	{
		printf("%.2f",array[i]);
		printf("%c",i<len-1?' ':']');
	}
	printf("\n");
}

void print_2Darray(float *array,int N , int M)
{
	int i=0,j=0;
	for(i=0;i<N;i++)
	{
		for(j=0;j<M;j++)
		{
			printf("%.2f%c",array[i*M+j],j==M-1?'\n':' ');
		}
	}
}

void selection_sort(float *array,int len)
{
	int i,j;
	for(i=0;i<len-1;i++)
	{
		for(j=i+1;j<len;j++)
		{
			if(array[j]>array[i])
			{
				swap(&array[j],&array[i]);
			}
		}
	}
}

void bubble_sort(float *array,int len)
{
	int i,j;
	for(i=0;i<len-1;i++)
	{
		for(j=0;j<len-1-i;j++)
		{
			if(array[j+1]>array[j]) swap(&array[j+1],&array[j]);
		}
	}
}

void swap(float *a,float *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
	
}

int max_index(float *a,int len)
{
    int max = a[0];
    int index=0,i;
    for(i=1;i<len;i++)
	{
        if(max<a[i])
		{
            max = a[i];
            index = i;
        }
    }
	return index;
}


float distance(int dim,float *av, float *bv) //dim代表两个向量的维度 
{
	int i,dist=0;
	for(i=0;i<dim;i++)
	{
		dist += pow(av[i]-bv[i],2);
	}
	return sqrt(dist);
}
KNN.h
#define K 3 
void KNN_load_data(int *data_num,int *dimension,float ***array,float ***karray);
float **dynamic_array(int n,int m);

KNN.c
#include<stdio.h>
#include<stdlib.h>
#include"KNN.h"
#include"array.h"
//创建动态二维数组 
float **dynamic_array(int n,int m)//n是数据个数,m是维数 
{
	int i;
	float **array;
	array=(float **)malloc(n*sizeof(float *));    
    array[0]=(float *)malloc(n*m*sizeof(float));
	for(i=1;i<n;i++)
	{
		array[i] = array[i-1] + m;
	}
	return array;
}


void KNN_load_data(int *data_num,int *dimension,float ***array,float ***karray)
{
	FILE *fp;
	int i=0,j=0;
	if((fp = fopen("data.txt","r")) == NULL) printf("文件打开失败\n");
	if((fscanf(fp,"N=%d,D=%d",data_num,dimension)) != 2) printf("文件打开失败\n");

	*array = dynamic_array(*data_num,*dimension);
	*karray = dynamic_array(*data_num,2);
	
	for(i=0;i<*data_num;i++)
	{
		for(j=0;j<*dimension;j++)
		{
			fscanf(fp,"%f",&(*array)[i][j]);
		}
	}
	for(i=0;i<*data_num;i++)
	{
		for(j=0;j<2;j++)
		{
			(*karray)[i][j]=999.0;
		}
	}

}
data.txt
N=6,D=9
N=7,D=9
2.0 2.1 5.2 4.1 0.5 5.3 2.4 2.1 2 
1.1 3.1 2.3 3.0 3.2 4.4 3.3 1.8 1 
1.7 3.2 3.2 2.1 4.4 3.5 4.3 1.2 0 
1.1 1.5 5.2 5.2 3.5 1.2 5.3 3.5 1 
2.0 2.1 3.2 4.7 2.4 2.4 4.4 4.5 0 
2.6 3.2 3.1 5.2 1.2 7.4 1.2 3.4 2 
2.1 1.1 2.2 3.7 1.4 3.4 3.4 3.5 0 

程序的最终结果
在这里插入图片描述

  • 4
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是基于C语言实现KNN算法鸢尾花分类代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define K 3 // 定义K值 // 定义鸢尾花的数据结构 typedef struct { double sepallength; // 花萼长度 double sepalwidth; // 花萼宽度 double petallength; // 花瓣长度 double petalwidth; // 花瓣宽度 char class[20]; // 类别 } Iris; // 读取数据集 void read_data(char* filename, Iris* dataset, int* n) { FILE* fp = fopen(filename, "r"); if (fp == NULL) { printf("Open file %s failed!\n", filename); exit(1); } char buf[1024]; int i = 0; while (fgets(buf, 1024, fp)) { sscanf(buf, "%lf,%lf,%lf,%lf,%s", &dataset[i].sepallength, &dataset[i].sepalwidth, \ &dataset[i].petallength, &dataset[i].petalwidth, dataset[i].class); i++; } *n = i; fclose(fp); } // 计算两点之间的距离 double distance(Iris* p, Iris* q) { return sqrt(pow(p->sepallength - q->sepallength, 2) + pow(p->sepalwidth - q->sepalwidth, 2) + \ pow(p->petallength - q->petallength, 2) + pow(p->petalwidth - q->petalwidth, 2)); } // 查找K个最近邻居 void find_k_neighbors(Iris* dataset, int n, Iris* test, Iris** neighbors) { double dist; double max_dist = 0.0; int max_index = 0; for (int i = 0; i < K; i++) { neighbors[i] = &dataset[i]; dist = distance(neighbors[i], test); if (dist > max_dist) { max_dist = dist; max_index = i; } } for (int i = K; i < n; i++) { dist = distance(&dataset[i], test); if (dist < max_dist) { neighbors[max_index] = &dataset[i]; max_dist = dist; for (int j = 0; j < K; j++) { if (distance(neighbors[j], test) > max_dist) { max_dist = distance(neighbors[j], test); max_index = j; } } } } } // 计算类别出现次数 char* get_class(Iris** neighbors) { int class_count[3] = {0, 0, 0}; for (int i = 0; i < K; i++) { if (strcmp(neighbors[i]->class, "Iris-setosa") == 0) { class_count[0]++; } else if (strcmp(neighbors[i]->class, "Iris-versicolor") == 0) { class_count[1]++; } else { class_count[2]++; } } int max_count = 0; int max_index = 0; for (int i = 0; i < 3; i++) { if (class_count[i] > max_count) { max_count = class_count[i]; max_index = i; } } if (max_index == 0) { return "Iris-setosa"; } else if (max_index == 1) { return "Iris-versicolor"; } else { return "Iris-virginica"; } } int main() { Iris dataset[150]; // 数据集 Iris test_data; // 测试数据 Iris* neighbors[K]; // 最近邻居 int n; // 数据集的大小 read_data("iris.data", dataset, &n); // 读取数据集 // 手动输入测试数据 printf("Please input the test data (sepal length, sepal width, petal length, petal width):\n"); scanf("%lf%lf%lf%lf", &test_data.sepallength, &test_data.sepalwidth, &test_data.petallength, &test_data.petalwidth); find_k_neighbors(dataset, n, &test_data, neighbors); // 查找最近邻居 printf("The class of the test data is %s.\n", get_class(neighbors)); // 输出分类结果 return 0; } ``` 注意:本代码中的数据集文件"iris.data"需要自行下载。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值