View on GitHub

ML

机器学习课程作业

实验代码(github)

一、实验内容

二、理论准备

三、实验环境

四、实验过程

然后在main中,使用一个循环来进行测试模型的正确率:

int test_number = 100;
	int correct = 0;
	for (int counter0 = 0; counter0 < test_number;counter0++ )//测试循环
	{
		//统计每个分类器的输出
		int count[10] = { 0 };
		for (int counter1 = 0; counter1 < 5; counter1++)
		{
			count[(int)(KNN(train_images, train_labels, counter1 * 200, 200, 20, test_images[counter0]))]++;//每200个训练数据作为一个分类器的训练数
		}

		//进行投票
		unsigned char index = 0, max = 0;
		for (int counter2 = 0; counter2 < 10; counter2++)
		{
			if (count[counter2] > max)
			{
				max = count[counter2];
				index = counter2;
			}
		}
		//判断分类是否正确
		if (index == test_labels[counter0])
			correct++;
	}
	cout << "正确率: " << (double)correct / (double)test_number<<endl;

五、实验结果

正确率0.7 在这里插入图片描述

六、实验总结