运用KNN实现鸢尾花分类

先上代码

#include
#include
#include
#include
#include
#define bool int
#define true 1
#define false 0
struct Iris {				//存储数据
	double sepalLength;		//花萼长度
	double sepalWidth;		//花萼宽度
	double petaLength;		//花瓣长度
	double petaWidth;		//花瓣宽度
	char name[20];			//种类名字
	int ID;					//分类编号
};
struct Distance {			//存储测试集待分类样本与训练集样本的欧式距离
	double diatance;		//欧式距离
	int number;				//训练集样本编号
};
int d[3];					//存储K中三类中各类数目
struct Distance dis[150];
struct Iris iris[150];
const char* irisname[3] = { "Iris-setosa","Iris-versicolor","Iris-virginica" };//用于比较名字
int K;						//邻近样本个数
int DataNum;				//样本总数
int Datatest;				//测试样本个数
int Datatrain;				//训练样本个数
int n;						//训练集数目会变化
bool Inputs() {				
	srand(time(NULL));
	char fname[256];
	printf("请输入存放数据的文件:\n");
	scanf("%s", fname);
	printf("请输入样本数目:\n");
	scanf("%d", &DataNum);
	printf("请输入K的大小:\n");
	scanf("%d", &K);
	FILE* fp = fopen(fname, "rb");
	if (fp == NULL) {
		printf("不能打开文件!\n");
		return false;
	}
	Datatrain = 3 * DataNum / 4;			//选取3/4为训练集
	Datatest = DataNum - Datatrain;			//选取1/4为测试集
	for (int i = 0; i < DataNum; ++i) {		//读取数据
		fscanf(fp, "%lf,%lf,%lf,%lf,%s", &iris[i].sepalLength, &iris[i].sepalWidth, &iris[i].petaLength, &iris[i].petaWidth,iris[i].name);
		for (int j = 0; j < 3; ++j) {
			if (strcmp(iris[i].name,irisname[j])==0) {//种类维度数据化
				iris[i].ID = j+1;
				break;
			}							
		}
	}
	/*struct Iris temp;
	for (int i = 0; i < DataNum; ++i) {		//打乱数据,需要就用
		int l = rand() % DataNum;
		temp = iris[i];
		iris[i] = iris[l];
		iris[l] = temp;
	}*/
	/*for (int i = 0; i < DataNum; ++i) {		//测试读取是否成功,需要就用
		printf("%lf %lf %lf %lf %d\n", iris[i].sepalLength, iris[i].sepalWidth, iris[i].petaLength, iris[i].petaWidth, iris[i].ID);
	}*/
	fclose(fp);
	return true;
}
bool compare(struct Distance a, struct Distance b) {	//比较函数
	return a.diatance < b.diatance;
}
int QuickPass(struct Distance* a, int low, int high) {//快速排序划分函数
	double x = a[low].diatance;
	while (low < high) {
		while (low < high && x <= a[high].diatance) {
			--high;
		}
		if (low == high) {
			break;
		}
		a[low++] = a[high];
		while (low < high && x >= a[low].diatance) {
			++low;
		}
		if (low == high) {
			break;
		}
		a[high--] = a[low];
	}
	a[low].diatance = x;
	return low;
}
void QuickSort(struct Distance* a, int low, int high) {//快速排序函数
	if (low >= high) {
		return;
	}
	int p = QuickPass(a, low, high);	
	QuickSort(a, low, p - 1);
	QuickSort(a, p + 1, high);			
}
void classification() {					//将测试集分类
	int i, j;
	n = Datatrain;
	for (i = n; i < DataNum; ++i) {
		for (j = 0; j < n; ++j) {
			dis[j].diatance = sqrt(pow(iris[j].petaLength - iris[i].petaLength, 2) + pow(iris[j].petaWidth - iris[i].petaWidth, 2) + pow(iris[j].sepalLength - iris[i].sepalLength, 2) + pow(iris[j].sepalWidth - iris[i].sepalWidth, 2));
			dis[j].number = j;
		}
		QuickSort(dis,0,n-1);
		memset(d, 0, sizeof(d));
		for (j = 0; j < K; ++j) {
			d[iris[dis[j].number].ID-1]++;
		}
		int max = d[0];
		int t = 0;
		for (j = 1; j < 3; ++j) {
			if (d[j] > max) {
				max = d[j];
				t = j;
			}
		}
		iris[i].ID = t + 1;
		n++;			//将测试集分类样本归入训练集
	}
}
void test() {					//计算准确度
	int i,j,l;
	int t = 0;
	double result;
	for (i = Datatrain; i < DataNum; ++i) {
		for (j = 0; j < 3; ++j) {
			if (strcmp(iris[i].name,irisname[j])==0) {
				l = j + 1;
				break;
			}
		}
		if (l == iris[i].ID) {
			t++;
		}
		iris[i].ID = l;
	}
	result = (double)t / Datatest;
	printf("\n预测准确度为:%lf\n", result);
}
int main() {			//主函数
	if (Inputs() == false) {
		return 0;
	}
	classification();
	test();
	return 0;
}

总结

在不打乱数据情况下,准确度普遍较低。且随着K值越来越大,准确度越来越低(如下图)。在打乱数据情况下,准确度更高(读者可自行测试)。
运用KNN实现鸢尾花分类_第1张图片

数据文件

文件名:data,样本数量:150.
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
5.0,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa
4.7,3.2,1.6,0.2,Iris-setosa
4.8,3.1,1.6,0.2,Iris-setosa
5.4,3.4,1.5,0.4,Iris-setosa
5.2,4.1,1.5,0.1,Iris-setosa
5.5,4.2,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.0,3.2,1.2,0.2,Iris-setosa
5.5,3.5,1.3,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
4.4,3.0,1.3,0.2,Iris-setosa
5.1,3.4,1.5,0.2,Iris-setosa
5.0,3.5,1.3,0.3,Iris-setosa
4.5,2.3,1.3,0.3,Iris-setosa
4.4,3.2,1.3,0.2,Iris-setosa
5.0,3.5,1.6,0.6,Iris-setosa
5.1,3.8,1.9,0.4,Iris-setosa
4.8,3.0,1.4,0.3,Iris-setosa
5.1,3.8,1.6,0.2,Iris-setosa
4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
5.0,3.3,1.4,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4.0,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
5.7,2.8,4.5,1.3,Iris-versicolor
6.3,3.3,4.7,1.6,Iris-versicolor
4.9,2.4,3.3,1.0,Iris-versicolor
6.6,2.9,4.6,1.3,Iris-versicolor
5.2,2.7,3.9,1.4,Iris-versicolor
5.0,2.0,3.5,1.0,Iris-versicolor
5.9,3.0,4.2,1.5,Iris-versicolor
6.0,2.2,4.0,1.0,Iris-versicolor
6.1,2.9,4.7,1.4,Iris-versicolor
5.6,2.9,3.6,1.3,Iris-versicolor
6.7,3.1,4.4,1.4,Iris-versicolor
5.6,3.0,4.5,1.5,Iris-versicolor
5.8,2.7,4.1,1.0,Iris-versicolor
6.2,2.2,4.5,1.5,Iris-versicolor
5.6,2.5,3.9,1.1,Iris-versicolor
5.9,3.2,4.8,1.8,Iris-versicolor
6.1,2.8,4.0,1.3,Iris-versicolor
6.3,2.5,4.9,1.5,Iris-versicolor
6.1,2.8,4.7,1.2,Iris-versicolor
6.4,2.9,4.3,1.3,Iris-versicolor
6.6,3.0,4.4,1.4,Iris-versicolor
6.8,2.8,4.8,1.4,Iris-versicolor
6.7,3.0,5.0,1.7,Iris-versicolor
6.0,2.9,4.5,1.5,Iris-versicolor
5.7,2.6,3.5,1.0,Iris-versicolor
5.5,2.4,3.8,1.1,Iris-versicolor
5.5,2.4,3.7,1.0,Iris-versicolor
5.8,2.7,3.9,1.2,Iris-versicolor
6.0,2.7,5.1,1.6,Iris-versicolor
5.4,3.0,4.5,1.5,Iris-versicolor
6.0,3.4,4.5,1.6,Iris-versicolor
6.7,3.1,4.7,1.5,Iris-versicolor
6.3,2.3,4.4,1.3,Iris-versicolor
5.6,3.0,4.1,1.3,Iris-versicolor
5.5,2.5,4.0,1.3,Iris-versicolor
5.5,2.6,4.4,1.2,Iris-versicolor
6.1,3.0,4.6,1.4,Iris-versicolor
5.8,2.6,4.0,1.2,Iris-versicolor
5.0,2.3,3.3,1.0,Iris-versicolor
5.6,2.7,4.2,1.3,Iris-versicolor
5.7,3.0,4.2,1.2,Iris-versicolor
5.7,2.9,4.2,1.3,Iris-versicolor
6.2,2.9,4.3,1.3,Iris-versicolor
5.1,2.5,3.0,1.1,Iris-versicolor
5.7,2.8,4.1,1.3,Iris-versicolor
6.3,3.3,6.0,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3.0,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
6.5,3.0,5.8,2.2,Iris-virginica
7.6,3.0,6.6,2.1,Iris-virginica
4.9,2.5,4.5,1.7,Iris-virginica
7.3,2.9,6.3,1.8,Iris-virginica
6.7,2.5,5.8,1.8,Iris-virginica
7.2,3.6,6.1,2.5,Iris-virginica
6.5,3.2,5.1,2.0,Iris-virginica
6.4,2.7,5.3,1.9,Iris-virginica
6.8,3.0,5.5,2.1,Iris-virginica
5.7,2.5,5.0,2.0,Iris-virginica
5.8,2.8,5.1,2.4,Iris-virginica
6.4,3.2,5.3,2.3,Iris-virginica
6.5,3.0,5.5,1.8,Iris-virginica
7.7,3.8,6.7,2.2,Iris-virginica
7.7,2.6,6.9,2.3,Iris-virginica
6.0,2.2,5.0,1.5,Iris-virginica
6.9,3.2,5.7,2.3,Iris-virginica
5.6,2.8,4.9,2.0,Iris-virginica
7.7,2.8,6.7,2.0,Iris-virginica
6.3,2.7,4.9,1.8,Iris-virginica
6.7,3.3,5.7,2.1,Iris-virginica
7.2,3.2,6.0,1.8,Iris-virginica
6.2,2.8,4.8,1.8,Iris-virginica
6.1,3.0,4.9,1.8,Iris-virginica
6.4,2.8,5.6,2.1,Iris-virginica
7.2,3.0,5.8,1.6,Iris-virginica
7.4,2.8,6.1,1.9,Iris-virginica
7.9,3.8,6.4,2.0,Iris-virginica
6.4,2.8,5.6,2.2,Iris-virginica
6.3,2.8,5.1,1.5,Iris-virginica
6.1,2.6,5.6,1.4,Iris-virginica
7.7,3.0,6.1,2.3,Iris-virginica
6.3,3.4,5.6,2.4,Iris-virginica
6.4,3.1,5.5,1.8,Iris-virginica
6.0,3.0,4.8,1.8,Iris-virginica
6.9,3.1,5.4,2.1,Iris-virginica
6.7,3.1,5.6,2.4,Iris-virginica
6.9,3.1,5.1,2.3,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
6.8,3.2,5.9,2.3,Iris-virginica
6.7,3.3,5.7,2.5,Iris-virginica
6.7,3.0,5.2,2.3,Iris-virginica
6.3,2.5,5.0,1.9,Iris-virginica
6.5,3.0,5.2,2.0,Iris-virginica
6.2,3.4,5.4,2.3,Iris-virginica
5.9,3.0,5.1,1.8,Iris-virginica

你可能感兴趣的:(机器学习,算法,c语言,大数据,visual,studio)