⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 result.cpp

📁 FCM算法的实现及测试
💻 CPP
字号:
#include "data.h"
#include "max.h"

double Result(int Lab, double**u,int k, int m, int ix)
{
	int i,j;

	cout<<"输出第"<<ix+1<<"次运行的聚类结果:"<<endl;
	ofstream outLabel("聚类结果.txt",ios::app);

	if(ix>0)
	{
		outLabel<<endl<<endl;
	}

	outLabel<<"输出第"<<ix+1<<"次运行的聚类结果:"<<endl;
	outLabel<<"************************************************"<<endl;

	//读入数据样本的实际类别标号文件
	int* label=new int[m];   //存储类别标号
	char*filename;           //实际类别的标号文件名

	if(Lab==1)
	{
		filename="Label\\label1.txt";
	}
	else if(Lab==2)
	{
		filename="Label\\label2.txt";
	}
	else if(Lab==3)
	{
		filename="Label\\label3.txt";
	}
	else if(Lab==4)
	{
		filename="Label\\label4.txt";
	}

	ifstream ReadLabel(filename);
	
	if(ReadLabel.fail())
	{
		cerr<<"无法找到标记文件!"<<endl;
	}
	
	for(i=0;i<m;i++)
	{
		ReadLabel>>label[i];
	}
	
	ReadLabel.close();
	
	cout<<endl;

	//聚类结果: 各个样本的类别标号序列
	int *c=new int[m];
	double* col=new double[k];
	for(j=0;j<m;j++)
	{
		for(i=0;i<k;i++)
		{
			col[i]=u[i][j];
		}
		double m=col[0];
		unsigned int cx=0;

		for(i=0;i<k;i++)
		{
			if(col[i]>m)
			{
				m=col[i];
				cx=i;
			}
		}
		c[j]=cx;

	} 
	//结果中类别标号计算完毕
	delete[] col;

	//确定各个别中样本的个数
	int* NK=new int[k];
	for(j=0;j<k;j++)	
	{
		NK[j]=0;
		int nk=0;	
		for(int t=0;t<m;t++)	
		{		
			if(label[t]==j)		
			{		
				nk+=1;	
			}	
		}		
		NK[j]=nk;
	}
	

	//统计各类别中的样本序号以及标记正确样本数
	int* right=new int[k];

	for(i=0;i<k;i++)                    //1-3
	{
		int ni=NK[i];                   //类别i中的样本数
		int* ci=new int[ni];            //类别i中的样本序号
		int* nr=new int[k];             //结果类别i中各类样本数目

		int t=0;
		//统计该类别i中的样本序号ci[]
		for(int j=0;j<m;j++)             
		{		
			if(label[j]==i)	
			{	
				ci[t]=j;
				t++;
			}	
		}

		cout<<"第"<<i<<"类样本:\n";
		outLabel<<"第"<<i<<"类样本:\n";
		for(t=0;t<ni;t++)		
		{
			int nt=ci[t];
			cout<<c[nt]<<" ";
			outLabel<<c[nt]<<" ";
		}
		cout<<endl;
		outLabel<<endl;
		

		//统计聚类结果cx中,类别i中的正确样本
		for(int i1=0;i1<k;i1++)		
		{
			int r=0;
			for(t=0;t<ni;t++)
			{
				int nt=ci[t];
				if(c[nt]==i1)
				{
					r++;
				}
				nr[i1]=r;
			}
			
		}
		//找出nr中最大值
		int sx=0;
		//显示输出
		right[i]=max(nr,k,sx);
		cout<<"正确样本数: right["<<i<<"]="<<right[i]<<endl;
		cout<<"错误样本数: "<<ni-right[i]<<endl;
		cout<<"聚类类别号: "<<"sx="<<sx<<endl;
		cout<<endl;

		//文件保存
		outLabel<<"正确样本数: right["<<i<<"]="<<right[i]<<endl;
		outLabel<<"错误样本数: "<<ni-right[i]<<endl;
		outLabel<<"聚类类别号: "<<"sx="<<sx<<endl;
		outLabel<<endl;

		delete[]ci;
		delete[]nr;
	
		//第i类实际样本序号统计完毕,
		//system("pause");
		
	}

	delete[] NK;
	delete[] c;



	//计算聚类正确率
	int nx=0;
	for(i=0;i<k;i++) 
	{
		nx+=right[i];
	}
		
	delete[] right;

	double r=(double)nx/m;	
	cout.precision(2);
	cout<<"聚类正确率: "<<100*r<<"%"<<endl;
	outLabel<<"聚类正确率: "<<100*r<<"%"<<endl;


	return r;



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -