第三次实现Logistic Regression(c++)_实现(一)
2014-01-18 01:27
246 查看
1. scale
为什么要对输入数据做scale?在《再次实现Logistic Regression(c++)_实现和测试》给出的理由是这样一句话“由于sigmoid函数在计算机中的精度限制,我们必须对实值输入进行归一化处理。” 具体的来说,是指数函数exp在计算中的精度限制,才需要对数据进行处理。
scale的接口为
输入是原始sample文件,需要制定最大feature数目(其实也可以在读取文件的过程中得知,不过效率会比较低,需要动态维护feature存储空间),scale之后输出到文本文件中。该函数调用了两个private函数:
代码实现很简单,如下:
调用如下:
觉去了,明天继续码。
转载请注明出处:http://blog.csdn.net/xceman1997/article/details/18428391
为什么要对输入数据做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
相关文章推荐
- 第三次实现Logistic Regression(c++)_接口
- 第三次实现Logistic Regression(c++)_实现(二)
- 第三次实现Logistic Regression(c++)_再尝试
- 第三次实现Logistic Regression(c++)_测试
- C++根据头文件自动生成实现文件框架(支持模版)
- 设计模式解析和实现(C++)之十九-Memento模式
- 关于如何实现程序一天只启动一次的想法(C++实现)
- 逆波兰表达式实现计算器(附c++代码)
- 【日期类】C++实现
- 仿基因编程原理及其C++实现
- 使用C++容器vector实现的两路归并排序
- C++ 二叉树的实现
- C++实现全局鼠标、键盘消息hook,支持事件
- C++程序设计实践指导1.3求任意整数降序数改写要求实现
- C++实现串口通信
- C/C++练习题 (将n(n<20)个数按输入时顺序的逆序排列,用函数实现。)
- 设计模式-迭代器模式 C++实现
- Head First设计模式C++实现--第十章:状态(State)模式
- 利用C语言中的函数指针实现c++中的虚函数
- 数据结构之单链表 C++ 实现