第三次实现Logistic Regression(c++)_实现(一)

1. scale

为什么要对输入数据做scale?在《再次实现Logistic Regression(c++)_实现和测试》给出的理由是这样一句话“由于sigmoid函数在计算机中的精度限制,我们必须对实值输入进行归一化处理。” 具体的来说,是指数函数exp在计算中的精度限制,才需要对数据进行处理。

scale的接口为

// scale all of the sample values and put the result into txt
bool ScaleAllSampleValTxt (const char * sFileIn, int iFeatureNum, const char * sFileOut);

输入是原始sample文件,需要制定最大feature数目(其实也可以在读取文件的过程中得知,不过效率会比较低,需要动态维护feature存储空间),scale之后输出到文本文件中。该函数调用了两个private函数:

// read a sample from a line, return false if fail
bool ReadSampleFrmLine (string & sLine, Sample & theSample);
// load all of the samples into sample vector, this is for scale samples
bool LoadAllSamples (const char * sFileName, vector<Sample> & SampleVec);
scaling过程中用到了常数smothing fator,用来避免scaling过程中除数为零的情况

// the minimal float number for smoothing for scaling the input samples
#define SMOOTHFATOR 1e-100

代码实现很简单,如下:

// the input format is: iClassId featureid1:featurevalue1 featureid2:featurevalue2 ... 
bool LogisticRegression::ReadSampleFrmLine (string & sLine, Sample & theSample)
{
	istringstream isLine (sLine);
	if (!isLine)
		return false;

	// the class index
	isLine >> theSample.iClass;

	// the feature and its value
	string sItem;
	while (isLine >> sItem )
	{
		FeaValNode theNode;
		string::size_type iPos = sItem.find (':');
		theNode.iFeatureId = atoi (sItem.substr(0, iPos).c_str());
		theNode.dValue = atof (sItem.substr (iPos+1).c_str());
		theSample.FeaValNodeVec.push_back (theNode);
	}

	return true;
}

bool LogisticRegression::LoadAllSamples (const char * sFileName, vector<Sample> & SampleVec)
{
	ifstream in (sFileName);
	if (!in)
	{
		cerr << "Can not open the file of " << sFileName << endl;
		return false;
	}

	SampleVec.clear();

	string sLine;
	while (getline (in, sLine))
	{
		Sample theSample;
		if (ReadSampleFrmLine (sLine, theSample))
			SampleVec.push_back (theSample);
	}

	return true;
}

bool LogisticRegression::ScaleAllSampleValTxt (const char * sFileIn, int iFeatureNum, const char * sFileOut)
{
	ifstream in (sFileIn);
	ofstream out (sFileOut);
	if (!in || !out)
	{
		cerr << "Can not open the file" << endl;
		return false;
	}

	// load all of the samples
	vector<Sample> SampleVec;
	if (!LoadAllSamples (sFileIn, SampleVec))
		return false;

	// get the max value of each feature
	vector<double> FeaMaxValVec (iFeatureNum, 0.0); 
	vector<Sample>::iterator p = SampleVec.begin();
	while (p != SampleVec.end())
	{
		vector<FeaValNode>::iterator pFea = p->FeaValNodeVec.begin();
		while (pFea != p->FeaValNodeVec.end())
		{
			if (pFea->iFeatureId < iFeatureNum 
				&& pFea->dValue > FeaMaxValVec[pFea->iFeatureId])
				FeaMaxValVec[pFea->iFeatureId] = pFea->dValue;
			pFea++;
		}
		p++;
	}

	// smoothing FeaMaxValVec to avoid zero value
	vector<double>::iterator pFeaMax = FeaMaxValVec.begin();
	while (pFeaMax != FeaMaxValVec.end())
	{
		*pFeaMax += SMOOTHFATOR;
		pFeaMax++;
	}

	// scale the samples
	p = SampleVec.begin();
	while (p != SampleVec.end())
	{
		vector<FeaValNode>::iterator pFea = p->FeaValNodeVec.begin();
		while (pFea != p->FeaValNodeVec.end())
		{
			if (pFea->iFeatureId < iFeatureNum)
				pFea->dValue /= FeaMaxValVec[pFea->iFeatureId];
			pFea++;
		}
		p++;
	}

	// dump the result
	p = SampleVec.begin();
	while (p != SampleVec.end())
	{
		out << p->iClass << " ";
		vector<FeaValNode>::iterator pFea = p->FeaValNodeVec.begin();
		while (pFea != p->FeaValNodeVec.end())
		{
			out << pFea->iFeatureId << ":" << pFea->dValue << " ";
			pFea++;
		}
		out << "\n";
		p++;
	}


	return true;
}

调用如下:

ScaleAllSampleValTxt ("..\\Data\\SamplesMultClassesTrain.txt", 25334, "..\\Data\\SamplesMultClassesTrainScale.txt");
ScaleAllSampleValTxt ("..\\Data\\SamplesMultClassesTest.txt", 25334, "..\\Data\\SamplesMultClassesTestScale.txt");

觉去了,明天继续码。


转载请注明出处:http://blog.csdn.net/xceman1997/article/details/18428391

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值