View on GitHub

ML

机器学习课程作业

一、理论知识

二、实验环境

二、实验过程

  1. 主函数PETest.cpp
	PE pe;
	pe.setSampleFile("a.txt");
	pe.setClassSize(3);
	pe.setSampleSize(10);
	pe.setVectorLen(1);

	pe.estimate();
	cout<<"ch3_1_a:\n";
	for(int counter = 1; counter <= 3; counter++)
	{
		cout<<"clo "<<counter<<" means :"<<pe.getMean(counter).matrixToFloat()
		<<" variance :"<<pe.getSigma(counter).matrixToFloat()<<endl;
	}


①. 上面部分处理的是a小题的处理过程,新建对象,设置样本数据所在的文件pe.setSampleFile("a.txt");,设置有几组参数要估计pe.setClassSize(3);, 设置每组有几个样本 pe.setSampleSize(10);,设置每个样本维度pe.setVectorLen(1);上述这些参数设置在每次计算前必须设置,除非他和最近一次设置的是一样的
②. 进行估计pe.estimate();, 获取估计结果pe.getMean(counter).matrixToFloat();getMean()表示获取$\mu$的估计值,counter表示获取哪一组的估计值。matrixToFloat()将矩阵转化为浮点数,由于一维的样本,最终他的估计值也是一维的,但是它以矩阵的形式存在,所以我们可以将他转换成float形式。当然也可以不转换直接使用printMatrix()将它打印出来。
③. b小题和c小题处理办法是一样的,只是样本维度不同,获取到估计结果后不能转化为float,而要使用printMatrix()打印出来。

pe.setSampleFile("b.txt");
	pe.setClassSize(3);
	pe.setSampleSize(10);
	pe.setVectorLen(2);

	pe.estimate();
	cout<<"\nch3_1_b:\n";
	for(int counter = 1; counter <= 3; counter++)
	{
		cout<<"pair "<<counter<<"\nmeans :";
		pe.getMean(counter).printMatrix();
		cout<<"variance :";
		pe.getSigma(counter).printMatrix();
	}

	pe.setSampleFile("c.txt");
	pe.setClassSize(1);
	pe.setSampleSize(10);
	pe.setVectorLen(3);

	pe.estimate();
	cout<<"\nch3_1_c:\n";
	cout<<"means :";
	pe.getMean(1).printMatrix();
	cout<<"variance :";
	pe.getSigma(1).printMatrix();


④. d小题

三、实验结果

a小题结果

在这里插入图片描述

b小题结果

在这里插入图片描述

c小题结果

在这里插入图片描述

d小题结果

在这里插入图片描述

e小题

四、遇到的困难