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

📄 geteigen.cpp

📁 应用于模式分类中
💻 CPP
字号:
#include <iostream.h>
#include <malloc.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <fstream.h>
#include "backprop.h"
#include <iomanip.h>
//////////////////////////////////////////////////////////////////////////
//  取每个数字每行到左边的距离一个特征值
//  这样就构成了一个28维的特征向量
//////////////////////////////////////////////////////////////////////////
#define  DIMENSION 28
#define  TRAIN_COUNT 60000
#define  TEST_COUNT 10000
#define FEATURENUM 49
#define STEP 4
void Aigen_Left_Distance(int input[][DIMENSION],double *output)
{
	int i,j,k;
	k=0;
	for(i=0;i<DIMENSION;i++)
		output[i]=0;
	for(i=0;i<DIMENSION;i++)
	{	
		for (j=0;j<DIMENSION;j++)
		{
			if(input[i][j]!=0)
			{
				output[k++]=j;
				//	cout<<j<<endl;
				break;
			}
			if(j==DIMENSION-1)
			{
				output[k++]=0;
				//		cout<<j<<endl;
				
			}
		}
	}
	
    k=0;
	for(i=0;i<DIMENSION;i++)
	{	
		for (j=DIMENSION-1;j>=0;j--)
		{
			if(input[i][j]!=0)
			{
				output[k]=j-output[k];
				k++;
				//	cout<<j<<endl;
				break;
			}
			if(j==0)
			{
				output[k++]=0;
				//		cout<<j<<endl;
				
			}
		}
	}
	////归一化
	double num=0;
	for(i=0;i<DIMENSION;i++)
	{
		num+=output[i]*output[i];
		
	}
	num=sqrt(num);
	for(i=0;i<DIMENSION;i++)
	{
		output[i]=output[i]/num;
	}
	
	
}
//////////////////////////////////////////////////////////////////////////
//把矩阵分成7×7块,然后统计没块中1的个数
//此时size=7
//////////////////////////////////////////////////////////////////////////
void Aigen_Area(int input[][DIMENSION],double output[FEATURENUM])
{
	int i = 0, j = 0, k = 0, l = 0;
	int cur = 0;
	
	for ( i = 0; i < FEATURENUM; i++)
		output[i] = 0;
	
	for ( k = 1; k <= DIMENSION/STEP; k++)
	{
		for ( l = 1; l <= DIMENSION/STEP; l++)
		{
			for ( i = (k-1)*STEP; i < k*STEP; i++)
			{
				for ( j = (l-1)*STEP; j < l*STEP; j++)
					output[cur] += input[i][j];
			}
			cur++;
		}
	}
	
}
class temp
{
public:
	double feature[FEATURENUM];
	int number;
};
double mo(double *a)
{
	double s=0.0;
	for(int i=0;i<FEATURENUM;i++)
	{
		s+=a[i]*a[i];
	}
	return sqrt(s);
}
void main()
{
	
    int i,j,k;
    fstream read_train_num;
	read_train_num.open("train-numbers.txt",ios::in);
    fstream read_train_image;
	read_train_image.open("train-images.txt",ios::in);
	temp *train_template;//[TRAIN_COUNT];
	train_template=new temp[TRAIN_COUNT];
	int input[DIMENSION][DIMENSION];
	for(i=0;i<TRAIN_COUNT;i++)
	{
		for(j=0;j<DIMENSION;j++)
			for(k=0;k<DIMENSION;k++)
				read_train_image>>input[j][k];		
			read_train_num>>train_template[i].number;	
			Aigen_Area(input,train_template[i].feature);
	
	}
    
	
	//////////////////////////////////////////////////////////////////////////
	//读入数据完毕
	//////////////////////////////////////////////////////////////////////////
	cout<<"complete\n"<<endl;
	
    fstream testimage;
	testimage.open("test-images.txt",ios::in);
	fstream testlabel;
	testlabel.open("test-numbers.txt",ios::in);
	
	fstream write;
	write.open("restult3.txt",ios::out);
	
	temp *test_template;
	test_template=new temp[TEST_COUNT];
	int counterror=0;
	for(i=0;i<TEST_COUNT;i++)
	{
		for(j=0;j<DIMENSION;j++)
			for(k=0;k<DIMENSION;k++)
				testimage>>input[j][k];					
			testlabel>>test_template[i].number;
			
			Aigen_Area(input,test_template[i].feature);
			//////////////////////////////////////////////////////////////////////////
			///计算分类
			int classf=-1;
			double max=-2.0;
			for(j=0;j<TRAIN_COUNT;j++)
			{
				
				double res=0.0;
				for(k=0;k<FEATURENUM;k++)
				{
				//	res+=(test_template[i].feature[k]-train_template[i].feature[k])*(test_template[i].feature[k]-train_template[i].feature[k]);
				  res+=(test_template[i].feature[k]*train_template[j].feature[k]);
				}
				res/=mo(test_template[i].feature)*mo(train_template[j].feature);
			//	cout<<res<<endl;
				if(res>max)
				{
					max=res;
					classf=train_template[j].number;
				}
			}
			write<<test_template[i].number<<"   "<<classf<<endl;
			if(test_template[i].number!=classf)
			{
				
				counterror++;
			}
			
	}
	write<<double(double(counterror)/double(TEST_COUNT))<<endl;	
	delete train_template;
	delete test_template;	
}

⌨️ 快捷键说明

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