?? traindata.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 + -