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

📄 traindata.cpp

📁 该程序包实现了模式识别中的两个特征提取算法
💻 CPP
字号:
#include "stdlib.h"
#include <iostream>
#include <string.h>
#include <fstream>
#include <math.h>
using namespace std;
#include "global.h"

CSampleData::CSampleData()
{
	this->numClass = 0;
	this->numFeature = 0;
	this->numSample = 0;
	this->nameClass = NULL;
	this->xdata = NULL;
	this->ydata = NULL;
	this->maxValue = NULL;
	this->minValue = NULL;
}

CSampleData::~CSampleData()
{
	if (this->nameClass != NULL)
		delete[] this->nameClass;
	if (this->xdata != NULL)
		delete[] this->xdata;
	if (this->ydata != NULL)
		delete[] this->ydata;
	if (this->maxValue != NULL)
		delete[] this->maxValue;
	if (this->minValue != NULL)
		delete[] this->minValue;
}

bool CSampleData::readFile(char* fileName)
{
	bool ret = true;
	ifstream ifs(fileName);
	int bufSize = 1024;
	char* buf=new char[bufSize];
	char* temp=new char[bufSize];
	int n=0;
	int i, j, k, index;
	//获取特征数
	if (ifs.good()) {
		ifs.getline(buf, bufSize);
		n=0;
		while (*(buf+n)!=0 && n<bufSize)
		{
			if (*(buf+n)==',')
				this->numFeature ++;
			n++;
		}
	}
	else
		ret = false;
	ifs.seekg(0, ios::beg);
	//获取总行数
	n=0;
	while (ifs.good()) {
		ifs.getline(buf, bufSize);
		n++;
	}
	this->numSample = n;
	ifs.clear();
	ifs.seekg(0, ios::beg);
	//读入采样数据
	this->xdata = new DOUBLE[(this->numSample)*(this->numFeature)];
	this->ydata = new int[this->numSample];
	this->nameClass = new char*[this->numSample];//现在不知道类别数,先用采样数来代替,浪费一些内存

	int cnum;
	i=0;//用来记录采样数
	while (ifs.good()) {
		ifs.getline(buf, bufSize);
		n=0;
		while (*(buf+n)!=',' && n<bufSize)
		{
			*(temp+n)=*(buf+n);
			n++;
		}
		*(temp+n)=0;
		cnum=this->searchClassName(temp);
		if (cnum==-1)//新类别
		{
			this->nameClass[this->numClass] = new char[strlen(temp)];
			strcpy(this->nameClass[this->numClass], temp);
			this->ydata[i]=this->numClass;
			this->numClass ++;
		}
		else
			this->ydata[i]=cnum;
		for (j=0; j<this->numFeature; j++)
		{
			n++;
			int n2=n;
			while (*(buf+n)!=',' && *(buf+n)!=0 && n<bufSize)
			{
				*(temp+n-n2)=*(buf+n);
				n++;
			}
			*(temp+n-n2)=0;
			this->xdata[i*this->numFeature+j]=atof(temp);
		}
		i++;
	}
	if (i!=this->numSample)
		ret = false;
	delete[] buf;
	delete[] temp;
    ifs.close();
	return ret;
}

bool CSampleData::writeFile(char* fileName)
{
	bool ret = true;
	ofstream ofs(fileName);
	int i, j, k, index;
	char* str = NULL;
	char* buf = new char[256];
	bool flag = false;
	DOUBLE temp;
	
	for (i=0; i<this->numSample; i++) {
		if (flag)
			ofs.write("\n", 1);
		else
			flag = true;
		str = this->nameClass[this->ydata[i]];
		ofs.write(str, strlen(str));
		ofs.write(",", 1);
		for (j=0; j<this->numFeature; j++)
		{
			temp = this->xdata[i*this->numFeature+j];
			if (temp!=((DOUBLE)((int)temp)))
				sprintf(buf, "%.6f", temp);
			else
				sprintf(buf, "%d", (int)temp);
			ofs.write(buf, strlen(buf));
			if (j!=(this->numFeature-1))
				ofs.write(",", 1);
		}
	}
	ofs.close();
	return ret;
}

bool CSampleData::normalize()
{
	bool ret = true;
	int i, j, index;
	this->maxValue = new DOUBLE[this->numFeature];
	this->minValue = new DOUBLE[this->numFeature];
	DOUBLE temp;
	for (j=0; j<this->numFeature; j++)
	{
		this->maxValue[j] = this->xdata[j];
		this->minValue[j] = this->xdata[j];
	}
	for (i=1; i<this->numSample; i++)
	{
		for (j=0; j<this->numFeature; j++)
		{
			temp = this->xdata[i*this->numFeature+j];
			if (this->maxValue[j]<temp)
				this->maxValue[j]=temp;
			else if (this->minValue[j]>temp)
				this->minValue[j]=temp;
		}
	}
	for (j=0; j<this->numFeature; j++)
	{
		if (this->minValue[j]<this->maxValue[j])
		{
			temp = this->maxValue[j]-this->minValue[j];
			for (i=0; i<this->numSample; i++)
			{
				index = i*this->numFeature+j;
				this->xdata[index] = (this->xdata[index]-this->minValue[j])/temp;
			}
		}
		else
		{
			for (i=0; i<this->numSample; i++)
			{
				this->xdata[i*this->numFeature+j] = 0.5;
			}
		}
	}
	return ret;
}

int CSampleData::searchClassName(char* className)
{
	for (int n=0; n<this->numClass; n++)
	{
		if (strcmp (className, this->nameClass[n])==0)
			return n;
	}
	return -1;
}

⌨️ 快捷键说明

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