C++ 将float数据归一化到[0,1]

<pre name="code" class="cpp">std::ofstream fileout(‘features_normalize.txt’,std::ios::app); 
float ymax = 1; //归一化数据范围
float ymin = 0; 
float feature_blob_data[5] = [-1.254,0.254,2.147,-3.456,4.257];//初始化浮点数
std::vector<float> features; //临时特征向量
for (int d = 0; d < 5; ++d) 
	  features.push_back(feature_blob_data[d]);
//特征归一化
float dMaxValue = *max_element(features.begin(),features.end());  //求最大值
std::cout<<"maxdata"<<dMaxValue<<'\n';
float dMinValue = *min_element(features.begin(),features.end());  //求最小值
std::cout<<"mindata"<<dMinValue<<'\n';
for (int f = 0; f < features.size(); ++f) {
    features[f] = (ymax-ymin)*(features[f]-dMinValue)/(dMaxValue-dMinValue)+ymin;	  
   //极小值限制
  fileout<<f+1<<":"<< features[f]+1e-8<<" "; 
        }
fileout<<"\n";   
features.clear();//删除容器


 
 

你可能感兴趣的:(C++)